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