LLVM  13.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/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 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 BasicBlockmoveFunctionData (Function &Old, Function &New)
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, ArrayRef< 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, BasicBlock *OutputBB)

void replaceConstants (OutlinableRegion &Region)
Within an extracted function, replace the constants that need to be lifted into arguments with the actual argument. More...

static std::vector< Instruction * > collectRelevantInstructions (Function &F, DenseSet< BasicBlock * > &ExcludeBlocks)
For the given function, find all the nondebug or lifetime instructions, and return them as a vector. More...

Optional< unsigned > findDuplicateOutputBlock (BasicBlock *OutputBB, ArrayRef< BasicBlock * > OutputStoreBBs)
It is possible that there is a basic block that already performs the same stores. More...

static void alignOutputBlockWithAggFunc (OutlinableGroup &OG, OutlinableRegion &Region, BasicBlock *OutputBB, BasicBlock *EndBB, const DenseMap< Value *, Value * > &OutputMappings, std::vector< BasicBlock * > &OutputStoreBBs)
For the outlined section, move needed the StoreInsts for the output registers into their own block. More...

void createSwitchStatement (Module &M, OutlinableGroup &OG, BasicBlock *EndBB, ArrayRef< 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< 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 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 > 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 28 of file IROutliner.cpp.

## ◆ alignOutputBlockWithAggFunc()

 static void alignOutputBlockWithAggFunc ( OutlinableGroup & OG, OutlinableRegion & Region, BasicBlock * OutputBB, BasicBlock * EndBB, const DenseMap< Value *, Value * > & OutputMappings, std::vector< 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] OutputBB - the block that stores for this region will be placed in. [in] EndBB - the final block 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 997 of file IROutliner.cpp.

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

Referenced by OutlinableGroup::findSameConstants().

## ◆ collectRelevantInstructions()

 static std::vector collectRelevantInstructions ( Function & F, DenseSet< BasicBlock * > & ExcludeBlocks )
static

For the given function, find all the nondebug or lifetime instructions, and return them as a vector.

Exclude any blocks in ExludeBlocks.

Parameters
 [in] F - The function we collect the instructions from. [in] ExcludeBlocks - BasicBlocks to ignore.
Returns
the list of instructions extracted.

Definition at line 920 of file IROutliner.cpp.

Referenced by alignOutputBlockWithAggFunc().

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

Referenced by collectRegionsConstants().

## ◆ createSwitchStatement()

 void createSwitchStatement ( Module & M, OutlinableGroup & OG, BasicBlock * EndBB, ArrayRef< 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] EndBB - The final block of the extracted function. [in,out] OutputStoreBBs - The existing output blocks.

Definition at line 1084 of file IROutliner.cpp.

## ◆ fillOverallFunction()

 static void fillOverallFunction ( Module & M, OutlinableGroup & CurrentGroup, std::vector< 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 1140 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 412 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 1335 of file IROutliner.cpp.

## ◆ findDuplicateOutputBlock()

 Optional findDuplicateOutputBlock ( BasicBlock * OutputBB, ArrayRef< 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
 OutputBB [in] the block 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 948 of file IROutliner.cpp.

References llvm::BasicBlock::begin(), 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 569 of file IROutliner.cpp.

## ◆ findExtractedOutputToOverallOutputMapping()

 static void findExtractedOutputToOverallOutputMapping ( OutlinableRegion & Region, ArrayRef< 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 640 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 498 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 442 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 117 of file IROutliner.cpp.

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

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

## ◆ moveFunctionData()

 static BasicBlock* moveFunctionData ( Function & Old, Function & New )
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.
Returns
the first return block for the function in New.

Definition at line 385 of file IROutliner.cpp.

References assert(), llvm::Function::begin(), llvm::Function::end(), and I.

Referenced by fillOverallFunction().

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

Referenced by getCodeExtractorArguments().

## ◆ replaceArgumentUses()

 static void replaceArgumentUses ( OutlinableRegion & Region, BasicBlock * OutputBB )
static
Parameters
 [in] Region - The region of extracted code to be changed. [in,out] OutputBB - The BasicBlock for the output stores for this region.

Definition at line 838 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 732 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 886 of file IROutliner.cpp.

Referenced by fillOverallFunction().

## Variable Documentation

static

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

## ◆ false

 IR false

Definition at line 1758 of file IROutliner.cpp.

## ◆ iroutliner

 iroutliner

Definition at line 1758 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 1758 of file IROutliner.cpp.