LLVM  14.0.0git
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/DebugInfoMetadata.h"
#include "llvm/IR/DIBuilder.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 <map>
#include <set>
#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...

class  IROutlinerLegacyPass

## Macros

#define DEBUG_TYPE   "iroutliner"

## 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 Optional< bool > constantMatches (Value *V, unsigned GVN, DenseMap< unsigned, Constant * > &GVNToConstant)
Find whether V matches the Constants previously found for the GVN. 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 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 void replaceArgumentUses (OutlinableRegion &Region, DenseMap< Value *, BasicBlock * > &OutputBBs, 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)
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 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

cl::opt< bool > DisableBranches

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

## ◆ DEBUG_TYPE

 #define DEBUG_TYPE   "iroutliner"

Definition at line 32 of file IROutliner.cpp.

## ◆ 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 1314 of file IROutliner.cpp.

## ◆ 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 1267 of file IROutliner.cpp.

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

Referenced by alignOutputBlockWithAggFunc(), and fillOverallFunction().

## ◆ 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 371 of file IROutliner.cpp.

Referenced by OutlinableGroup::findSameConstants().

## ◆ constantMatches()

 static Optional 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 303 of file IROutliner.cpp.

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 1370 of file IROutliner.cpp.

Referenced by createSwitchStatement(), and fillOverallFunction().

## ◆ createSwitchStatement()

 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.

For the outlined section, determine if an outlined block already exists that matches the needed stores for the extracted section.

Parameters
 [in] M - The module we are outlining from. [in] OG - The group of regions to be outlined. [in] EndBBs - The final blocks of the extracted function. [in,out] OutputStoreBBs - The existing output blocks.

Definition at line 1396 of file IROutliner.cpp.

## ◆ fillOverallFunction()

 static void fillOverallFunction ( Module & M, OutlinableGroup & CurrentGroup, std::vector< DenseMap< Value *, BasicBlock * >> & OutputStoreBBs, std::vector< Function * > & FuncsToRemove )
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.

Definition at line 1476 of file IROutliner.cpp.

## ◆ 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 590 of file IROutliner.cpp.

Referenced by getCodeExtractorArguments().

## ◆ findCostForOutputBlocks()

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

Find the extra instructions needed to handle any output values for the region.

Parameters
 [in] M - The Module to outline from. [in] CurrentGroup - The collection of OutlinableRegions to analyze. [in] TTI - The TargetTransformInfo used to collect information for new instruction costs.
Returns
the additional cost to handle the outputs.

Definition at line 1749 of file IROutliner.cpp.

## ◆ findDuplicateOutputBlock()

 Optional 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 1212 of file IROutliner.cpp.

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 747 of file IROutliner.cpp.

## ◆ findExtractedOutputToOverallOutputMapping()

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

Create a mapping of the output arguments for the Region to the output arguments of the overall outlined function.

Parameters
 [in,out] Region - The region of code to be analyzed. [in] Outputs - The values found by the code extractor.

Definition at line 846 of file IROutliner.cpp.

## ◆ 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 676 of file IROutliner.cpp.

## ◆ 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 151 of file IROutliner.cpp.

References assert(), llvm::ConstantInt::getLimitedValue(), 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 441 of file IROutliner.cpp.

## ◆ 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 620 of file IROutliner.cpp.

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 137 of file IROutliner.cpp.

References llvm::BasicBlock::begin(), and llvm::BasicBlock::end().

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 528 of file IROutliner.cpp.

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 1569 of file IROutliner.cpp.

## ◆ 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 646 of file IROutliner.cpp.

Referenced by getCodeExtractorArguments().

## ◆ replaceArgumentUses()

 static void replaceArgumentUses ( OutlinableRegion & Region, DenseMap< Value *, BasicBlock * > & OutputBBs, bool FirstFunction = false )
static
Parameters
 [in] Region - The region of extracted code to be changed. [in,out] OutputBBs - The BasicBlock for the output stores for this region. [in] FirstFunction - A flag to indicate whether we are using this function to define the overall outlined function for all the regions, or if we are operating on one of the following regions.

Definition at line 1083 of file IROutliner.cpp.

Referenced by fillOverallFunction().

## ◆ 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 969 of file IROutliner.cpp.

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 1179 of file IROutliner.cpp.

Referenced by fillOverallFunction().

## ◆ DisableBranches

 cl::opt DisableBranches

static

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

## ◆ false

 IR false

Definition at line 2284 of file IROutliner.cpp.

## ◆ iroutliner

 iroutliner

Definition at line 2284 of file IROutliner.cpp.

## ◆ NoCostModel

 cl::opt 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 2284 of file IROutliner.cpp.