LLVM  15.0.0git
Classes | Namespaces | Macros | Typedefs | Functions | Variables
IROutliner.cpp File Reference
#include "llvm/Transforms/IPO/IROutliner.h"
#include "llvm/Analysis/IRSimilarityIdentifier.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/IPO.h"
#include <vector>
Include dependency graph for IROutliner.cpp:

Go to the source code of this file.

Classes

struct  OutlinableGroup
 The OutlinableGroup holds all the overarching information for outlining a set of regions that are structurally similar to one another, such as the types of the overall function, the output blocks, the sets of stores needed and a list of the different regions. More...
 

Namespaces

 llvm
 This is an optimization pass for GlobalISel generic memory operations.
 

Macros

#define DEBUG_TYPE   "iroutliner"
 

Typedefs

using ArgLocWithBBCanon = std::pair< unsigned, unsigned >
 
using CanonList = SmallVector< unsigned, 2 >
 
using PHINodeData = std::pair< ArgLocWithBBCanon, CanonList >
 

Functions

static void moveBBContents (BasicBlock &SourceBB, BasicBlock &TargetBB)
 Move the contents of SourceBB to before the last instruction of TargetBB. More...
 
static void getSortedConstantKeys (std::vector< Value * > &SortedKeys, DenseMap< Value *, BasicBlock * > &Map)
 A function to sort the keys of Map, which must be a mapping of constant values to basic blocks and return it in SortedKeys. More...
 
static void replaceTargetsFromPHINode (BasicBlock *PHIBlock, BasicBlock *Find, BasicBlock *Replace, DenseSet< BasicBlock * > &Included)
 Rewrite the BranchInsts in the incoming blocks to PHIBlock that are found in Included to branch to BasicBlock Replace if they currently branch to the BasicBlock Find. More...
 
static Optional< bool > constantMatches (Value *V, unsigned GVN, DenseMap< unsigned, Constant * > &GVNToConstant)
 Find whether V matches the Constants previously found for the GVN. More...
 
static ValuefindOutputMapping (const DenseMap< Value *, Value * > OutputMappings, Value *Input)
 Check the OutputMappings structure for value Input, if it exists it has been used as an output for outlining, and has been renamed, and we return the new value, otherwise, we return the same value. More...
 
static bool collectRegionsConstants (OutlinableRegion &Region, DenseMap< unsigned, Constant * > &GVNToConstant, DenseSet< unsigned > &NotSame)
 Find whether Region matches the global value numbering to Constant mapping found so far. More...
 
static DISubprogramgetSubprogramOrNull (OutlinableGroup &Group)
 Get the subprogram if it exists for one of the outlined regions. More...
 
static void moveFunctionData (Function &Old, Function &New, DenseMap< Value *, BasicBlock * > &NewEnds)
 Move each BasicBlock in Old to New. More...
 
static void findConstants (IRSimilarityCandidate &C, DenseSet< unsigned > &NotSame, std::vector< unsigned > &Inputs)
 Find the the constants that will need to be lifted into arguments as they are not the same in each instance of the region. More...
 
static void mapInputsToGVNs (IRSimilarityCandidate &C, SetVector< Value * > &CurrentInputs, const DenseMap< Value *, Value * > &OutputMappings, std::vector< unsigned > &EndInputNumbers)
 Find the GVN for the inputs that have been found by the CodeExtractor. More...
 
static void remapExtractedInputs (const ArrayRef< Value * > ArgInputs, const DenseMap< Value *, Value * > &OutputMappings, SetVector< Value * > &RemappedArgInputs)
 Find the original value for the ArgInput values if any one of them was replaced during a previous extraction. More...
 
static void getCodeExtractorArguments (OutlinableRegion &Region, std::vector< unsigned > &InputGVNs, DenseSet< unsigned > &NotSame, DenseMap< Value *, Value * > &OutputMappings, SetVector< Value * > &ArgInputs, SetVector< Value * > &Outputs)
 Find the input GVNs and the output values for a region of Instructions. More...
 
static void findExtractedInputToOverallInputMapping (OutlinableRegion &Region, std::vector< unsigned > &InputGVNs, SetVector< Value * > &ArgInputs)
 Look over the inputs and map each input argument to an argument in the overall function for the OutlinableRegions. More...
 
static bool outputHasNonPHI (Value *V, unsigned PHILoc, PHINode &PN, SmallPtrSet< BasicBlock *, 1 > &Exits, DenseSet< BasicBlock * > &BlocksInRegion)
 Check if the V has any uses outside of the region other than PN. More...
 
static void analyzeExitPHIsForOutputUses (BasicBlock *CurrentExitFromRegion, SmallPtrSet< BasicBlock *, 1 > &PotentialExitsFromRegion, DenseSet< BasicBlock * > &RegionBlocks, SetVector< Value * > &Outputs, DenseSet< Value * > &OutputsReplacedByPHINode, DenseSet< Value * > &OutputsWithNonPhiUses)
 Test whether CurrentExitFromRegion contains any PhiNodes that should be considered outputs. More...
 
static hash_code encodePHINodeData (PHINodeData &PND)
 Encode PND as an integer for easy lookup based on the argument location, the parent BasicBlock canonical numbering, and the canonical numbering of the values stored in the PHINode. More...
 
static Optional< unsigned > getGVNForPHINode (OutlinableRegion &Region, PHINode *PN, DenseSet< BasicBlock * > &Blocks, unsigned AggArgIdx)
 Create a special GVN for PHINodes that will be used outside of the region. More...
 
static void findExtractedOutputToOverallOutputMapping (OutlinableRegion &Region, SetVector< Value * > &Outputs)
 Create a mapping of the output arguments for the Region to the output arguments of the overall outlined function. More...
 
CallInstreplaceCalledFunction (Module &M, OutlinableRegion &Region)
 Replace the extracted function in the Region with a call to the overall function constructed from the deduplicated similar regions, replacing and remapping the values passed to the extracted function as arguments to the new arguments of the overall function. More...
 
static BasicBlockfindOrCreatePHIBlock (OutlinableGroup &Group, Value *RetVal)
 Find or create a BasicBlock in the outlined function containing PhiBlocks for RetVal. More...
 
static ValuegetPassedArgumentInAlreadyOutlinedFunction (const Argument *A, const OutlinableRegion &Region)
 For the function call now representing the Region, find the passed value to that call that represents Argument A at the call location if the call has already been replaced with a call to the overall, aggregate function. More...
 
static ValuegetPassedArgumentAndAdjustArgumentLocation (const Argument *A, const OutlinableRegion &Region)
 For the function call now representing the Region, find the passed value to that call that represents Argument A at the call location if the call has only been replaced by the call to the aggregate function. More...
 
static void findCanonNumsForPHI (PHINode *PN, OutlinableRegion &Region, const DenseMap< Value *, Value * > &OutputMappings, SmallVector< std::pair< unsigned, BasicBlock * >> &CanonNums, bool ReplacedWithOutlinedCall=true)
 Find the canonical numbering for the incoming Values into the PHINode PN. More...
 
static PHINodefindOrCreatePHIInBlock (PHINode &PN, OutlinableRegion &Region, BasicBlock *OverallPhiBlock, const DenseMap< Value *, Value * > &OutputMappings, DenseSet< PHINode * > &UsedPHIs)
 Find, or add PHINode PN to the combined PHINode Block OverallPHIBlock in order to condense the number of instructions added to the outlined function. More...
 
static void replaceArgumentUses (OutlinableRegion &Region, DenseMap< Value *, BasicBlock * > &OutputBBs, const DenseMap< Value *, Value * > &OutputMappings, bool FirstFunction=false)
 
void replaceConstants (OutlinableRegion &Region)
 Within an extracted function, replace the constants that need to be lifted into arguments with the actual argument. More...
 
Optional< unsigned > findDuplicateOutputBlock (DenseMap< Value *, BasicBlock * > &OutputBBs, std::vector< DenseMap< Value *, BasicBlock * >> &OutputStoreBBs)
 It is possible that there is a basic block that already performs the same stores. More...
 
static bool analyzeAndPruneOutputBlocks (DenseMap< Value *, BasicBlock * > &BlocksToPrune, OutlinableRegion &Region)
 Remove empty output blocks from the outlined region. More...
 
static void alignOutputBlockWithAggFunc (OutlinableGroup &OG, OutlinableRegion &Region, DenseMap< Value *, BasicBlock * > &OutputBBs, DenseMap< Value *, BasicBlock * > &EndBBs, const DenseMap< Value *, Value * > &OutputMappings, std::vector< DenseMap< Value *, BasicBlock * >> &OutputStoreBBs)
 For the outlined section, move needed the StoreInsts for the output registers into their own block. More...
 
static void createAndInsertBasicBlocks (DenseMap< Value *, BasicBlock * > &OldMap, DenseMap< Value *, BasicBlock * > &NewMap, Function *ParentFunc, Twine BaseName)
 Takes in a mapping, OldMap of ConstantValues to BasicBlocks, sorts keys, before creating a basic block for each NewMap, and inserting into the new block. More...
 
void createSwitchStatement (Module &M, OutlinableGroup &OG, DenseMap< Value *, BasicBlock * > &EndBBs, std::vector< DenseMap< Value *, BasicBlock * >> &OutputStoreBBs)
 Create the switch statement for outlined function to differentiate between all the output blocks. More...
 
static void fillOverallFunction (Module &M, OutlinableGroup &CurrentGroup, std::vector< DenseMap< Value *, BasicBlock * >> &OutputStoreBBs, std::vector< Function * > &FuncsToRemove, const DenseMap< Value *, Value * > &OutputMappings)
 Fill the new function that will serve as the replacement function for all of the extracted regions of a certain structure from the first region in the list of regions. More...
 
static bool nextIRInstructionDataMatchesNextInst (IRInstructionData &ID)
 Checks that the next instruction in the InstructionDataList matches the next instruction in the module. More...
 
static ValuefindOutputValueInRegion (OutlinableRegion &Region, unsigned OutputCanon)
 For the OutputCanon number passed in find the value represented by this canonical number. More...
 
