Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
CloneFunction.cpp
Go to the documentation of this file.
1//===- CloneFunction.cpp - Clone a function into another function ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the CloneFunctionInto interface, which is used as the
10// low-level function cloner. This is used by the CloneFunction and function
11// inliner to do the dirty work of copying the body of a function around.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/Analysis/ConstantFolding.h"
17#include "llvm/Analysis/DomTreeUpdater.h"
18#include "llvm/Analysis/InstructionSimplify.h"
19#include "llvm/Analysis/LoopInfo.h"
20#include "llvm/IR/AttributeMask.h"
21#include "llvm/IR/CFG.h"
22#include "llvm/IR/Constants.h"
23#include "llvm/IR/DebugInfo.h"
24#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Function.h"
26#include "llvm/IR/InstIterator.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/MDBuilder.h"
31#include "llvm/IR/Metadata.h"
32#include "llvm/IR/Module.h"
33#include "llvm/Transforms/Utils/BasicBlockUtils.h"
34#include "llvm/Transforms/Utils/Cloning.h"
35#include "llvm/Transforms/Utils/Local.h"
36#include "llvm/Transforms/Utils/ValueMapper.h"
37#include <map>
38#include <optional>
39using namespacellvm;
40
41#define DEBUG_TYPE "clone-function"
42
43/// See comments in Cloning.h.
44BasicBlock *llvm::CloneBasicBlock(constBasicBlock *BB,ValueToValueMapTy &VMap,
45constTwine &NameSuffix,Function *F,
46ClonedCodeInfo *CodeInfo) {
47BasicBlock *NewBB =BasicBlock::Create(BB->getContext(),"",F);
48 NewBB->IsNewDbgInfoFormat = BB->IsNewDbgInfoFormat;
49if (BB->hasName())
50 NewBB->setName(BB->getName() + NameSuffix);
51
52bool hasCalls =false, hasDynamicAllocas =false, hasMemProfMetadata =false;
53
54// Loop over all instructions, and copy them over.
55for (constInstruction &I : *BB) {
56Instruction *NewInst =I.clone();
57if (I.hasName())
58 NewInst->setName(I.getName() + NameSuffix);
59
60 NewInst->insertBefore(*NewBB, NewBB->end());
61 NewInst->cloneDebugInfoFrom(&I);
62
63 VMap[&I] = NewInst;// Add instruction map to value.
64
65if (isa<CallInst>(I) && !I.isDebugOrPseudoInst()) {
66 hasCalls =true;
67 hasMemProfMetadata |=I.hasMetadata(LLVMContext::MD_memprof);
68 hasMemProfMetadata |=I.hasMetadata(LLVMContext::MD_callsite);
69 }
70if (constAllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
71if (!AI->isStaticAlloca()) {
72 hasDynamicAllocas =true;
73 }
74 }
75 }
76
77if (CodeInfo) {
78 CodeInfo->ContainsCalls |= hasCalls;
79 CodeInfo->ContainsMemProfMetadata |= hasMemProfMetadata;
80 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
81 }
82return NewBB;
83}
84
85voidllvm::CloneFunctionAttributesInto(Function *NewFunc,
86constFunction *OldFunc,
87ValueToValueMapTy &VMap,
88bool ModuleLevelChanges,
89ValueMapTypeRemapper *TypeMapper,
90ValueMaterializer *Materializer) {
91// Copy all attributes other than those stored in Function's AttributeList
92// which holds e.g. parameters and return value attributes.
93AttributeList NewAttrs = NewFunc->getAttributes();
94 NewFunc->copyAttributesFrom(OldFunc);
95 NewFunc->setAttributes(NewAttrs);
96
97constRemapFlags FuncGlobalRefFlags =
98 ModuleLevelChanges ?RF_None :RF_NoModuleLevelChanges;
99
100// Fix up the personality function that got copied over.
101if (OldFunc->hasPersonalityFn())
102 NewFunc->setPersonalityFn(MapValue(OldFunc->getPersonalityFn(), VMap,
103 FuncGlobalRefFlags, TypeMapper,
104 Materializer));
105
106if (OldFunc->hasPrefixData()) {
107 NewFunc->setPrefixData(MapValue(OldFunc->getPrefixData(), VMap,
108 FuncGlobalRefFlags, TypeMapper,
109 Materializer));
110 }
111
112if (OldFunc->hasPrologueData()) {
113 NewFunc->setPrologueData(MapValue(OldFunc->getPrologueData(), VMap,
114 FuncGlobalRefFlags, TypeMapper,
115 Materializer));
116 }
117
118SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size());
119AttributeList OldAttrs = OldFunc->getAttributes();
120
121// Clone any argument attributes that are present in the VMap.
122for (constArgument &OldArg : OldFunc->args()) {
123if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
124// Remap the parameter indices.
125 NewArgAttrs[NewArg->getArgNo()] =
126 OldAttrs.getParamAttrs(OldArg.getArgNo());
127 }
128 }
129
130 NewFunc->setAttributes(
131AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttrs(),
132 OldAttrs.getRetAttrs(), NewArgAttrs));
133}
134
135DISubprogram *llvm::CollectDebugInfoForCloning(constFunction &F,
136CloneFunctionChangeType Changes,
137DebugInfoFinder &DIFinder) {
138DISubprogram *SPClonedWithinModule =nullptr;
139if (Changes < CloneFunctionChangeType::DifferentModule) {
140 SPClonedWithinModule =F.getSubprogram();
141 }
142if (SPClonedWithinModule)
143 DIFinder.processSubprogram(SPClonedWithinModule);
144
145constModule *M =F.getParent();
146if (Changes != CloneFunctionChangeType::ClonedModule && M) {
147// Inspect instructions to process e.g. DILexicalBlocks of inlined functions
148for (constauto &I :instructions(F))
149 DIFinder.processInstruction(*M,I);
150 }
151
152return SPClonedWithinModule;
153}
154
155MetadataSetTy
156llvm::FindDebugInfoToIdentityMap(CloneFunctionChangeType Changes,
157DebugInfoFinder &DIFinder,
158DISubprogram *SPClonedWithinModule) {
159MetadataSetTy MD;
160
161if (Changes < CloneFunctionChangeType::DifferentModule &&
162 DIFinder.subprogram_count() > 0) {
163// Avoid cloning types, compile units, and (other) subprograms.
164for (DISubprogram *ISP : DIFinder.subprograms()) {
165if (ISP != SPClonedWithinModule)
166 MD.insert(ISP);
167 }
168
169// If a subprogram isn't going to be cloned skip its lexical blocks as well.
170for (DIScope *S : DIFinder.scopes()) {
171auto *LScope = dyn_cast<DILocalScope>(S);
172if (LScope && LScope->getSubprogram() != SPClonedWithinModule)
173 MD.insert(S);
174 }
175
176for (DICompileUnit *CU : DIFinder.compile_units())
177 MD.insert(CU);
178
179for (DIType *Type : DIFinder.types())
180 MD.insert(Type);
181 }else {
182assert(!SPClonedWithinModule &&
183"Subprogram should be in DIFinder->subprogram_count()...");
184 }
185
186return MD;
187}
188
189voidllvm::CloneFunctionMetadataInto(Function &NewFunc,constFunction &OldFunc,
190ValueToValueMapTy &VMap,
191RemapFlags RemapFlag,
192ValueMapTypeRemapper *TypeMapper,
193ValueMaterializer *Materializer,
194constMetadataSetTy *IdentityMD) {
195SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
196 OldFunc.getAllMetadata(MDs);
197for (auto MD : MDs) {
198 NewFunc.addMetadata(MD.first,
199 *MapMetadata(MD.second, VMap, RemapFlag, TypeMapper,
200 Materializer, IdentityMD));
201 }
202}
203
204voidllvm::CloneFunctionBodyInto(Function &NewFunc,constFunction &OldFunc,
205ValueToValueMapTy &VMap,RemapFlags RemapFlag,
206SmallVectorImpl<ReturnInst *> &Returns,
207constchar *NameSuffix,
208ClonedCodeInfo *CodeInfo,
209ValueMapTypeRemapper *TypeMapper,
210ValueMaterializer *Materializer,
211constMetadataSetTy *IdentityMD) {
212if (OldFunc.isDeclaration())
213return;
214
215// Loop over all of the basic blocks in the function, cloning them as
216// appropriate. Note that we save BE this way in order to handle cloning of
217// recursive functions into themselves.
218for (constBasicBlock &BB : OldFunc) {
219
220// Create a new basic block and copy instructions into it!
221BasicBlock *CBB =
222CloneBasicBlock(&BB, VMap, NameSuffix, &NewFunc, CodeInfo);
223
224// Add basic block mapping.
225 VMap[&BB] = CBB;
226
227// It is only legal to clone a function if a block address within that
228// function is never referenced outside of the function. Given that, we
229// want to map block addresses from the old function to block addresses in
230// the clone. (This is different from the generic ValueMapper
231// implementation, which generates an invalid blockaddress when
232// cloning a function.)
233if (BB.hasAddressTaken()) {
234Constant *OldBBAddr =BlockAddress::get(const_cast<Function *>(&OldFunc),
235const_cast<BasicBlock *>(&BB));
236 VMap[OldBBAddr] =BlockAddress::get(&NewFunc, CBB);
237 }
238
239// Note return instructions for the caller.
240if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
241 Returns.push_back(RI);
242 }
243
244// Loop over all of the instructions in the new function, fixing up operand
245// references as we go. This uses VMap to do all the hard work.
246for (Function::iterator
247 BB = cast<BasicBlock>(VMap[&OldFunc.front()])->getIterator(),
248 BE = NewFunc.end();
249 BB != BE; ++BB)
250// Loop over all instructions, fixing each one as we find it, and any
251// attached debug-info records.
252for (Instruction &II : *BB) {
253RemapInstruction(&II, VMap, RemapFlag, TypeMapper, Materializer,
254 IdentityMD);
255RemapDbgRecordRange(II.getModule(),II.getDbgRecordRange(), VMap,
256 RemapFlag, TypeMapper, Materializer, IdentityMD);
257 }
258}
259
260// Clone OldFunc into NewFunc, transforming the old arguments into references to
261// VMap values.
262voidllvm::CloneFunctionInto(Function *NewFunc,constFunction *OldFunc,
263ValueToValueMapTy &VMap,
264CloneFunctionChangeType Changes,
265SmallVectorImpl<ReturnInst *> &Returns,
266constchar *NameSuffix,ClonedCodeInfo *CodeInfo,
267ValueMapTypeRemapper *TypeMapper,
268ValueMaterializer *Materializer) {
269 NewFunc->setIsNewDbgInfoFormat(OldFunc->IsNewDbgInfoFormat);
270assert(NameSuffix &&"NameSuffix cannot be null!");
271
272#ifndef NDEBUG
273for (constArgument &I : OldFunc->args())
274assert(VMap.count(&I) &&"No mapping from source argument specified!");
275#endif
276
277bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly;
278
279CloneFunctionAttributesInto(NewFunc, OldFunc, VMap, ModuleLevelChanges,
280 TypeMapper, Materializer);
281
282// Everything else beyond this point deals with function instructions,
283// so if we are dealing with a function declaration, we're done.
284if (OldFunc->isDeclaration())
285return;
286
287// When we remap instructions within the same module, we want to avoid
288// duplicating inlined DISubprograms, so record all subprograms we find as we
289// duplicate instructions and then freeze them in the MD map. We also record
290// information about dbg.value and dbg.declare to avoid duplicating the
291// types.
292DebugInfoFinder DIFinder;
293
294// Track the subprogram attachment that needs to be cloned to fine-tune the
295// mapping within the same module.
296if (Changes < CloneFunctionChangeType::DifferentModule) {
297// Need to find subprograms, types, and compile units.
298
299assert((NewFunc->getParent() ==nullptr ||
300 NewFunc->getParent() == OldFunc->getParent()) &&
301"Expected NewFunc to have the same parent, or no parent");
302 }else {
303// Need to find all the compile units.
304
305assert((NewFunc->getParent() ==nullptr ||
306 NewFunc->getParent() != OldFunc->getParent()) &&
307"Expected NewFunc to have different parents, or no parent");
308
309if (Changes == CloneFunctionChangeType::DifferentModule) {
310assert(NewFunc->getParent() &&
311"Need parent of new function to maintain debug info invariants");
312 }
313 }
314
315DISubprogram *SPClonedWithinModule =
316CollectDebugInfoForCloning(*OldFunc, Changes, DIFinder);
317
318MetadataSetTy IdentityMD =
319FindDebugInfoToIdentityMap(Changes, DIFinder, SPClonedWithinModule);
320
321// Cloning is always a Module level operation, since Metadata needs to be
322// cloned.
323constauto RemapFlag =RF_None;
324
325CloneFunctionMetadataInto(*NewFunc, *OldFunc, VMap, RemapFlag, TypeMapper,
326 Materializer, &IdentityMD);
327
328CloneFunctionBodyInto(*NewFunc, *OldFunc, VMap, RemapFlag, Returns,
329 NameSuffix, CodeInfo, TypeMapper, Materializer,
330 &IdentityMD);
331
332// Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the
333// same module, the compile unit will already be listed (or not). When
334// cloning a module, CloneModule() will handle creating the named metadata.
335if (Changes != CloneFunctionChangeType::DifferentModule)
336return;
337
338// Update !llvm.dbg.cu with compile units added to the new module if this
339// function is being cloned in isolation.
340//
341// FIXME: This is making global / module-level changes, which doesn't seem
342// like the right encapsulation Consider dropping the requirement to update
343// !llvm.dbg.cu (either obsoleting the node, or restricting it to
344// non-discardable compile units) instead of discovering compile units by
345// visiting the metadata attached to global values, which would allow this
346// code to be deleted. Alternatively, perhaps give responsibility for this
347// update to CloneFunctionInto's callers.
348auto *NewModule = NewFunc->getParent();
349auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
350// Avoid multiple insertions of the same DICompileUnit to NMD.
351SmallPtrSet<const void *, 8> Visited;
352for (auto *Operand : NMD->operands())
353 Visited.insert(Operand);
354for (auto *Unit : DIFinder.compile_units()) {
355MDNode *MappedUnit =
356MapMetadata(Unit, VMap,RF_None, TypeMapper, Materializer);
357if (Visited.insert(MappedUnit).second)
358 NMD->addOperand(MappedUnit);
359 }
360}
361
362/// Return a copy of the specified function and add it to that function's
363/// module. Also, any references specified in the VMap are changed to refer to
364/// their mapped value instead of the original one. If any of the arguments to
365/// the function are in the VMap, the arguments are deleted from the resultant
366/// function. The VMap is updated to include mappings from all of the
367/// instructions and basicblocks in the function from their old to new values.
368///
369Function *llvm::CloneFunction(Function *F,ValueToValueMapTy &VMap,
370ClonedCodeInfo *CodeInfo) {
371 std::vector<Type *> ArgTypes;
372
373// The user might be deleting arguments to the function by specifying them in
374// the VMap. If so, we need to not add the arguments to the arg ty vector
375//
376for (constArgument &I :F->args())
377if (VMap.count(&I) == 0)// Haven't mapped the argument to anything yet?
378 ArgTypes.push_back(I.getType());
379
380// Create a new function type...
381FunctionType *FTy =
382 FunctionType::get(F->getFunctionType()->getReturnType(), ArgTypes,
383F->getFunctionType()->isVarArg());
384
385// Create the new function...
386Function *NewF =Function::Create(FTy,F->getLinkage(),F->getAddressSpace(),
387F->getName(),F->getParent());
388 NewF->setIsNewDbgInfoFormat(F->IsNewDbgInfoFormat);
389
390// Loop over the arguments, copying the names of the mapped arguments over...
391Function::arg_iterator DestI = NewF->arg_begin();
392for (constArgument &I :F->args())
393if (VMap.count(&I) == 0) {// Is this argument preserved?
394 DestI->setName(I.getName());// Copy the name over...
395 VMap[&I] = &*DestI++;// Add mapping to VMap
396 }
397
398SmallVector<ReturnInst *, 8> Returns;// Ignore returns cloned.
399CloneFunctionInto(NewF,F, VMap, CloneFunctionChangeType::LocalChangesOnly,
400 Returns,"", CodeInfo);
401
402return NewF;
403}
404
405namespace{
406/// This is a private class used to implement CloneAndPruneFunctionInto.
407structPruningFunctionCloner {
408Function *NewFunc;
409constFunction *OldFunc;
410ValueToValueMapTy &VMap;
411bool ModuleLevelChanges;
412constchar *NameSuffix;
413ClonedCodeInfo *CodeInfo;
414bool HostFuncIsStrictFP;
415
416Instruction *cloneInstruction(BasicBlock::const_iteratorII);
417
418public:
419 PruningFunctionCloner(Function *newFunc,constFunction *oldFunc,
420ValueToValueMapTy &valueMap,bool moduleLevelChanges,
421constchar *nameSuffix,ClonedCodeInfo *codeInfo)
422 : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
423 ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
424 CodeInfo(codeInfo) {
425 HostFuncIsStrictFP =
426 newFunc->getAttributes().hasFnAttr(Attribute::StrictFP);
427 }
428
429 /// The specified block is found to be reachable, clone it and
430 /// anything that it can reach.
431void CloneBlock(constBasicBlock *BB,BasicBlock::const_iterator StartingInst,
432 std::vector<const BasicBlock *> &ToClone);
433};
434}// namespace
435
436Instruction *
437PruningFunctionCloner::cloneInstruction(BasicBlock::const_iteratorII) {
438constInstruction &OldInst = *II;
439Instruction *NewInst =nullptr;
440if (HostFuncIsStrictFP) {
441Intrinsic::ID CIID =getConstrainedIntrinsicID(OldInst);
442if (CIID !=Intrinsic::not_intrinsic) {
443// Instead of cloning the instruction, a call to constrained intrinsic
444// should be created.
445// Assume the first arguments of constrained intrinsics are the same as
446// the operands of original instruction.
447
448// Determine overloaded types of the intrinsic.
449SmallVector<Type *, 2> TParams;
450SmallVector<Intrinsic::IITDescriptor, 8> Descriptor;
451getIntrinsicInfoTableEntries(CIID, Descriptor);
452for (unsignedI = 0, E = Descriptor.size();I != E; ++I) {
453Intrinsic::IITDescriptor Operand = Descriptor[I];
454switch (Operand.Kind) {
455caseIntrinsic::IITDescriptor::Argument:
456if (Operand.getArgumentKind() !=
457 Intrinsic::IITDescriptor::AK_MatchType) {
458if (I == 0)
459 TParams.push_back(OldInst.getType());
460else
461 TParams.push_back(OldInst.getOperand(I - 1)->getType());
462 }
463break;
464caseIntrinsic::IITDescriptor::SameVecWidthArgument:
465 ++I;
466break;
467default:
468break;
469 }
470 }
471
472// Create intrinsic call.
473LLVMContext &Ctx = NewFunc->getContext();
474Function *IFn =Intrinsic::getOrInsertDeclaration(NewFunc->getParent(),
475 CIID, TParams);
476SmallVector<Value *, 4>Args;
477unsigned NumOperands = OldInst.getNumOperands();
478if (isa<CallInst>(OldInst))
479 --NumOperands;
480for (unsignedI = 0;I < NumOperands; ++I) {
481Value *Op = OldInst.getOperand(I);
482Args.push_back(Op);
483 }
484if (constauto *CmpI = dyn_cast<FCmpInst>(&OldInst)) {
485FCmpInst::Predicate Pred = CmpI->getPredicate();
486StringRef PredName = FCmpInst::getPredicateName(Pred);
487Args.push_back(MetadataAsValue::get(Ctx,MDString::get(Ctx, PredName)));
488 }
489
490// The last arguments of a constrained intrinsic are metadata that
491// represent rounding mode (absents in some intrinsics) and exception
492// behavior. The inlined function uses default settings.
493if (Intrinsic::hasConstrainedFPRoundingModeOperand(CIID))
494Args.push_back(
495MetadataAsValue::get(Ctx,MDString::get(Ctx,"round.tonearest")));
496Args.push_back(
497MetadataAsValue::get(Ctx,MDString::get(Ctx,"fpexcept.ignore")));
498
499 NewInst =CallInst::Create(IFn, Args, OldInst.getName() +".strict");
500 }
501 }
502if (!NewInst)
503 NewInst =II->clone();
504return NewInst;
505}
506
507/// The specified block is found to be reachable, clone it and
508/// anything that it can reach.
509void PruningFunctionCloner::CloneBlock(
510constBasicBlock *BB,BasicBlock::const_iterator StartingInst,
511 std::vector<const BasicBlock *> &ToClone) {
512WeakTrackingVH &BBEntry = VMap[BB];
513
514// Have we already cloned this block?
515if (BBEntry)
516return;
517
518// Nope, clone it now.
519BasicBlock *NewBB;
520Twine NewName(BB->hasName() ?Twine(BB->getName()) + NameSuffix :"");
521 BBEntry = NewBB =BasicBlock::Create(BB->getContext(), NewName, NewFunc);
522 NewBB->IsNewDbgInfoFormat = BB->IsNewDbgInfoFormat;
523
524// It is only legal to clone a function if a block address within that
525// function is never referenced outside of the function. Given that, we
526// want to map block addresses from the old function to block addresses in
527// the clone. (This is different from the generic ValueMapper
528// implementation, which generates an invalid blockaddress when
529// cloning a function.)
530//
531// Note that we don't need to fix the mapping for unreachable blocks;
532// the default mapping there is safe.
533if (BB->hasAddressTaken()) {
534Constant *OldBBAddr =BlockAddress::get(const_cast<Function *>(OldFunc),
535const_cast<BasicBlock *>(BB));
536 VMap[OldBBAddr] =BlockAddress::get(NewFunc, NewBB);
537 }
538
539bool hasCalls =false, hasDynamicAllocas =false, hasStaticAllocas =false;
540bool hasMemProfMetadata =false;
541
542// Keep a cursor pointing at the last place we cloned debug-info records from.
543BasicBlock::const_iterator DbgCursor = StartingInst;
544auto CloneDbgRecordsToHere =
545 [NewBB, &DbgCursor](Instruction *NewInst,BasicBlock::const_iteratorII) {
546if (!NewBB->IsNewDbgInfoFormat)
547return;
548
549// Clone debug-info records onto this instruction. Iterate through any
550// source-instructions we've cloned and then subsequently optimised
551// away, so that their debug-info doesn't go missing.
552for (; DbgCursor !=II; ++DbgCursor)
553 NewInst->cloneDebugInfoFrom(&*DbgCursor, std::nullopt,false);
554 NewInst->cloneDebugInfoFrom(&*II);
555 DbgCursor = std::next(II);
556 };
557
558// Loop over all instructions, and copy them over, DCE'ing as we go. This
559// loop doesn't include the terminator.
560for (BasicBlock::const_iteratorII = StartingInst, IE = --BB->end();II != IE;
561 ++II) {
562
563// Don't clone fake_use as it may suppress many optimizations
564// due to inlining, especially SROA.
565if (auto *IntrInst = dyn_cast<IntrinsicInst>(II))
566if (IntrInst->getIntrinsicID() == Intrinsic::fake_use)
567continue;
568
569Instruction *NewInst = cloneInstruction(II);
570 NewInst->insertInto(NewBB, NewBB->end());
571
572if (HostFuncIsStrictFP) {
573// All function calls in the inlined function must get 'strictfp'
574// attribute to prevent undesirable optimizations.
575if (auto *Call = dyn_cast<CallInst>(NewInst))
576Call->addFnAttr(Attribute::StrictFP);
577 }
578
579// Eagerly remap operands to the newly cloned instruction, except for PHI
580// nodes for which we defer processing until we update the CFG. Also defer
581// debug intrinsic processing because they may contain use-before-defs.
582if (!isa<PHINode>(NewInst) && !isa<DbgVariableIntrinsic>(NewInst)) {
583RemapInstruction(NewInst, VMap,
584 ModuleLevelChanges ?RF_None :RF_NoModuleLevelChanges);
585
586// Eagerly constant fold the newly cloned instruction. If successful, add
587// a mapping to the new value. Non-constant operands may be incomplete at
588// this stage, thus instruction simplification is performed after
589// processing phi-nodes.
590if (Value *V =ConstantFoldInstruction(
591 NewInst, BB->getDataLayout())) {
592if (isInstructionTriviallyDead(NewInst)) {
593 VMap[&*II] =V;
594 NewInst->eraseFromParent();
595continue;
596 }
597 }
598 }
599
600if (II->hasName())
601 NewInst->setName(II->getName() + NameSuffix);
602 VMap[&*II] = NewInst;// Add instruction map to value.
603if (isa<CallInst>(II) && !II->isDebugOrPseudoInst()) {
604 hasCalls =true;
605 hasMemProfMetadata |=II->hasMetadata(LLVMContext::MD_memprof);
606 hasMemProfMetadata |=II->hasMetadata(LLVMContext::MD_callsite);
607 }
608
609 CloneDbgRecordsToHere(NewInst,II);
610
611if (CodeInfo) {
612 CodeInfo->OrigVMap[&*II] = NewInst;
613if (auto *CB = dyn_cast<CallBase>(&*II))
614if (CB->hasOperandBundles())
615 CodeInfo->OperandBundleCallSites.push_back(NewInst);
616 }
617
618if (constAllocaInst *AI = dyn_cast<AllocaInst>(II)) {
619if (isa<ConstantInt>(AI->getArraySize()))
620 hasStaticAllocas =true;
621else
622 hasDynamicAllocas =true;
623 }
624 }
625
626// Finally, clone over the terminator.
627constInstruction *OldTI = BB->getTerminator();
628bool TerminatorDone =false;
629if (constBranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
630if (BI->isConditional()) {
631// If the condition was a known constant in the callee...
632ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
633// Or is a known constant in the caller...
634if (!Cond) {
635Value *V = VMap.lookup(BI->getCondition());
636Cond = dyn_cast_or_null<ConstantInt>(V);
637 }
638
639// Constant fold to uncond branch!
640if (Cond) {
641BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
642 VMap[OldTI] =BranchInst::Create(Dest, NewBB);
643 ToClone.push_back(Dest);
644 TerminatorDone =true;
645 }
646 }
647 }elseif (constSwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
648// If switching on a value known constant in the caller.
649ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
650if (!Cond) {// Or known constant after constant prop in the callee...
651Value *V = VMap.lookup(SI->getCondition());
652Cond = dyn_cast_or_null<ConstantInt>(V);
653 }
654if (Cond) {// Constant fold to uncond branch!
655SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
656BasicBlock *Dest =const_cast<BasicBlock *>(Case.getCaseSuccessor());
657 VMap[OldTI] =BranchInst::Create(Dest, NewBB);
658 ToClone.push_back(Dest);
659 TerminatorDone =true;
660 }
661 }
662
663if (!TerminatorDone) {
664Instruction *NewInst = OldTI->clone();
665if (OldTI->hasName())
666 NewInst->setName(OldTI->getName() + NameSuffix);
667 NewInst->insertInto(NewBB, NewBB->end());
668
669 CloneDbgRecordsToHere(NewInst, OldTI->getIterator());
670
671 VMap[OldTI] = NewInst;// Add instruction map to value.
672
673if (CodeInfo) {
674 CodeInfo->OrigVMap[OldTI] = NewInst;
675if (auto *CB = dyn_cast<CallBase>(OldTI))
676if (CB->hasOperandBundles())
677 CodeInfo->OperandBundleCallSites.push_back(NewInst);
678 }
679
680// Recursively clone any reachable successor blocks.
681append_range(ToClone,successors(BB->getTerminator()));
682 }else {
683// If we didn't create a new terminator, clone DbgVariableRecords from the
684// old terminator onto the new terminator.
685Instruction *NewInst = NewBB->getTerminator();
686assert(NewInst);
687
688 CloneDbgRecordsToHere(NewInst, OldTI->getIterator());
689 }
690
691if (CodeInfo) {
692 CodeInfo->ContainsCalls |= hasCalls;
693 CodeInfo->ContainsMemProfMetadata |= hasMemProfMetadata;
694 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
695 CodeInfo->ContainsDynamicAllocas |=
696 hasStaticAllocas && BB != &BB->getParent()->front();
697 }
698}
699
700/// This works like CloneAndPruneFunctionInto, except that it does not clone the
701/// entire function. Instead it starts at an instruction provided by the caller
702/// and copies (and prunes) only the code reachable from that instruction.
703voidllvm::CloneAndPruneIntoFromInst(Function *NewFunc,constFunction *OldFunc,
704constInstruction *StartingInst,
705ValueToValueMapTy &VMap,
706bool ModuleLevelChanges,
707SmallVectorImpl<ReturnInst *> &Returns,
708constchar *NameSuffix,
709ClonedCodeInfo *CodeInfo) {
710assert(NameSuffix &&"NameSuffix cannot be null!");
711
712ValueMapTypeRemapper *TypeMapper =nullptr;
713ValueMaterializer *Materializer =nullptr;
714
715#ifndef NDEBUG
716// If the cloning starts at the beginning of the function, verify that
717// the function arguments are mapped.
718if (!StartingInst)
719for (constArgument &II : OldFunc->args())
720assert(VMap.count(&II) &&"No mapping from source argument specified!");
721#endif
722
723 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
724 NameSuffix, CodeInfo);
725constBasicBlock *StartingBB;
726if (StartingInst)
727 StartingBB = StartingInst->getParent();
728else {
729 StartingBB = &OldFunc->getEntryBlock();
730 StartingInst = &StartingBB->front();
731 }
732
733// Collect debug intrinsics for remapping later.
734SmallVector<const DbgVariableIntrinsic *, 8> DbgIntrinsics;
735for (constauto &BB : *OldFunc) {
736for (constauto &I : BB) {
737if (constauto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
738 DbgIntrinsics.push_back(DVI);
739 }
740 }
741
742// Clone the entry block, and anything recursively reachable from it.
743 std::vector<const BasicBlock *> CloneWorklist;
744 PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
745while (!CloneWorklist.empty()) {
746constBasicBlock *BB = CloneWorklist.back();
747 CloneWorklist.pop_back();
748 PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
749 }
750
751// Loop over all of the basic blocks in the old function. If the block was
752// reachable, we have cloned it and the old block is now in the value map:
753// insert it into the new function in the right order. If not, ignore it.
754//
755// Defer PHI resolution until rest of function is resolved.
756SmallVector<const PHINode *, 16> PHIToResolve;
757for (constBasicBlock &BI : *OldFunc) {
758Value *V = VMap.lookup(&BI);
759BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
760if (!NewBB)
761continue;// Dead block.
762
763// Move the new block to preserve the order in the original function.
764 NewBB->moveBefore(NewFunc->end());
765
766// Handle PHI nodes specially, as we have to remove references to dead
767// blocks.
768for (constPHINode &PN : BI.phis()) {
769// PHI nodes may have been remapped to non-PHI nodes by the caller or
770// during the cloning process.
771if (isa<PHINode>(VMap[&PN]))
772 PHIToResolve.push_back(&PN);
773else
774break;
775 }
776
777// Finally, remap the terminator instructions, as those can't be remapped
778// until all BBs are mapped.
779RemapInstruction(NewBB->getTerminator(), VMap,
780 ModuleLevelChanges ?RF_None :RF_NoModuleLevelChanges,
781 TypeMapper, Materializer);
782 }
783
784// Defer PHI resolution until rest of function is resolved, PHI resolution
785// requires the CFG to be up-to-date.
786for (unsigned phino = 0, e = PHIToResolve.size(); phino != e;) {
787constPHINode *OPN = PHIToResolve[phino];
788unsigned NumPreds = OPN->getNumIncomingValues();
789constBasicBlock *OldBB = OPN->getParent();
790BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
791
792// Map operands for blocks that are live and remove operands for blocks
793// that are dead.
794for (; phino != PHIToResolve.size() &&
795 PHIToResolve[phino]->getParent() == OldBB;
796 ++phino) {
797 OPN = PHIToResolve[phino];
798PHINode *PN = cast<PHINode>(VMap[OPN]);
799for (unsignedpred = 0, e = NumPreds;pred != e; ++pred) {
800Value *V = VMap.lookup(PN->getIncomingBlock(pred));
801if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
802Value *InVal =
803MapValue(PN->getIncomingValue(pred), VMap,
804 ModuleLevelChanges ?RF_None :RF_NoModuleLevelChanges);
805assert(InVal &&"Unknown input value?");
806 PN->setIncomingValue(pred, InVal);
807 PN->setIncomingBlock(pred, MappedBlock);
808 }else {
809 PN->removeIncomingValue(pred,false);
810 --pred;// Revisit the next entry.
811 --e;
812 }
813 }
814 }
815
816// The loop above has removed PHI entries for those blocks that are dead
817// and has updated others. However, if a block is live (i.e. copied over)
818// but its terminator has been changed to not go to this block, then our
819// phi nodes will have invalid entries. Update the PHI nodes in this
820// case.
821PHINode *PN = cast<PHINode>(NewBB->begin());
822 NumPreds =pred_size(NewBB);
823if (NumPreds != PN->getNumIncomingValues()) {
824assert(NumPreds < PN->getNumIncomingValues());
825// Count how many times each predecessor comes to this block.
826 std::map<BasicBlock *, unsigned> PredCount;
827for (BasicBlock *Pred :predecessors(NewBB))
828 --PredCount[Pred];
829
830// Figure out how many entries to remove from each PHI.
831for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
832 ++PredCount[PN->getIncomingBlock(i)];
833
834// At this point, the excess predecessor entries are positive in the
835// map. Loop over all of the PHIs and remove excess predecessor
836// entries.
837BasicBlock::iteratorI = NewBB->begin();
838for (; (PN = dyn_cast<PHINode>(I)); ++I) {
839for (constauto &PCI : PredCount) {
840BasicBlock *Pred = PCI.first;
841for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
842 PN->removeIncomingValue(Pred,false);
843 }
844 }
845 }
846
847// If the loops above have made these phi nodes have 0 or 1 operand,
848// replace them with poison or the input value. We must do this for
849// correctness, because 0-operand phis are not valid.
850 PN = cast<PHINode>(NewBB->begin());
851if (PN->getNumIncomingValues() == 0) {
852BasicBlock::iteratorI = NewBB->begin();
853BasicBlock::const_iterator OldI = OldBB->begin();
854while ((PN = dyn_cast<PHINode>(I++))) {
855Value *NV =PoisonValue::get(PN->getType());
856 PN->replaceAllUsesWith(NV);
857assert(VMap[&*OldI] == PN &&"VMap mismatch");
858 VMap[&*OldI] = NV;
859 PN->eraseFromParent();
860 ++OldI;
861 }
862 }
863 }
864
865// Drop all incompatible return attributes that cannot be applied to NewFunc
866// during cloning, so as to allow instruction simplification to reason on the
867// old state of the function. The original attributes are restored later.
868AttributeList Attrs = NewFunc->getAttributes();
869AttributeMask IncompatibleAttrs =AttributeFuncs::typeIncompatible(
870 OldFunc->getReturnType(), Attrs.getRetAttrs());
871 NewFunc->removeRetAttrs(IncompatibleAttrs);
872
873// As phi-nodes have been now remapped, allow incremental simplification of
874// newly-cloned instructions.
875constDataLayout &DL = NewFunc->getDataLayout();
876for (constauto &BB : *OldFunc) {
877for (constauto &I : BB) {
878auto *NewI = dyn_cast_or_null<Instruction>(VMap.lookup(&I));
879if (!NewI)
880continue;
881
882if (Value *V =simplifyInstruction(NewI,DL)) {
883 NewI->replaceAllUsesWith(V);
884
885if (isInstructionTriviallyDead(NewI)) {
886 NewI->eraseFromParent();
887 }else {
888// Did not erase it? Restore the new instruction into VMap previously
889// dropped by `ValueIsRAUWd`.
890 VMap[&I] = NewI;
891 }
892 }
893 }
894 }
895
896// Restore attributes.
897 NewFunc->setAttributes(Attrs);
898
899// Remap debug intrinsic operands now that all values have been mapped.
900// Doing this now (late) preserves use-before-defs in debug intrinsics. If
901// we didn't do this, ValueAsMetadata(use-before-def) operands would be
902// replaced by empty metadata. This would signal later cleanup passes to
903// remove the debug intrinsics, potentially causing incorrect locations.
904for (constauto *DVI : DbgIntrinsics) {
905if (DbgVariableIntrinsic *NewDVI =
906 cast_or_null<DbgVariableIntrinsic>(VMap.lookup(DVI)))
907RemapInstruction(NewDVI, VMap,
908 ModuleLevelChanges ?RF_None :RF_NoModuleLevelChanges,
909 TypeMapper, Materializer);
910 }
911
912// Do the same for DbgVariableRecords, touching all the instructions in the
913// cloned range of blocks.
914Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
915for (BasicBlock &BB :make_range(Begin, NewFunc->end())) {
916for (Instruction &I : BB) {
917RemapDbgRecordRange(I.getModule(),I.getDbgRecordRange(), VMap,
918 ModuleLevelChanges ?RF_None
919 :RF_NoModuleLevelChanges,
920 TypeMapper, Materializer);
921 }
922 }
923
924// Simplify conditional branches and switches with a constant operand. We try
925// to prune these out when cloning, but if the simplification required
926// looking through PHI nodes, those are only available after forming the full
927// basic block. That may leave some here, and we still want to prune the dead
928// code as early as possible.
929for (BasicBlock &BB :make_range(Begin, NewFunc->end()))
930ConstantFoldTerminator(&BB);
931
932// Some blocks may have become unreachable as a result. Find and delete them.
933 {
934SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
935SmallVector<BasicBlock *, 16> Worklist;
936 Worklist.push_back(&*Begin);
937while (!Worklist.empty()) {
938BasicBlock *BB = Worklist.pop_back_val();
939if (ReachableBlocks.insert(BB).second)
940append_range(Worklist,successors(BB));
941 }
942
943SmallVector<BasicBlock *, 16> UnreachableBlocks;
944for (BasicBlock &BB :make_range(Begin, NewFunc->end()))
945if (!ReachableBlocks.contains(&BB))
946 UnreachableBlocks.push_back(&BB);
947DeleteDeadBlocks(UnreachableBlocks);
948 }
949
950// Now that the inlined function body has been fully constructed, go through
951// and zap unconditional fall-through branches. This happens all the time when
952// specializing code: code specialization turns conditional branches into
953// uncond branches, and this code folds them.
954Function::iteratorI = Begin;
955while (I != NewFunc->end()) {
956BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
957if (!BI || BI->isConditional()) {
958 ++I;
959continue;
960 }
961
962BasicBlock *Dest = BI->getSuccessor(0);
963if (!Dest->getSinglePredecessor()) {
964 ++I;
965continue;
966 }
967
968// We shouldn't be able to get single-entry PHI nodes here, as instsimplify
969// above should have zapped all of them..
970assert(!isa<PHINode>(Dest->begin()));
971
972// We know all single-entry PHI nodes in the inlined function have been
973// removed, so we just need to splice the blocks.
974 BI->eraseFromParent();
975
976// Make all PHI nodes that referred to Dest now refer to I as their source.
977 Dest->replaceAllUsesWith(&*I);
978
979// Move all the instructions in the succ to the pred.
980I->splice(I->end(), Dest);
981
982// Remove the dest block.
983 Dest->eraseFromParent();
984
985// Do not increment I, iteratively merge all things this block branches to.
986 }
987
988// Make a final pass over the basic blocks from the old function to gather
989// any return instructions which survived folding. We have to do this here
990// because we can iteratively remove and merge returns above.
991for (Function::iteratorI = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
992 E = NewFunc->end();
993I != E; ++I)
994if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
995 Returns.push_back(RI);
996}
997
998/// This works exactly like CloneFunctionInto,
999/// except that it does some simple constant prop and DCE on the fly. The
1000/// effect of this is to copy significantly less code in cases where (for
1001/// example) a function call with constant arguments is inlined, and those
1002/// constant arguments cause a significant amount of code in the callee to be
1003/// dead. Since this doesn't produce an exact copy of the input, it can't be
1004/// used for things like CloneFunction or CloneModule.
1005voidllvm::CloneAndPruneFunctionInto(
1006Function *NewFunc,constFunction *OldFunc,ValueToValueMapTy &VMap,
1007bool ModuleLevelChanges,SmallVectorImpl<ReturnInst *> &Returns,
1008constchar *NameSuffix,ClonedCodeInfo *CodeInfo) {
1009CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
1010 ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
1011}
1012
1013/// Remaps instructions in \p Blocks using the mapping in \p VMap.
1014voidllvm::remapInstructionsInBlocks(ArrayRef<BasicBlock *>Blocks,
1015ValueToValueMapTy &VMap) {
1016// Rewrite the code to refer to itself.
1017for (auto *BB :Blocks) {
1018for (auto &Inst : *BB) {
1019RemapDbgRecordRange(Inst.getModule(), Inst.getDbgRecordRange(), VMap,
1020RF_NoModuleLevelChanges |RF_IgnoreMissingLocals);
1021RemapInstruction(&Inst, VMap,
1022RF_NoModuleLevelChanges |RF_IgnoreMissingLocals);
1023 }
1024 }
1025}
1026
1027/// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
1028/// Blocks.
1029///
1030/// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
1031/// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
1032Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before,BasicBlock *LoopDomBB,
1033Loop *OrigLoop,ValueToValueMapTy &VMap,
1034constTwine &NameSuffix,LoopInfo *LI,
1035DominatorTree *DT,
1036SmallVectorImpl<BasicBlock *> &Blocks) {
1037Function *F = OrigLoop->getHeader()->getParent();
1038Loop *ParentLoop = OrigLoop->getParentLoop();
1039DenseMap<Loop *, Loop *> LMap;
1040
1041Loop *NewLoop = LI->AllocateLoop();
1042 LMap[OrigLoop] = NewLoop;
1043if (ParentLoop)
1044 ParentLoop->addChildLoop(NewLoop);
1045else
1046 LI->addTopLevelLoop(NewLoop);
1047
1048BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
1049assert(OrigPH &&"No preheader");
1050BasicBlock *NewPH =CloneBasicBlock(OrigPH, VMap, NameSuffix,F);
1051// To rename the loop PHIs.
1052 VMap[OrigPH] = NewPH;
1053Blocks.push_back(NewPH);
1054
1055// Update LoopInfo.
1056if (ParentLoop)
1057 ParentLoop->addBasicBlockToLoop(NewPH, *LI);
1058
1059// Update DominatorTree.
1060 DT->addNewBlock(NewPH, LoopDomBB);
1061
1062for (Loop *CurLoop : OrigLoop->getLoopsInPreorder()) {
1063Loop *&NewLoop = LMap[CurLoop];
1064if (!NewLoop) {
1065 NewLoop = LI->AllocateLoop();
1066
1067// Establish the parent/child relationship.
1068Loop *OrigParent = CurLoop->getParentLoop();
1069assert(OrigParent &&"Could not find the original parent loop");
1070Loop *NewParentLoop = LMap[OrigParent];
1071assert(NewParentLoop &&"Could not find the new parent loop");
1072
1073 NewParentLoop->addChildLoop(NewLoop);
1074 }
1075 }
1076
1077for (BasicBlock *BB : OrigLoop->getBlocks()) {
1078Loop *CurLoop = LI->getLoopFor(BB);
1079Loop *&NewLoop = LMap[CurLoop];
1080assert(NewLoop &&"Expecting new loop to be allocated");
1081
1082BasicBlock *NewBB =CloneBasicBlock(BB, VMap, NameSuffix,F);
1083 VMap[BB] = NewBB;
1084
1085// Update LoopInfo.
1086 NewLoop->addBasicBlockToLoop(NewBB, *LI);
1087
1088// Add DominatorTree node. After seeing all blocks, update to correct
1089// IDom.
1090 DT->addNewBlock(NewBB, NewPH);
1091
1092Blocks.push_back(NewBB);
1093 }
1094
1095for (BasicBlock *BB : OrigLoop->getBlocks()) {
1096// Update loop headers.
1097Loop *CurLoop = LI->getLoopFor(BB);
1098if (BB == CurLoop->getHeader())
1099 LMap[CurLoop]->moveToHeader(cast<BasicBlock>(VMap[BB]));
1100
1101// Update DominatorTree.
1102BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
1103 DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
1104 cast<BasicBlock>(VMap[IDomBB]));
1105 }
1106
1107// Move them physically from the end of the block list.
1108F->splice(Before->getIterator(),F, NewPH->getIterator());
1109F->splice(Before->getIterator(),F, NewLoop->getHeader()->getIterator(),
1110F->end());
1111
1112return NewLoop;
1113}
1114
1115/// Duplicate non-Phi instructions from the beginning of block up to
1116/// StopAt instruction into a split block between BB and its predecessor.
1117BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
1118BasicBlock *BB,BasicBlock *PredBB,Instruction *StopAt,
1119ValueToValueMapTy &ValueMapping,DomTreeUpdater &DTU) {
1120
1121assert(count(successors(PredBB), BB) == 1 &&
1122"There must be a single edge between PredBB and BB!");
1123// We are going to have to map operands from the original BB block to the new
1124// copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
1125// account for entry from PredBB.
1126BasicBlock::iterator BI = BB->begin();
1127for (;PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
1128 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
1129
1130BasicBlock *NewBB =SplitEdge(PredBB, BB);
1131 NewBB->setName(PredBB->getName() +".split");
1132Instruction *NewTerm = NewBB->getTerminator();
1133
1134// FIXME: SplitEdge does not yet take a DTU, so we include the split edge
1135// in the update set here.
1136 DTU.applyUpdates({{DominatorTree::Delete, PredBB, BB},
1137 {DominatorTree::Insert, PredBB, NewBB},
1138 {DominatorTree::Insert, NewBB, BB}});
1139
1140// Clone the non-phi instructions of BB into NewBB, keeping track of the
1141// mapping and using it to remap operands in the cloned instructions.
1142// Stop once we see the terminator too. This covers the case where BB's
1143// terminator gets replaced and StopAt == BB's terminator.
1144for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) {
1145Instruction *New = BI->clone();
1146 New->setName(BI->getName());
1147 New->insertBefore(NewTerm->getIterator());
1148 New->cloneDebugInfoFrom(&*BI);
1149 ValueMapping[&*BI] = New;
1150
1151// Remap operands to patch up intra-block references.
1152for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
1153if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
1154autoI = ValueMapping.find(Inst);
1155if (I != ValueMapping.end())
1156 New->setOperand(i,I->second);
1157 }
1158
1159// Remap debug variable operands.
1160remapDebugVariable(ValueMapping, New);
1161 }
1162
1163return NewBB;
1164}
1165
1166voidllvm::cloneNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
1167DenseMap<MDNode *, MDNode *> &ClonedScopes,
1168StringRef Ext,LLVMContext &Context) {
1169MDBuilder MDB(Context);
1170
1171for (auto *ScopeList : NoAliasDeclScopes) {
1172for (constauto &MDOperand : ScopeList->operands()) {
1173if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) {
1174AliasScopeNode SNANode(MD);
1175
1176 std::stringName;
1177auto ScopeName = SNANode.getName();
1178if (!ScopeName.empty())
1179Name = (Twine(ScopeName) +":" + Ext).str();
1180else
1181Name = std::string(Ext);
1182
1183MDNode *NewScope = MDB.createAnonymousAliasScope(
1184const_cast<MDNode *>(SNANode.getDomain()),Name);
1185 ClonedScopes.insert(std::make_pair(MD, NewScope));
1186 }
1187 }
1188 }
1189}
1190
1191voidllvm::adaptNoAliasScopes(Instruction *I,
1192constDenseMap<MDNode *, MDNode *> &ClonedScopes,
1193LLVMContext &Context) {
1194auto CloneScopeList = [&](constMDNode *ScopeList) ->MDNode * {
1195bool NeedsReplacement =false;
1196SmallVector<Metadata *, 8> NewScopeList;
1197for (constauto &MDOp : ScopeList->operands()) {
1198if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
1199if (auto *NewMD = ClonedScopes.lookup(MD)) {
1200 NewScopeList.push_back(NewMD);
1201 NeedsReplacement =true;
1202continue;
1203 }
1204 NewScopeList.push_back(MD);
1205 }
1206 }
1207if (NeedsReplacement)
1208returnMDNode::get(Context, NewScopeList);
1209returnnullptr;
1210 };
1211
1212if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
1213if (auto *NewScopeList = CloneScopeList(Decl->getScopeList()))
1214 Decl->setScopeList(NewScopeList);
1215
1216auto replaceWhenNeeded = [&](unsigned MD_ID) {
1217if (constMDNode *CSNoAlias =I->getMetadata(MD_ID))
1218if (auto *NewScopeList = CloneScopeList(CSNoAlias))
1219I->setMetadata(MD_ID, NewScopeList);
1220 };
1221 replaceWhenNeeded(LLVMContext::MD_noalias);
1222 replaceWhenNeeded(LLVMContext::MD_alias_scope);
1223}
1224
1225voidllvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
1226ArrayRef<BasicBlock *> NewBlocks,
1227LLVMContext &Context,StringRef Ext) {
1228if (NoAliasDeclScopes.empty())
1229return;
1230
1231DenseMap<MDNode *, MDNode *> ClonedScopes;
1232LLVM_DEBUG(dbgs() <<"cloneAndAdaptNoAliasScopes: cloning "
1233 << NoAliasDeclScopes.size() <<" node(s)\n");
1234
1235cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
1236// Identify instructions using metadata that needs adaptation
1237for (BasicBlock *NewBlock : NewBlocks)
1238for (Instruction &I : *NewBlock)
1239adaptNoAliasScopes(&I, ClonedScopes, Context);
1240}
1241
1242voidllvm::cloneAndAdaptNoAliasScopes(ArrayRef<MDNode *> NoAliasDeclScopes,
1243Instruction *IStart,Instruction *IEnd,
1244LLVMContext &Context,StringRef Ext) {
1245if (NoAliasDeclScopes.empty())
1246return;
1247
1248DenseMap<MDNode *, MDNode *> ClonedScopes;
1249LLVM_DEBUG(dbgs() <<"cloneAndAdaptNoAliasScopes: cloning "
1250 << NoAliasDeclScopes.size() <<" node(s)\n");
1251
1252cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
1253// Identify instructions using metadata that needs adaptation
1254assert(IStart->getParent() == IEnd->getParent() &&"different basic block ?");
1255auto ItStart = IStart->getIterator();
1256auto ItEnd = IEnd->getIterator();
1257 ++ItEnd;// IEnd is included, increment ItEnd to get the end of the range
1258for (auto &I :llvm::make_range(ItStart, ItEnd))
1259adaptNoAliasScopes(&I, ClonedScopes, Context);
1260}
1261
1262voidllvm::identifyNoAliasScopesToClone(
1263ArrayRef<BasicBlock *> BBs,SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
1264for (BasicBlock *BB : BBs)
1265for (Instruction &I : *BB)
1266if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
1267 NoAliasDeclScopes.push_back(Decl->getScopeList());
1268}
1269
1270voidllvm::identifyNoAliasScopesToClone(
1271BasicBlock::iterator Start,BasicBlock::iteratorEnd,
1272SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
1273for (Instruction &I :make_range(Start,End))
1274if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
1275 NoAliasDeclScopes.push_back(Decl->getScopeList());
1276}
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition:ARMSLSHardening.cpp:73
instructions
Expand Atomic instructions
Definition:AtomicExpandPass.cpp:172
AttributeMask.h
getParent
static const Function * getParent(const Value *V)
Definition:BasicAliasAnalysis.cpp:863
BasicBlockUtils.h
Cloning.h
ConstantFolding.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
LLVM_DEBUG
#define LLVM_DEBUG(...)
Definition:Debug.h:106
DerivedTypes.h
DomTreeUpdater.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
End
bool End
Definition:ELF_riscv.cpp:480
Blocks
DenseMap< Block *, BlockRelaxAux > Blocks
Definition:ELF_riscv.cpp:507
pred
hexagon gen pred
Definition:HexagonGenPredicate.cpp:134
CFG.h
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Function.h
IntrinsicInst.h
Module.h
Module.h This file contains the declarations for the Module class.
InstIterator.h
InstructionSimplify.h
Instructions.h
LLVMContext.h
LoopInfo.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
MDBuilder.h
Metadata.h
This file contains the declarations for metadata subclasses.
II
uint64_t IntrinsicInst * II
Definition:NVVMIntrRange.cpp:51
Cond
const SmallVectorImpl< MachineOperand > & Cond
Definition:RISCVRedundantCopyElimination.cpp:75
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallVector.h
This file defines the SmallVector class.
IRDumpFileSuffixType::Before
@ Before
Local.h
ValueMapper.h
llvm::AliasScopeNode
This is a simple wrapper around an MDNode which provides a higher-level interface by hiding the detai...
Definition:Metadata.h:1573
llvm::AliasScopeNode::getDomain
const MDNode * getDomain() const
Get the MDNode for this AliasScopeNode's domain.
Definition:Metadata.h:1584
llvm::AliasScopeNode::getName
StringRef getName() const
Definition:Metadata.h:1589
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition:Instructions.h:63
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition:Argument.h:31
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition:ArrayRef.h:163
llvm::AttributeList
Definition:Attributes.h:490
llvm::AttributeList::getFnAttrs
AttributeSet getFnAttrs() const
The function attributes are returned.
Definition:Attributes.cpp:1860
llvm::AttributeList::get
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
Definition:Attributes.cpp:1499
llvm::AttributeList::getRetAttrs
AttributeSet getRetAttrs() const
The attributes for the ret value are returned.
Definition:Attributes.cpp:1856
llvm::AttributeList::hasFnAttr
bool hasFnAttr(Attribute::AttrKind Kind) const
Return true if the attribute exists for the function.
Definition:Attributes.cpp:1877
llvm::AttributeList::getParamAttrs
AttributeSet getParamAttrs(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
Definition:Attributes.cpp:1852
llvm::AttributeMask
Definition:AttributeMask.h:29
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::end
iterator end()
Definition:BasicBlock.h:464
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:451
llvm::BasicBlock::hasAddressTaken
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition:BasicBlock.h:661
llvm::BasicBlock::const_iterator
InstListType::const_iterator const_iterator
Definition:BasicBlock.h:178
llvm::BasicBlock::front
const Instruction & front() const
Definition:BasicBlock.h:474
llvm::BasicBlock::Create
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition:BasicBlock.h:213
llvm::BasicBlock::getSinglePredecessor
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition:BasicBlock.cpp:471
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition:BasicBlock.h:220
llvm::BasicBlock::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
Definition:BasicBlock.cpp:296
llvm::BasicBlock::eraseFromParent
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition:BasicBlock.cpp:279
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition:BasicBlock.h:177
llvm::BasicBlock::getContext
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition:BasicBlock.cpp:168
llvm::BasicBlock::IsNewDbgInfoFormat
bool IsNewDbgInfoFormat
Flag recording whether or not this block stores debug-info in the form of intrinsic instructions (fal...
Definition:BasicBlock.h:67
llvm::BasicBlock::moveBefore
void moveBefore(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it into the function that MovePos lives ...
Definition:BasicBlock.h:379
llvm::BasicBlock::getTerminator
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition:BasicBlock.h:240
llvm::BasicBlock::back
const Instruction & back() const
Definition:BasicBlock.h:476
llvm::BlockAddress::get
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition:Constants.cpp:1897
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition:Instructions.h:3016
llvm::BranchInst::isConditional
bool isConditional() const
Definition:Instructions.h:3090
llvm::BranchInst::Create
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
Definition:Instructions.h:3072
llvm::BranchInst::getSuccessor
BasicBlock * getSuccessor(unsigned i) const
Definition:Instructions.h:3104
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition:Instructions.h:1514
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition:InstrTypes.h:673
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::DICompileUnit
Compile unit.
Definition:DebugInfoMetadata.h:1469
llvm::DIScope
Base class for scope-like contexts.
Definition:DebugInfoMetadata.h:519
llvm::DISubprogram
Subprogram description.
Definition:DebugInfoMetadata.h:1710
llvm::DIType
Base class for types.
Definition:DebugInfoMetadata.h:710
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition:DataLayout.h:63
llvm::DbgVariableIntrinsic
This is the common base class for debug info intrinsics for variables.
Definition:IntrinsicInst.h:308
llvm::DebugInfoFinder
Utility to find all debug info in a module.
Definition:DebugInfo.h:105
llvm::DebugInfoFinder::processInstruction
void processInstruction(const Module &M, const Instruction &I)
Process a single instruction and collect debug info anchors.
Definition:DebugInfo.cpp:256
llvm::DebugInfoFinder::subprogram_count
unsigned subprogram_count() const
Definition:DebugInfo.h:167
llvm::DebugInfoFinder::processSubprogram
void processSubprogram(DISubprogram *SP)
Process subprogram.
Definition:DebugInfo.cpp:331
llvm::DebugInfoFinder::subprograms
iterator_range< subprogram_iterator > subprograms() const
Definition:DebugInfo.h:149
llvm::DebugInfoFinder::types
iterator_range< type_iterator > types() const
Definition:DebugInfo.h:157
llvm::DebugInfoFinder::scopes
iterator_range< scope_iterator > scopes() const
Definition:DebugInfo.h:161
llvm::DebugInfoFinder::compile_units
iterator_range< compile_unit_iterator > compile_units() const
Definition:DebugInfo.h:145
llvm::DenseMapBase::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition:DenseMap.h:194
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition:DenseMap.h:211
llvm::DenseMap
Definition:DenseMap.h:727
llvm::DomTreeNodeBase::getIDom
DomTreeNodeBase * getIDom() const
Definition:GenericDomTree.h:90
llvm::DomTreeNodeBase::getBlock
NodeT * getBlock() const
Definition:GenericDomTree.h:89
llvm::DomTreeUpdater
Definition:DomTreeUpdater.h:30
llvm::DominatorTreeBase::changeImmediateDominator
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
Definition:GenericDomTree.h:723
llvm::DominatorTreeBase::addNewBlock
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
Definition:GenericDomTree.h:687
llvm::DominatorTreeBase::getNode
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Definition:GenericDomTree.h:401
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition:Dominators.h:162
llvm::FunctionType
Class to represent function types.
Definition:DerivedTypes.h:105
llvm::Function
Definition:Function.h:63
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition:Function.h:173
llvm::Function::getEntryBlock
const BasicBlock & getEntryBlock() const
Definition:Function.h:809
llvm::Function::iterator
BasicBlockListType::iterator iterator
Definition:Function.h:68
llvm::Function::setPrefixData
void setPrefixData(Constant *PrefixData)
Definition:Function.cpp:1063
llvm::Function::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition:Function.cpp:373
llvm::Function::front
const BasicBlock & front() const
Definition:Function.h:860
llvm::Function::args
iterator_range< arg_iterator > args()
Definition:Function.h:892
llvm::Function::IsNewDbgInfoFormat
bool IsNewDbgInfoFormat
Is this function using intrinsics to record the position of debugging information,...
Definition:Function.h:116
llvm::Function::hasPrefixData
bool hasPrefixData() const
Check whether this function has prefix data.
Definition:Function.h:914
llvm::Function::hasPersonalityFn
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition:Function.h:905
llvm::Function::getPrologueData
Constant * getPrologueData() const
Get the prologue data associated with this function.
Definition:Function.cpp:1068
llvm::Function::getPersonalityFn
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition:Function.cpp:1048
llvm::Function::setPersonalityFn
void setPersonalityFn(Constant *Fn)
Definition:Function.cpp:1053
llvm::Function::getAttributes
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition:Function.h:353
llvm::Function::arg_begin
arg_iterator arg_begin()
Definition:Function.h:868
llvm::Function::setAttributes
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition:Function.h:356
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition:Function.cpp:369
llvm::Function::arg_size
size_t arg_size() const
Definition:Function.h:901
llvm::Function::setPrologueData
void setPrologueData(Constant *PrologueData)
Definition:Function.cpp:1073
llvm::Function::removeRetAttrs
void removeRetAttrs(const AttributeMask &Attrs)
removes the attributes from the return value list of attributes.
Definition:Function.cpp:709
llvm::Function::setIsNewDbgInfoFormat
void setIsNewDbgInfoFormat(bool NewVal)
Definition:Function.cpp:105
llvm::Function::getReturnType
Type * getReturnType() const
Returns the type of the ret val.
Definition:Function.h:221
llvm::Function::getPrefixData
Constant * getPrefixData() const
Get the prefix data associated with this function.
Definition:Function.cpp:1058
llvm::Function::end
iterator end()
Definition:Function.h:855
llvm::Function::hasPrologueData
bool hasPrologueData() const
Check whether this function has prologue data.
Definition:Function.h:923
llvm::Function::copyAttributesFrom
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition:Function.cpp:860
llvm::GenericDomTreeUpdater::applyUpdates
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Definition:GenericDomTreeUpdaterImpl.h:59
llvm::GlobalObject::getAllMetadata
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition:Metadata.cpp:1521
llvm::GlobalObject::addMetadata
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition:Metadata.cpp:1565
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition:Globals.cpp:296
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition:GlobalValue.h:657
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::clone
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
Definition:Instruction.cpp:1364
llvm::Instruction::cloneDebugInfoFrom
iterator_range< simple_ilist< DbgRecord >::iterator > cloneDebugInfoFrom(const Instruction *From, std::optional< simple_ilist< DbgRecord >::iterator > FromHere=std::nullopt, bool InsertAtHead=false)
Clone any debug-info attached to From onto this instruction.
Definition:Instruction.cpp:249
llvm::Instruction::insertBefore
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
Definition:Instruction.cpp:99
llvm::Instruction::eraseFromParent
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition:Instruction.cpp:94
llvm::Instruction::insertInto
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
Definition:Instruction.cpp:123
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::LoopBase::getLoopsInPreorder
SmallVector< const LoopT *, 4 > getLoopsInPreorder() const
Return all loops in the loop nest rooted by the loop in preorder, with siblings in forward program or...
Definition:GenericLoopInfo.h:357
llvm::LoopBase::getHeader
BlockT * getHeader() const
Definition:GenericLoopInfo.h:90
llvm::LoopBase::addBasicBlockToLoop
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
Definition:GenericLoopInfoImpl.h:282
llvm::LoopBase::addChildLoop
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
Definition:GenericLoopInfo.h:391
llvm::LoopBase::getLoopPreheader
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition:GenericLoopInfoImpl.h:210
llvm::LoopBase::getBlocks
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition:GenericLoopInfo.h:173
llvm::LoopBase::getParentLoop
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
Definition:GenericLoopInfo.h:99
llvm::LoopInfoBase::addTopLevelLoop
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
Definition:GenericLoopInfo.h:663
llvm::LoopInfoBase::AllocateLoop
LoopT * AllocateLoop(ArgsTy &&...Args)
Definition:GenericLoopInfo.h:570
llvm::LoopInfoBase::getLoopFor
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition:GenericLoopInfo.h:606
llvm::LoopInfo
Definition:LoopInfo.h:407
llvm::Loop
Represents a single loop in the control flow graph.
Definition:LoopInfo.h:39
llvm::MDBuilder
Definition:MDBuilder.h:36
llvm::MDBuilder::createAnonymousAliasScope
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
Definition:MDBuilder.h:174
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition:Metadata.h:1549
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition:Metadata.h:895
llvm::MDString::get
static MDString * get(LLVMContext &Context, StringRef Str)
Definition:Metadata.cpp:606
llvm::MetadataAsValue::get
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition:Metadata.cpp:103
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::Module::getOrInsertNamedMetadata
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Return the named MDNode in the module with the specified name.
Definition:Module.cpp:304
llvm::PHINode
Definition:Instructions.h:2600
llvm::PHINode::setIncomingBlock
void setIncomingBlock(unsigned i, BasicBlock *BB)
Definition:Instructions.h:2714
llvm::PHINode::removeIncomingValue
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
Definition:Instructions.cpp:137
llvm::PHINode::setIncomingValue
void setIncomingValue(unsigned i, Value *V)
Definition:Instructions.h:2678
llvm::PHINode::getIncomingValueForBlock
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition:Instructions.h:2775
llvm::PHINode::getIncomingBlock
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Definition:Instructions.h:2695
llvm::PHINode::getIncomingValue
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Definition:Instructions.h:2675
llvm::PHINode::getNumIncomingValues
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition:Instructions.h:2671
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::ReturnInst
Return a value (possibly void), from a function.
Definition:Instructions.h:2938
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition:SmallPtrSet.h:384
llvm::SmallPtrSetImpl::contains
bool contains(ConstPtrType Ptr) const
Definition:SmallPtrSet.h:458
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallVectorBase::empty
bool empty() const
Definition:SmallVector.h:81
llvm::SmallVectorBase::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition:SmallVector.h:673
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::SwitchInst::CaseHandleImpl
A handle to a particular switch case.
Definition:Instructions.h:3198
llvm::SwitchInst::CaseHandleImpl::getCaseSuccessor
BasicBlockT * getCaseSuccessor() const
Resolves successor for current case.
Definition:Instructions.h:3222
llvm::SwitchInst
Multiway switch.
Definition:Instructions.h:3154
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition:Twine.h:81
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition:User.h:228
llvm::User::getNumOperands
unsigned getNumOperands() const
Definition:User.h:250
llvm::ValueMapTypeRemapper
This is a class that can be implemented by clients to remap types when cloning constants and instruct...
Definition:ValueMapper.h:43
llvm::ValueMap< const Value *, WeakTrackingVH >
llvm::ValueMap::lookup
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition:ValueMap.h:164
llvm::ValueMap::count
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition:ValueMap.h:151
llvm::ValueMap::find
iterator find(const KeyT &Val)
Definition:ValueMap.h:155
llvm::ValueMap::end
iterator end()
Definition:ValueMap.h:135
llvm::ValueMaterializer
This is a class that can be implemented by clients to materialize Values on demand.
Definition:ValueMapper.h:56
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition:Value.h:255
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition:Value.cpp:377
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition:Value.cpp:534
llvm::Value::hasName
bool hasName() const
Definition:Value.h:261
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition:Value.cpp:309
llvm::WeakTrackingVH
Value handle that is nullable, but tries to track the Value.
Definition:ValueHandle.h:204
llvm::ilist_detail::node_parent_access::getParent
const ParentTy * getParent() const
Definition:ilist_node.h:32
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition:ilist_node.h:132
unsigned
DebugInfo.h
CU
Definition:AArch64AsmBackend.cpp:549
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition:AMDGPUMetadata.h:395
llvm::AttributeFuncs::typeIncompatible
AttributeMask typeIncompatible(Type *Ty, AttributeSet AS, AttributeSafetyKind ASK=ASK_ALL)
Which attributes cannot be applied to a type.
Definition:Attributes.cpp:2349
llvm::Intrinsic::getOrInsertDeclaration
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition:Intrinsics.cpp:732
llvm::Intrinsic::getIntrinsicInfoTableEntries
void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl< IITDescriptor > &T)
Return the IIT table descriptor for the specified intrinsic into an array of IITDescriptors.
Definition:Intrinsics.cpp:447
llvm::Intrinsic::not_intrinsic
@ not_intrinsic
Definition:Intrinsics.h:44
llvm::Intrinsic::hasConstrainedFPRoundingModeOperand
bool hasConstrainedFPRoundingModeOperand(ID QID)
Returns true if the intrinsic ID is for one of the "Constrained Floating-Point Intrinsics" that take ...
Definition:Intrinsics.cpp:775
llvm::M68k::MemAddrModeKind::V
@ V
llvm::MCID::Call
@ Call
Definition:MCInstrDesc.h:156
llvm::SIEncodingFamily::SI
@ SI
Definition:SIDefines.h:36
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::CloneFunctionAttributesInto
void CloneFunctionAttributesInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc's attributes into NewFunc, transforming values based on the mappings in VMap.
Definition:CloneFunction.cpp:85
llvm::ConstantFoldTerminator
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition:Local.cpp:136
llvm::successors
auto successors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1376
llvm::MapMetadata
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Lookup or compute a mapping for a piece of metadata.
Definition:ValueMapper.h:250
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition:iterator_range.h:77
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition:STLExtras.h:2115
llvm::remapDebugVariable
void remapDebugVariable(ValueToValueMapTy &Mapping, Instruction *Inst)
Remap the operands of the debug records attached to Inst, and the operands of Inst itself if it's a d...
Definition:Local.cpp:3787
llvm::RemapDbgRecordRange
void RemapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Remap the Values used in the DbgRecords Range using the value map VM.
Definition:ValueMapper.h:299
llvm::pred_size
auto pred_size(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1381
llvm::DuplicateInstructionsInSplitBetween
BasicBlock * DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt, ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU)
Split edge between BB and PredBB and duplicate all non-Phi instructions from BB between its beginning...
Definition:CloneFunction.cpp:1117
llvm::simplifyInstruction
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
Definition:InstructionSimplify.cpp:7234
llvm::isInstructionTriviallyDead
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition:Local.cpp:406
llvm::cloneLoopWithPreheader
Loop * cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, Loop *OrigLoop, ValueToValueMapTy &VMap, const Twine &NameSuffix, LoopInfo *LI, DominatorTree *DT, SmallVectorImpl< BasicBlock * > &Blocks)
Clones a loop OrigLoop.
Definition:CloneFunction.cpp:1032
llvm::RemapFlags
RemapFlags
These are flags that the value mapping APIs allow.
Definition:ValueMapper.h:72
llvm::RF_IgnoreMissingLocals
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition:ValueMapper.h:96
llvm::RF_None
@ RF_None
Definition:ValueMapper.h:73
llvm::RF_NoModuleLevelChanges
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition:ValueMapper.h:78
llvm::CloneAndPruneFunctionInto
void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr)
This works exactly like CloneFunctionInto, except that it does some simple constant prop and DCE on t...
Definition:CloneFunction.cpp:1005
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::MapValue
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Look up or compute a value in the value map.
Definition:ValueMapper.h:224
llvm::RemapInstruction
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
Definition:ValueMapper.h:277
llvm::cloneNoAliasScopes
void cloneNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, DenseMap< MDNode *, MDNode * > &ClonedScopes, StringRef Ext, LLVMContext &Context)
Duplicate the specified list of noalias decl scopes.
Definition:CloneFunction.cpp:1166
llvm::getConstrainedIntrinsicID
Intrinsic::ID getConstrainedIntrinsicID(const Instruction &Instr)
Returns constrained intrinsic id to represent the given instruction in strictfp function.
Definition:FPEnv.cpp:90
llvm::FindDebugInfoToIdentityMap
MetadataSetTy FindDebugInfoToIdentityMap(CloneFunctionChangeType Changes, DebugInfoFinder &DIFinder, DISubprogram *SPClonedWithinModule)
Based on Changes and DIFinder return debug info that needs to be identity mapped during Metadata clon...
Definition:CloneFunction.cpp:156
llvm::CloneFunctionMetadataInto
void CloneFunctionMetadataInto(Function &NewFunc, const Function &OldFunc, ValueToValueMapTy &VMap, RemapFlags RemapFlag, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Clone OldFunc's metadata into NewFunc.
Definition:CloneFunction.cpp:189
llvm::CloneBasicBlock
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
Definition:CloneFunction.cpp:44
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition:STLExtras.h:1938
llvm::adaptNoAliasScopes
void adaptNoAliasScopes(llvm::Instruction *I, const DenseMap< MDNode *, MDNode * > &ClonedScopes, LLVMContext &Context)
Adapt the metadata for the specified instruction according to the provided mapping.
Definition:CloneFunction.cpp:1191
llvm::ConstantFoldInstruction
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Definition:ConstantFolding.cpp:1123
llvm::cloneAndAdaptNoAliasScopes
void cloneAndAdaptNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, ArrayRef< BasicBlock * > NewBlocks, LLVMContext &Context, StringRef Ext)
Clone the specified noalias decl scopes.
Definition:CloneFunction.cpp:1225
llvm::remapInstructionsInBlocks
void remapInstructionsInBlocks(ArrayRef< BasicBlock * > Blocks, ValueToValueMapTy &VMap)
Remaps instructions in Blocks using the mapping in VMap.
Definition:CloneFunction.cpp:1014
llvm::CloneFunctionChangeType
CloneFunctionChangeType
Definition:Cloning.h:138
llvm::CloneFunctionInto
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.
Definition:CloneFunction.cpp:262
llvm::predecessors
auto predecessors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1377
llvm::CollectDebugInfoForCloning
DISubprogram * CollectDebugInfoForCloning(const Function &F, CloneFunctionChangeType Changes, DebugInfoFinder &DIFinder)
Collect debug information such as types, compile units, and other subprograms that are reachable from...
Definition:CloneFunction.cpp:135
llvm::DeleteDeadBlocks
void DeleteDeadBlocks(ArrayRef< BasicBlock * > BBs, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified blocks from BB.
Definition:BasicBlockUtils.cpp:101
llvm::identifyNoAliasScopesToClone
void identifyNoAliasScopesToClone(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< MDNode * > &NoAliasDeclScopes)
Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified basic blocks and extract ...
Definition:CloneFunction.cpp:1262
llvm::SplitEdge
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
Definition:BasicBlockUtils.cpp:762
llvm::CloneAndPruneIntoFromInst
void CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, const Instruction *StartingInst, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr)
This works like CloneAndPruneFunctionInto, except that it does not clone the entire function.
Definition:CloneFunction.cpp:703
llvm::CloneFunction
Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
Return a copy of the specified function and add it to that function's module.
Definition:CloneFunction.cpp:369
llvm::CloneFunctionBodyInto
void CloneFunctionBodyInto(Function &NewFunc, const Function &OldFunc, ValueToValueMapTy &VMap, RemapFlags RemapFlag, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataSetTy *IdentityMD=nullptr)
Clone OldFunc's body into NewFunc.
Definition:CloneFunction.cpp:204
llvm::ClonedCodeInfo
This struct can be used to capture information about code being cloned, while it is being cloned.
Definition:Cloning.h:63
llvm::ClonedCodeInfo::ContainsDynamicAllocas
bool ContainsDynamicAllocas
This is set to true if the cloned code contains a 'dynamic' alloca.
Definition:Cloning.h:74
llvm::ClonedCodeInfo::ContainsCalls
bool ContainsCalls
This is set to true if the cloned code contains a normal call instruction.
Definition:Cloning.h:65
llvm::ClonedCodeInfo::ContainsMemProfMetadata
bool ContainsMemProfMetadata
This is set to true if there is memprof related metadata (memprof or callsite metadata) in the cloned...
Definition:Cloning.h:69
llvm::ClonedCodeInfo::OrigVMap
DenseMap< const Value *, const Value * > OrigVMap
Like VMap, but maps only unsimplified instructions.
Definition:Cloning.h:84
llvm::ClonedCodeInfo::OperandBundleCallSites
std::vector< WeakTrackingVH > OperandBundleCallSites
All cloned call sites that have operand bundles attached are appended to this vector.
Definition:Cloning.h:79
llvm::Intrinsic::IITDescriptor
This is a type descriptor which explains the type requirements of an intrinsic.
Definition:Intrinsics.h:131
llvm::Intrinsic::IITDescriptor::Kind
enum llvm::Intrinsic::IITDescriptor::IITDescriptorKind Kind
llvm::Intrinsic::IITDescriptor::SameVecWidthArgument
@ SameVecWidthArgument
Definition:Intrinsics.h:151
llvm::Intrinsic::IITDescriptor::Argument
@ Argument
Definition:Intrinsics.h:147
llvm::Intrinsic::IITDescriptor::getArgumentKind
ArgKind getArgumentKind() const
Definition:Intrinsics.h:186

Generated on Thu Jul 17 2025 17:32:06 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp