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