static InstructionCost findCostForOutputBlocks (Module &M, OutlinableGroup &CurrentGroup, TargetTransformInfo &TTI)
 Find the extra instructions needed to handle any output values for the region. More...
 
 INITIALIZE_PASS_BEGIN (IROutlinerLegacyPass, "iroutliner", "IR Outliner", false, false) INITIALIZE_PASS_END(IROutlinerLegacyPass
 

Variables

static cl::opt< bool > EnableLinkOnceODRIROutlining ("enable-linkonceodr-ir-outlining", cl::Hidden, cl::desc("Enable the IR outliner on linkonceodr functions"), cl::init(false))
 
static cl::opt< bool > NoCostModel ("ir-outlining-no-cost", cl::init(false), cl::ReallyHidden, cl::desc("Debug option to outline greedily, without restriction that " "calculated benefit outweighs cost"))
 
 iroutliner
 
IR Outliner
 
IR false
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "iroutliner"

Definition at line 31 of file IROutliner.cpp.

Typedef Documentation

◆ ArgLocWithBBCanon

using ArgLocWithBBCanon = std::pair<unsigned, unsigned>

Definition at line 1133 of file IROutliner.cpp.

◆ CanonList

using CanonList = SmallVector<unsigned, 2>

Definition at line 1135 of file IROutliner.cpp.

◆ PHINodeData

Definition at line 1138 of file IROutliner.cpp.

Function Documentation

◆ alignOutputBlockWithAggFunc()

static void alignOutputBlockWithAggFunc ( OutlinableGroup OG,
OutlinableRegion Region,
DenseMap< Value *, BasicBlock * > &  OutputBBs,
DenseMap< Value *, BasicBlock * > &  EndBBs,
const DenseMap< Value *, Value * > &  OutputMappings,
std::vector< DenseMap< Value *, BasicBlock * >> &  OutputStoreBBs 
)
static

For the outlined section, move needed the StoreInsts for the output registers into their own block.

Then, determine if there is a duplicate output block already created.

Parameters
[in]OG- The OutlinableGroup of regions to be outlined.
[in]Region- The OutlinableRegion that is being analyzed.
[in,out]OutputBBs- the blocks that stores for this region will be placed in.
[in]EndBBs- the final blocks of the extracted function.
[in]OutputMappings- OutputMappings the mapping of values that have been replaced by a new output value.
[in,out]OutputStoreBBs- The existing output blocks.

Definition at line 2064 of file IROutliner.cpp.

References analyzeAndPruneOutputBlocks(), llvm::BranchInst::Create(), llvm::dbgs(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), findDuplicateOutputBlock(), llvm::Optional< T >::getValue(), and LLVM_DEBUG.

◆ analyzeAndPruneOutputBlocks()

static bool analyzeAndPruneOutputBlocks ( DenseMap< Value *, BasicBlock * > &  BlocksToPrune,
OutlinableRegion Region 
)
static

Remove empty output blocks from the outlined region.

Parameters
BlocksToPrune- Mapping of return values output blocks for the Region.
Region- The OutlinableRegion we are analyzing.

Definition at line 2017 of file IROutliner.cpp.

References llvm::BasicBlock::eraseFromParent(), llvm::BasicBlock::size(), and ToRemove.

Referenced by alignOutputBlockWithAggFunc(), and fillOverallFunction().

◆ analyzeExitPHIsForOutputUses()

static void analyzeExitPHIsForOutputUses ( BasicBlock CurrentExitFromRegion,
SmallPtrSet< BasicBlock *, 1 > &  PotentialExitsFromRegion,
DenseSet< BasicBlock * > &  RegionBlocks,
SetVector< Value * > &  Outputs,
DenseSet< Value * > &  OutputsReplacedByPHINode,
DenseSet< Value * > &  OutputsWithNonPhiUses 
)
static

Test whether CurrentExitFromRegion contains any PhiNodes that should be considered outputs.

A PHINodes is an output when more than one incoming value has been marked by the CodeExtractor as an output.

Parameters
CurrentExitFromRegion[in] - The block to analyze.
PotentialExitsFromRegion[in] - The potential exit blocks from the region.
RegionBlocks[in] - The basic blocks in the region.
Outputs[in, out] - The existing outputs for the region, we may add PHINodes to this as we find that they replace output values.
OutputsReplacedByPHINode[out] - A set containing outputs that are totally replaced by a PHINode.
OutputsWithNonPhiUses[out] - A set containing outputs that are used in PHINodes, but have other uses, and should still be considered outputs.

Definition at line 1085 of file IROutliner.cpp.

References llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::contains(), llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::contains(), E, llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::erase(), I, llvm::SetVector< T, Vector, Set >::insert(), llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::insert(), outputHasNonPHI(), and llvm::BasicBlock::phis().

Referenced by findExtractedOutputToOverallOutputMapping().

◆ collectRegionsConstants()

static bool collectRegionsConstants ( OutlinableRegion Region,
DenseMap< unsigned, Constant * > &  GVNToConstant,
DenseSet< unsigned > &  NotSame 
)
static

Find whether Region matches the global value numbering to Constant mapping found so far.

Parameters
Region- The OutlinableRegion we are checking for constants
GVNToConstant- The mapping of global value number to Constants.
NotSame- The set of global value numbers that do not have the same constant in each region.
Returns
true if all Constants are the same in every use of a Constant in Region and false if not

Definition at line 544 of file IROutliner.cpp.

References assert(), constantMatches(), llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::contains(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find(), llvm::Optional< T >::getValue(), and llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::insert().

Referenced by OutlinableGroup::findSameConstants().

◆ constantMatches()

static Optional<bool> constantMatches ( Value V,
unsigned  GVN,
DenseMap< unsigned, Constant * > &  GVNToConstant 
)
static

Find whether V matches the Constants previously found for the GVN.

Parameters
V- The value to check for consistency.
GVN- The global value number assigned to V.
GVNToConstant- The mapping of global value number to Constants.
Returns
true if the Value matches the Constant mapped to by V and false if it V is a Constant but does not match.
None if V is not a Constant.

Definition at line 458 of file IROutliner.cpp.

References llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert(), and llvm::None.

Referenced by collectRegionsConstants().

◆ createAndInsertBasicBlocks()

static void createAndInsertBasicBlocks ( DenseMap< Value *, BasicBlock * > &  OldMap,
DenseMap< Value *, BasicBlock * > &  NewMap,
Function ParentFunc,
Twine  BaseName 
)
static

Takes in a mapping, OldMap of ConstantValues to BasicBlocks, sorts keys, before creating a basic block for each NewMap, and inserting into the new block.

Each BasicBlock is named with the scheme "<basename>_<key_idx>".

Parameters
OldMap[in] - The mapping to base the new mapping off of.
NewMap[out] - The output mapping using the keys of OldMap.
ParentFunc[in] - The function to put the new basic block in.
BaseName[in] - The start of the BasicBlock names to be appended to by an index value.

Definition at line 2120 of file IROutliner.cpp.

References llvm::BasicBlock::Create(), llvm::Function::getContext(), getSortedConstantKeys(), and llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert().

Referenced by createSwitchStatement(), and fillOverallFunction().

◆ createSwitchStatement()

void createSwitchStatement ( Module M,
OutlinableGroup OG,
DenseMap< Value *, BasicBlock * > &  EndBBs,
std::vector< DenseMap< Value *, BasicBlock * >> &  OutputStoreBBs 
)

◆ encodePHINodeData()

static hash_code encodePHINodeData ( PHINodeData PND)
static

Encode PND as an integer for easy lookup based on the argument location, the parent BasicBlock canonical numbering, and the canonical numbering of the values stored in the PHINode.

Parameters
PND- The data to hash.
Returns
The hash code of PND.

Definition at line 1146 of file IROutliner.cpp.

References llvm::hash_combine(), llvm::hash_combine_range(), and llvm::hash_value().

Referenced by getGVNForPHINode().

◆ fillOverallFunction()

static void fillOverallFunction ( Module M,
OutlinableGroup CurrentGroup,
std::vector< DenseMap< Value *, BasicBlock * >> &  OutputStoreBBs,
std::vector< Function * > &  FuncsToRemove,
const DenseMap< Value *, Value * > &  OutputMappings 
)
static

Fill the new function that will serve as the replacement function for all of the extracted regions of a certain structure from the first region in the list of regions.

Replace this first region's extracted function with the new overall function.

Parameters
[in]M- The module we are outlining from.
[in]CurrentGroup- The group of regions to be outlined.
[in,out]OutputStoreBBs- The output blocks for each different set of stores needed for the different functions.
[in,out]FuncsToRemove- Extracted functions to erase from module once outlining is complete.
[in]OutputMappings- Extracted functions to erase from module once outlining is complete.

Definition at line 2239 of file IROutliner.cpp.

References llvm::Function::addFnAttr(), analyzeAndPruneOutputBlocks(), llvm::OutlinableRegion::Call, llvm::BranchInst::Create(), createAndInsertBasicBlocks(), llvm::dbgs(), OutlinableGroup::EndBBs, llvm::OutlinableRegion::ExtractedFunction, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::Function::getAttributes(), llvm::AttributeList::getFnAttrs(), LLVM_DEBUG, M, moveFunctionData(), OutlinableGroup::OutlinedFunction, llvm::OutlinableRegion::OutputBlockNum, OutlinableGroup::Regions, replaceArgumentUses(), replaceCalledFunction(), and replaceConstants().

◆ findCanonNumsForPHI()

static void findCanonNumsForPHI ( PHINode PN,
OutlinableRegion Region,
const DenseMap< Value *, Value * > &  OutputMappings,
SmallVector< std::pair< unsigned, BasicBlock * >> &  CanonNums,
bool  ReplacedWithOutlinedCall = true 
)
static

Find the canonical numbering for the incoming Values into the PHINode PN.

Parameters
PN[in] - The PHINode that we are finding the canonical numbers for.
Region[in] - The OutlinableRegion containing PN.
OutputMappings[in] - The mapping of output values from outlined region to their original values.
CanonNums[out] - The canonical numbering for the incoming values to PN paired with their incoming block.
ReplacedWithOutlinedCall- A flag to use the extracted function call of Region rather than the overall function's call.

Definition at line 1628 of file IROutliner.cpp.

References assert(), findOutputMapping(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), getPassedArgumentAndAdjustArgumentLocation(), and getPassedArgumentInAlreadyOutlinedFunction().

Referenced by findOrCreatePHIInBlock().

◆ findConstants()

static void findConstants ( IRSimilarityCandidate C,
DenseSet< unsigned > &  NotSame,
std::vector< unsigned > &  Inputs 
)
static

Find the the constants that will need to be lifted into arguments as they are not the same in each instance of the region.

Parameters
[in]C- The IRSimilarityCandidate containing the region we are analyzing.
[in]NotSame- The set of global value numbers that do not have a single Constant across all OutlinableRegions similar to C.
[out]Inputs- The list containing the global value numbers of the arguments needed for the region of code.

Definition at line 770 of file IROutliner.cpp.

References llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::contains(), and llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::insert().

Referenced by getCodeExtractorArguments().

◆ findCostForOutputBlocks()

static InstructionCost findCostForOutputBlocks ( Module M,
OutlinableGroup CurrentGroup,
TargetTransformInfo TTI 
)
static

◆ findDuplicateOutputBlock()

Optional<unsigned> findDuplicateOutputBlock ( DenseMap< Value *, BasicBlock * > &  OutputBBs,
std::vector< DenseMap< Value *, BasicBlock * >> &  OutputStoreBBs 
)

It is possible that there is a basic block that already performs the same stores.

This returns a duplicate block, if it exists

Parameters
OutputBBs[in] the blocks we are looking for a duplicate of.
OutputStoreBBs[in] The existing output blocks.
Returns
an optional value with the number output block if there is a match.

Definition at line 1962 of file IROutliner.cpp.

References llvm::BasicBlock::begin(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), I, llvm::None, and llvm::BasicBlock::size().

Referenced by alignOutputBlockWithAggFunc().

◆ findExtractedInputToOverallInputMapping()

static void findExtractedInputToOverallInputMapping ( OutlinableRegion Region,
std::vector< unsigned > &  InputGVNs,
SetVector< Value * > &  ArgInputs 
)
static

Look over the inputs and map each input argument to an argument in the overall function for the OutlinableRegions.

This creates a way to replace the arguments of the extracted function with the arguments of the new overall function.

Parameters
[in,out]Region- The region of code to be analyzed.
[in]InputGVNs- The global value numbering of the input values collected.
[in]ArgInputs- The values of the arguments to the extracted function.

Definition at line 926 of file IROutliner.cpp.

References OutlinableGroup::ArgumentTypes, assert(), OutlinableGroup::CanonicalNumberToAggArg, llvm::SetVector< T, Vector, Set >::count(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::Optional< T >::getValue(), OutlinableGroup::InputTypesSet, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), OutlinableGroup::NumAggregateInputs, and OutlinableGroup::SwiftErrorArgument.

◆ findExtractedOutputToOverallOutputMapping()

static void findExtractedOutputToOverallOutputMapping ( OutlinableRegion Region,
SetVector< Value * > &  Outputs 
)
static

◆ findOrCreatePHIBlock()

static BasicBlock* findOrCreatePHIBlock ( OutlinableGroup Group,
Value RetVal 
)
static

Find or create a BasicBlock in the outlined function containing PhiBlocks for RetVal.

Parameters
Group- The OutlinableGroup containing the information about the overall outlined function.
RetVal- The return value or exit option that we are currently evaluating.
Returns
The found or newly created BasicBlock to contain the needed PHINodes to be used as outputs.

Definition at line 1534 of file IROutliner.cpp.

References assert(), llvm::BasicBlock::Create(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), OutlinableGroup::EndBBs, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::BasicBlock::getContext(), llvm::BasicBlock::getParent(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), OutlinableGroup::PHIBlocks, and llvm::predecessors().

Referenced by replaceArgumentUses().

◆ findOrCreatePHIInBlock()

static PHINode* findOrCreatePHIInBlock ( PHINode PN,
OutlinableRegion Region,
BasicBlock OverallPhiBlock,
const DenseMap< Value *, Value * > &  OutputMappings,
DenseSet< PHINode * > &  UsedPHIs 
)
static

Find, or add PHINode PN to the combined PHINode Block OverallPHIBlock in order to condense the number of instructions added to the outlined function.

Parameters
PN[in] - The PHINode that we are finding the canonical numbers for.
Region[in] - The OutlinableRegion containing PN.
OverallPhiBlock[in] - The overall PHIBlock we are trying to find PN in.
OutputMappings[in] - The mapping of output values from outlined region to their original values.
UsedPHIs[in, out] - The PHINodes in the block that have already been matched.
Returns
the newly found or created PHINode in OverallPhiBlock.

Definition at line 1672 of file IROutliner.cpp.

References assert(), llvm::BasicBlock::begin(), llvm::SmallVectorImpl< T >::clear(), llvm::Instruction::clone(), llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::contains(), findCanonNumsForPHI(), findOutputMapping(), llvm::Function::getArg(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert(), llvm::Instruction::insertBefore(), OutlinableGroup::OutlinedFunction, llvm::BasicBlock::phis(), OutlinableGroup::Regions, llvm::OutlinableRegion::RemappedArguments, llvm::PHINode::setIncomingBlock(), and llvm::PHINode::setIncomingValue().

Referenced by replaceArgumentUses().

◆ findOutputMapping()

static Value* findOutputMapping ( const DenseMap< Value *, Value * >  OutputMappings,
Value Input 
)
static

Check the OutputMappings structure for value Input, if it exists it has been used as an output for outlining, and has been renamed, and we return the new value, otherwise, we return the same value.

Parameters
OutputMappings[in] - The mapping of values to their renamed value after being used as an output for an outlined region.
Input[in] - The value to find the remapped value of, if it exists.
Returns
The remapped value if it has been renamed, and the same value if has not.

Definition at line 525 of file IROutliner.cpp.

References llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end(), and llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find().

Referenced by findCanonNumsForPHI(), and findOrCreatePHIInBlock().

◆ findOutputValueInRegion()

static Value* findOutputValueInRegion ( OutlinableRegion Region,
unsigned  OutputCanon 
)
static

For the OutputCanon number passed in find the value represented by this canonical number.

If it is from a PHINode, we pick the first incoming value and return that Value instead.

Parameters
Region- The OutlinableRegion to get the Value from.
OutputCanon- The canonical number to find the Value from.
Returns
The Value represented by a canonical number OutputCanon in Region.

Definition at line 2491 of file IROutliner.cpp.

References assert(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), OutlinableGroup::PHINodeGVNToGVNs, and OutlinableGroup::PHINodeGVNTracker.

Referenced by findCostForOutputBlocks().

◆ getCodeExtractorArguments()

static void getCodeExtractorArguments ( OutlinableRegion Region,
std::vector< unsigned > &  InputGVNs,
DenseSet< unsigned > &  NotSame,
DenseMap< Value *, Value * > &  OutputMappings,
SetVector< Value * > &  ArgInputs,
SetVector< Value * > &  Outputs 
)
static

Find the input GVNs and the output values for a region of Instructions.

Using the code extractor, we collect the inputs to the extracted function.

The Region can be identified as needing to be ignored in this function. It should be checked whether it should be ignored after a call to this function.

Parameters
[in,out]Region- The region of code to be analyzed.
[out]InputGVNs- The global value numbers for the extracted arguments.
[in]NotSame- The global value numbers in the region that do not have the same constant value in the regions structurally similar to Region.
[in]OutputMappings- The mapping of values that have been replaced by a new output value after extraction.
[out]ArgInputs- The values of the inputs to the extracted function.
[out]Outputs- The set of values extracted by the CodeExtractor as outputs.

Definition at line 855 of file IROutliner.cpp.

References assert(), llvm::NVPTXISD::Dummy, findConstants(), llvm::SetVector< T, Vector, Set >::getArrayRef(), llvm::RegionBase< Tr >::getParent(), mapInputsToGVNs(), remapExtractedInputs(), llvm::SetVector< T, Vector, Set >::size(), and llvm::stable_sort().

◆ getGVNForPHINode()

static Optional<unsigned> getGVNForPHINode ( OutlinableRegion Region,
PHINode PN,
DenseSet< BasicBlock * > &  Blocks,
unsigned  AggArgIdx 
)
static

Create a special GVN for PHINodes that will be used outside of the region.

We create a hash code based on the Canonical number of the parent BasicBlock, the canonical numbering of the values stored in the PHINode and the aggregate argument location. This is used to find whether this PHINode type has been given a canonical numbering already. If not, we assign it a value and store it for later use. The value is returned to identify different output schemes for the set of regions.

Parameters
Region- The region that PN is an output for.
PN- The PHINode we are analyzing.
Blocks- The blocks for the region we are analyzing.
AggArgIdx- The argument PN will be stored into.
Returns
An optional holding the assigned canonical number, or None if there is some attribute of the PHINode blocking it from being used.

Definition at line 1166 of file IROutliner.cpp.

References assert(), llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::contains(), encodePHINodeData(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), llvm::IRSimilarity::IRSimilarityCandidate::getCanonicalNum(), llvm::IRSimilarity::IRSimilarityCandidate::getGVN(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::IRSimilarity::IRSimilarityCandidate::getStartBB(), llvm::Optional< T >::getValue(), OutlinableGroup::GVNsToPHINodeGVN, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::None, OutlinableGroup::PHINodeGVNToGVNs, OutlinableGroup::PHINodeGVNTracker, and llvm::predecessors().

Referenced by findExtractedOutputToOverallOutputMapping().

◆ getPassedArgumentAndAdjustArgumentLocation()

static Value* getPassedArgumentAndAdjustArgumentLocation ( const Argument A,
const OutlinableRegion Region 
)
static

For the function call now representing the Region, find the passed value to that call that represents Argument A at the call location if the call has only been replaced by the call to the aggregate function.

Parameters
A- The Argument to get the passed value for.
Region- The extracted Region corresponding to the outlined function.
Returns
The Value representing A at the call site.

Definition at line 1603 of file IROutliner.cpp.

Referenced by findCanonNumsForPHI().

◆ getPassedArgumentInAlreadyOutlinedFunction()

static Value* getPassedArgumentInAlreadyOutlinedFunction ( const Argument A,
const OutlinableRegion Region 
)
static

For the function call now representing the Region, find the passed value to that call that represents Argument A at the call location if the call has already been replaced with a call to the overall, aggregate function.

Parameters
A- The Argument to get the passed value for.
Region- The extracted Region corresponding to the outlined function.
Returns
The Value representing A at the call site.

Definition at line 1587 of file IROutliner.cpp.

Referenced by findCanonNumsForPHI().

◆ getSortedConstantKeys()

static void getSortedConstantKeys ( std::vector< Value * > &  SortedKeys,
DenseMap< Value *, BasicBlock * > &  Map 
)
static

A function to sort the keys of Map, which must be a mapping of constant values to basic blocks and return it in SortedKeys.

Parameters
SortedKeys- The vector the keys will be return in and sorted.
Map- The DenseMap containing keys to sort.

Definition at line 167 of file IROutliner.cpp.

References assert(), llvm::ConstantInt::getLimitedValue(), LHS, RHS, and llvm::stable_sort().

Referenced by createAndInsertBasicBlocks().

◆ getSubprogramOrNull()

static DISubprogram* getSubprogramOrNull ( OutlinableGroup Group)
static

Get the subprogram if it exists for one of the outlined regions.

Parameters
[in]Group- The set of regions to find a subprogram for.
Returns
the subprogram if it exists, or nullptr.

Definition at line 614 of file IROutliner.cpp.

References llvm::OutlinableRegion::Call, F, llvm::Instruction::getFunction(), and OutlinableGroup::Regions.

◆ INITIALIZE_PASS_BEGIN()

INITIALIZE_PASS_BEGIN ( IROutlinerLegacyPass  ,
"iroutliner"  ,
"IR Outliner ,
false  ,
false   
)

◆ mapInputsToGVNs()

static void mapInputsToGVNs ( IRSimilarityCandidate C,
SetVector< Value * > &  CurrentInputs,
const DenseMap< Value *, Value * > &  OutputMappings,
std::vector< unsigned > &  EndInputNumbers 
)
static

Find the GVN for the inputs that have been found by the CodeExtractor.

Parameters
[in]C- The IRSimilarityCandidate containing the region we are analyzing.
[in]CurrentInputs- The set of inputs found by the CodeExtractor.
[in]OutputMappings- The mapping of values that have been replaced by a new output value.
[out]EndInputNumbers- The global value numbers for the extracted arguments.

Definition at line 800 of file IROutliner.cpp.

References assert(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end(), and llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find().

Referenced by getCodeExtractorArguments().

◆ moveBBContents()

static void moveBBContents ( BasicBlock SourceBB,
BasicBlock TargetBB 
)
static

Move the contents of SourceBB to before the last instruction of TargetBB.

Parameters
SourceBB- the BasicBlock to pull Instructions from.
TargetBB- the BasicBlock to put Instruction into.

Definition at line 157 of file IROutliner.cpp.

References llvm::BasicBlock::end(), I, and llvm::make_early_inc_range().

Referenced by createSwitchStatement(), and llvm::OutlinableRegion::reattachCandidate().

◆ moveFunctionData()

static void moveFunctionData ( Function Old,
Function New,
DenseMap< Value *, BasicBlock * > &  NewEnds 
)
static

Move each BasicBlock in Old to New.

Parameters
[in]Old- The function to move the basic blocks from.
[in]New- The function to move the basic blocks to.
[out]NewEnds- The return blocks of the new overall function.

Definition at line 701 of file IROutliner.cpp.

References llvm::MDNode::get(), I, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), llvm::make_early_inc_range(), and llvm::updateLoopMetadataDebugLocations().

Referenced by fillOverallFunction().

◆ nextIRInstructionDataMatchesNextInst()

static bool nextIRInstructionDataMatchesNextInst ( IRInstructionData ID)
static

Checks that the next instruction in the InstructionDataList matches the next instruction in the module.

If they do not, there could be the possibility that extra code has been inserted, and we must ignore it.

Parameters
ID- The IRInstructionData to check the next instruction of.
Returns
true if the InstructionDataList and actual instruction match.

Definition at line 2333 of file IROutliner.cpp.

References llvm::Instruction::getNextNonDebugInstruction(), llvm::Instruction::getParent(), and llvm::BasicBlock::instructionsWithoutDebug().

◆ outputHasNonPHI()

static bool outputHasNonPHI ( Value V,
unsigned  PHILoc,
PHINode PN,
SmallPtrSet< BasicBlock *, 1 > &  Exits,
DenseSet< BasicBlock * > &  BlocksInRegion 
)
static

Check if the V has any uses outside of the region other than PN.

Parameters
V[in] - The value to check.
PHILoc[in] - The location in the PHINode of V.
PN[in] - The PHINode using V.
Exits[in] - The potential blocks we exit to from the outlined region.
BlocksInRegion[in] - The basic blocks contained in the region.
Returns
true if V has any use soutside its region other than PN.

Definition at line 1028 of file IROutliner.cpp.

References llvm::any_of(), llvm::PHINode::getNumIncomingValues(), and llvm::Value::users().

Referenced by analyzeExitPHIsForOutputUses().

◆ remapExtractedInputs()

static void remapExtractedInputs ( const ArrayRef< Value * >  ArgInputs,
const DenseMap< Value *, Value * > &  OutputMappings,
SetVector< Value * > &  RemappedArgInputs 
)
static

Find the original value for the ArgInput values if any one of them was replaced during a previous extraction.

Parameters
[in]ArgInputs- The inputs to be extracted by the code extractor.
[in]OutputMappings- The mapping of values that have been replaced by a new output value.
[out]RemappedArgInputs- The remapped values according to OutputMappings that will be extracted.

Definition at line 825 of file IROutliner.cpp.

References llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find(), and llvm::SetVector< T, Vector, Set >::insert().

Referenced by getCodeExtractorArguments().

◆ replaceArgumentUses()

static void replaceArgumentUses ( OutlinableRegion Region,
DenseMap< Value *, BasicBlock * > &  OutputBBs,
const DenseMap< Value *, Value * > &  OutputMappings,
bool  FirstFunction = false 
)
static

◆ replaceCalledFunction()

CallInst* replaceCalledFunction ( Module M,
OutlinableRegion Region 
)

Replace the extracted function in the Region with a call to the overall function constructed from the deduplicated similar regions, replacing and remapping the values passed to the extracted function as arguments to the new arguments of the overall function.

Parameters
[in]M- The module to outline from.
[in]Region- The regions of extracted code to be replaced with a new function.
Returns
a call instruction with the replaced function.

Definition at line 1421 of file IROutliner.cpp.

References llvm::Function::arg_size(), assert(), llvm::CallInst::Create(), llvm::dbgs(), llvm::RegionBase< Tr >::end(), llvm::Instruction::eraseFromParent(), llvm::ConstantInt::get(), llvm::ConstantPointerNull::get(), llvm::Function::getArg(), llvm::Function::getFunctionType(), llvm::Type::getInt32Ty(), llvm::Value::getType(), llvm::Optional< T >::getValue(), LLVM_DEBUG, M, OutlinableGroup::OutlinedFunction, OutlinableGroup::OutputGVNCombinations, llvm::Value::replaceAllUsesWith(), llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::size(), and OutlinableGroup::SwiftErrorArgument.

Referenced by fillOverallFunction().

◆ replaceConstants()

void replaceConstants ( OutlinableRegion Region)

Within an extracted function, replace the constants that need to be lifted into arguments with the actual argument.

Parameters
Region[in] - The region of extracted code to be changed.

Definition at line 1929 of file IROutliner.cpp.

References Arg, assert(), llvm::dbgs(), llvm::Function::getArg(), llvm::Use::getUser(), I, LLVM_DEBUG, OutlinableGroup::OutlinedFunction, and llvm::Value::replaceUsesWithIf().

Referenced by fillOverallFunction().

◆ replaceTargetsFromPHINode()

static void replaceTargetsFromPHINode ( BasicBlock PHIBlock,
BasicBlock Find,
BasicBlock Replace,
DenseSet< BasicBlock * > &  Included 
)
static

Rewrite the BranchInsts in the incoming blocks to PHIBlock that are found in Included to branch to BasicBlock Replace if they currently branch to the BasicBlock Find.

This is used to fix up the incoming basic blocks when PHINodes are included in outlined regions.

Parameters
PHIBlock- The BasicBlock containing the PHINodes that need to be checked.
Find- The successor block to be replaced.
Replace- The new succesor block to branch to.
Included- The set of blocks about to be outlined.

Definition at line 215 of file IROutliner.cpp.

References assert(), llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::contains(), llvm::BasicBlock::getTerminator(), and llvm::BasicBlock::phis().

Referenced by llvm::OutlinableRegion::reattachCandidate(), and llvm::OutlinableRegion::splitCandidate().

Variable Documentation

◆ EnableLinkOnceODRIROutlining

cl::opt<bool> EnableLinkOnceODRIROutlining("enable-linkonceodr-ir-outlining", cl::Hidden, cl::desc("Enable the IR outliner on linkonceodr functions"), cl::init(false))
static

Referenced by llvm::IROutliner::run().

◆ false

IR false

Definition at line 3078 of file IROutliner.cpp.

◆ iroutliner

iroutliner

Definition at line 3078 of file IROutliner.cpp.

◆ NoCostModel

cl::opt<bool> NoCostModel("ir-outlining-no-cost", cl::init(false), cl::ReallyHidden, cl::desc("Debug option to outline greedily, without restriction that " "calculated benefit outweighs cost"))
static

Referenced by llvm::IROutliner::run().

◆ Outliner

IR Outliner

Definition at line 3078 of file IROutliner.cpp.