|
|
|||
File indexing completed on 2026-05-10 08:44:42
0001 //===- Transform/Utils/CodeExtractor.h - Code extraction util ---*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // A utility to support extracting code from one function into its own 0010 // stand-alone function. 0011 // 0012 //===----------------------------------------------------------------------===// 0013 0014 #ifndef LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H 0015 #define LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H 0016 0017 #include "llvm/ADT/ArrayRef.h" 0018 #include "llvm/ADT/DenseMap.h" 0019 #include "llvm/ADT/SetVector.h" 0020 #include <limits> 0021 0022 namespace llvm { 0023 0024 template <typename PtrType> class SmallPtrSetImpl; 0025 class AllocaInst; 0026 class BasicBlock; 0027 class BlockFrequency; 0028 class BlockFrequencyInfo; 0029 class BranchProbabilityInfo; 0030 class AssumptionCache; 0031 class CallInst; 0032 class DominatorTree; 0033 class Function; 0034 class Instruction; 0035 class Module; 0036 class Type; 0037 class Value; 0038 class StructType; 0039 0040 /// A cache for the CodeExtractor analysis. The operation \ref 0041 /// CodeExtractor::extractCodeRegion is guaranteed not to invalidate this 0042 /// object. This object should conservatively be considered invalid if any 0043 /// other mutating operations on the IR occur. 0044 /// 0045 /// Constructing this object is O(n) in the size of the function. 0046 class CodeExtractorAnalysisCache { 0047 /// The allocas in the function. 0048 SmallVector<AllocaInst *, 16> Allocas; 0049 0050 /// Base memory addresses of load/store instructions, grouped by block. 0051 DenseMap<BasicBlock *, DenseSet<Value *>> BaseMemAddrs; 0052 0053 /// Blocks which contain instructions which may have unknown side-effects 0054 /// on memory. 0055 DenseSet<BasicBlock *> SideEffectingBlocks; 0056 0057 void findSideEffectInfoForBlock(BasicBlock &BB); 0058 0059 public: 0060 CodeExtractorAnalysisCache(Function &F); 0061 0062 /// Get the allocas in the function at the time the analysis was created. 0063 /// Note that some of these allocas may no longer be present in the function, 0064 /// due to \ref CodeExtractor::extractCodeRegion. 0065 ArrayRef<AllocaInst *> getAllocas() const { return Allocas; } 0066 0067 /// Check whether \p BB contains an instruction thought to load from, store 0068 /// to, or otherwise clobber the alloca \p Addr. 0069 bool doesBlockContainClobberOfAddr(BasicBlock &BB, AllocaInst *Addr) const; 0070 }; 0071 0072 /// Utility class for extracting code into a new function. 0073 /// 0074 /// This utility provides a simple interface for extracting some sequence of 0075 /// code into its own function, replacing it with a call to that function. It 0076 /// also provides various methods to query about the nature and result of 0077 /// such a transformation. 0078 /// 0079 /// The rough algorithm used is: 0080 /// 1) Find both the inputs and outputs for the extracted region. 0081 /// 2) Pass the inputs as arguments, remapping them within the extracted 0082 /// function to arguments. 0083 /// 3) Add allocas for any scalar outputs, adding all of the outputs' allocas 0084 /// as arguments, and inserting stores to the arguments for any scalars. 0085 class CodeExtractor { 0086 using ValueSet = SetVector<Value *>; 0087 0088 // Various bits of state computed on construction. 0089 DominatorTree *const DT; 0090 const bool AggregateArgs; 0091 BlockFrequencyInfo *BFI; 0092 BranchProbabilityInfo *BPI; 0093 AssumptionCache *AC; 0094 0095 // A block outside of the extraction set where any intermediate 0096 // allocations will be placed inside. If this is null, allocations 0097 // will be placed in the entry block of the function. 0098 BasicBlock *AllocationBlock; 0099 0100 // If true, varargs functions can be extracted. 0101 bool AllowVarArgs; 0102 0103 // Bits of intermediate state computed at various phases of extraction. 0104 SetVector<BasicBlock *> Blocks; 0105 0106 /// Lists of blocks that are branched from the code region to be extracted, 0107 /// also called the exit blocks. Each block is contained at most once. Its 0108 /// order defines the return value of the extracted function. 0109 /// 0110 /// When there is just one (or no) exit block, the return value is 0111 /// irrelevant. 0112 /// 0113 /// When there are exactly two exit blocks, the extracted function returns a 0114 /// boolean. For ExtractedFuncRetVals[0], it returns 'true'. For 0115 /// ExtractedFuncRetVals[1] it returns 'false'. 0116 /// NOTE: Since a boolean is represented by i1, ExtractedFuncRetVals[0] 0117 /// returns 1 and ExtractedFuncRetVals[1] returns 0, which opposite 0118 /// of the regular pattern below. 0119 /// 0120 /// When there are 3 or more exit blocks, leaving the extracted function via 0121 /// the first block it returns 0. When leaving via the second entry it 0122 /// returns 1, etc. 0123 SmallVector<BasicBlock *> ExtractedFuncRetVals; 0124 0125 // Suffix to use when creating extracted function (appended to the original 0126 // function name + "."). If empty, the default is to use the entry block 0127 // label, if non-empty, otherwise "extracted". 0128 std::string Suffix; 0129 0130 // If true, the outlined function has aggregate argument in zero address 0131 // space. 0132 bool ArgsInZeroAddressSpace; 0133 0134 public: 0135 /// Create a code extractor for a sequence of blocks. 0136 /// 0137 /// Given a sequence of basic blocks where the first block in the sequence 0138 /// dominates the rest, prepare a code extractor object for pulling this 0139 /// sequence out into its new function. When a DominatorTree is also given, 0140 /// extra checking and transformations are enabled. If AllowVarArgs is true, 0141 /// vararg functions can be extracted. This is safe, if all vararg handling 0142 /// code is extracted, including vastart. If AllowAlloca is true, then 0143 /// extraction of blocks containing alloca instructions would be possible, 0144 /// however code extractor won't validate whether extraction is legal. 0145 /// Any new allocations will be placed in the AllocationBlock, unless 0146 /// it is null, in which case it will be placed in the entry block of 0147 /// the function from which the code is being extracted. 0148 /// If ArgsInZeroAddressSpace param is set to true, then the aggregate 0149 /// param pointer of the outlined function is declared in zero address 0150 /// space. 0151 CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT = nullptr, 0152 bool AggregateArgs = false, BlockFrequencyInfo *BFI = nullptr, 0153 BranchProbabilityInfo *BPI = nullptr, 0154 AssumptionCache *AC = nullptr, bool AllowVarArgs = false, 0155 bool AllowAlloca = false, 0156 BasicBlock *AllocationBlock = nullptr, 0157 std::string Suffix = "", bool ArgsInZeroAddressSpace = false); 0158 0159 /// Perform the extraction, returning the new function. 0160 /// 0161 /// Returns zero when called on a CodeExtractor instance where isEligible 0162 /// returns false. 0163 Function *extractCodeRegion(const CodeExtractorAnalysisCache &CEAC); 0164 0165 /// Perform the extraction, returning the new function and providing an 0166 /// interface to see what was categorized as inputs and outputs. 0167 /// 0168 /// \param CEAC - Cache to speed up operations for the CodeExtractor when 0169 /// hoisting, and extracting lifetime values and assumes. 0170 /// \param Inputs [out] - filled with values marked as inputs to the 0171 /// newly outlined function. 0172 /// \param Outputs [out] - filled with values marked as outputs to the 0173 /// newly outlined function. 0174 /// \returns zero when called on a CodeExtractor instance where isEligible 0175 /// returns false. 0176 Function *extractCodeRegion(const CodeExtractorAnalysisCache &CEAC, 0177 ValueSet &Inputs, ValueSet &Outputs); 0178 0179 /// Verify that assumption cache isn't stale after a region is extracted. 0180 /// Returns true when verifier finds errors. AssumptionCache is passed as 0181 /// parameter to make this function stateless. 0182 static bool verifyAssumptionCache(const Function &OldFunc, 0183 const Function &NewFunc, 0184 AssumptionCache *AC); 0185 0186 /// Test whether this code extractor is eligible. 0187 /// 0188 /// Based on the blocks used when constructing the code extractor, 0189 /// determine whether it is eligible for extraction. 0190 /// 0191 /// Checks that varargs handling (with vastart and vaend) is only done in 0192 /// the outlined blocks. 0193 bool isEligible() const; 0194 0195 /// Compute the set of input values and output values for the code. 0196 /// 0197 /// These can be used either when performing the extraction or to evaluate 0198 /// the expected size of a call to the extracted function. Note that this 0199 /// work cannot be cached between the two as once we decide to extract 0200 /// a code sequence, that sequence is modified, including changing these 0201 /// sets, before extraction occurs. These modifications won't have any 0202 /// significant impact on the cost however. 0203 void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, 0204 const ValueSet &Allocas, 0205 bool CollectGlobalInputs = false) const; 0206 0207 /// Check if life time marker nodes can be hoisted/sunk into the outline 0208 /// region. 0209 /// 0210 /// Returns true if it is safe to do the code motion. 0211 bool 0212 isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, 0213 Instruction *AllocaAddr) const; 0214 0215 /// Find the set of allocas whose life ranges are contained within the 0216 /// outlined region. 0217 /// 0218 /// Allocas which have life_time markers contained in the outlined region 0219 /// should be pushed to the outlined function. The address bitcasts that 0220 /// are used by the lifetime markers are also candidates for shrink- 0221 /// wrapping. The instructions that need to be sunk are collected in 0222 /// 'Allocas'. 0223 void findAllocas(const CodeExtractorAnalysisCache &CEAC, 0224 ValueSet &SinkCands, ValueSet &HoistCands, 0225 BasicBlock *&ExitBlock) const; 0226 0227 /// Find or create a block within the outline region for placing hoisted 0228 /// code. 0229 /// 0230 /// CommonExitBlock is block outside the outline region. It is the common 0231 /// successor of blocks inside the region. If there exists a single block 0232 /// inside the region that is the predecessor of CommonExitBlock, that block 0233 /// will be returned. Otherwise CommonExitBlock will be split and the 0234 /// original block will be added to the outline region. 0235 BasicBlock *findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock); 0236 0237 /// Exclude a value from aggregate argument passing when extracting a code 0238 /// region, passing it instead as a scalar. 0239 void excludeArgFromAggregate(Value *Arg); 0240 0241 private: 0242 struct LifetimeMarkerInfo { 0243 bool SinkLifeStart = false; 0244 bool HoistLifeEnd = false; 0245 Instruction *LifeStart = nullptr; 0246 Instruction *LifeEnd = nullptr; 0247 }; 0248 0249 ValueSet ExcludeArgsFromAggregate; 0250 0251 LifetimeMarkerInfo 0252 getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, 0253 Instruction *Addr, BasicBlock *ExitBlock) const; 0254 0255 /// Updates the list of SwitchCases (corresponding to exit blocks) after 0256 /// changes of the control flow or the Blocks list. 0257 void computeExtractedFuncRetVals(); 0258 0259 /// Return the type used for the return code of the extracted function to 0260 /// indicate which exit block to jump to. 0261 Type *getSwitchType(); 0262 0263 void severSplitPHINodesOfEntry(BasicBlock *&Header); 0264 void severSplitPHINodesOfExits(); 0265 void splitReturnBlocks(); 0266 0267 void moveCodeToFunction(Function *newFunction); 0268 0269 void calculateNewCallTerminatorWeights( 0270 BasicBlock *CodeReplacer, 0271 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights, 0272 BranchProbabilityInfo *BPI); 0273 0274 /// Normalizes the control flow of the extracted regions, such as ensuring 0275 /// that the extracted region does not contain a return instruction. 0276 void normalizeCFGForExtraction(BasicBlock *&header); 0277 0278 /// Generates the function declaration for the function containing the 0279 /// extracted code. 0280 Function *constructFunctionDeclaration(const ValueSet &inputs, 0281 const ValueSet &outputs, 0282 BlockFrequency EntryFreq, 0283 const Twine &Name, 0284 ValueSet &StructValues, 0285 StructType *&StructTy); 0286 0287 /// Generates the code for the extracted function. That is: a prolog, the 0288 /// moved or copied code from the original function, and epilogs for each 0289 /// exit. 0290 void emitFunctionBody(const ValueSet &inputs, const ValueSet &outputs, 0291 const ValueSet &StructValues, Function *newFunction, 0292 StructType *StructArgTy, BasicBlock *header, 0293 const ValueSet &SinkingCands); 0294 0295 /// Generates a Basic Block that calls the extracted function. 0296 CallInst *emitReplacerCall(const ValueSet &inputs, const ValueSet &outputs, 0297 const ValueSet &StructValues, 0298 Function *newFunction, StructType *StructArgTy, 0299 Function *oldFunction, BasicBlock *ReplIP, 0300 BlockFrequency EntryFreq, 0301 ArrayRef<Value *> LifetimesStart, 0302 std::vector<Value *> &Reloads); 0303 0304 /// Connects the basic block containing the call to the extracted function 0305 /// into the original function's control flow. 0306 void insertReplacerCall( 0307 Function *oldFunction, BasicBlock *header, BasicBlock *codeReplacer, 0308 const ValueSet &outputs, ArrayRef<Value *> Reloads, 0309 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights); 0310 }; 0311 0312 } // end namespace llvm 0313 0314 #endif // LLVM_TRANSFORMS_UTILS_CODEEXTRACTOR_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|