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 RemapDbgVariableRecordRange(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
390 switch (CIID) {
391#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
392 case Intrinsic::INTRINSIC: \
393 return ROUND_MODE == 1;
394#define FUNCTION INSTRUCTION
395#include "llvm/IR/ConstrainedOps.def"
396 default:
397 llvm_unreachable("Unexpected constrained intrinsic id");
398 }
399}
400
402PruningFunctionCloner::cloneInstruction(BasicBlock::const_iterator II) {
403 const Instruction &OldInst = *II;
404 Instruction *NewInst = nullptr;
405 if (HostFuncIsStrictFP) {
407 if (CIID != Intrinsic::not_intrinsic) {
408 // Instead of cloning the instruction, a call to constrained intrinsic
409 // should be created.
410 // Assume the first arguments of constrained intrinsics are the same as
411 // the operands of original instruction.
412
413 // Determine overloaded types of the intrinsic.
416 getIntrinsicInfoTableEntries(CIID, Descriptor);
417 for (unsigned I = 0, E = Descriptor.size(); I != E; ++I) {
418 Intrinsic::IITDescriptor Operand = Descriptor[I];
419 switch (Operand.Kind) {
421 if (Operand.getArgumentKind() !=
422 Intrinsic::IITDescriptor::AK_MatchType) {
423 if (I == 0)
424 TParams.push_back(OldInst.getType());
425 else
426 TParams.push_back(OldInst.getOperand(I - 1)->getType());
427 }
428 break;
430 ++I;
431 break;
432 default:
433 break;
434 }
435 }
436
437 // Create intrinsic call.
438 LLVMContext &Ctx = NewFunc->getContext();
439 Function *IFn =
440 Intrinsic::getDeclaration(NewFunc->getParent(), CIID, TParams);
442 unsigned NumOperands = OldInst.getNumOperands();
443 if (isa<CallInst>(OldInst))
444 --NumOperands;
445 for (unsigned I = 0; I < NumOperands; ++I) {
446 Value *Op = OldInst.getOperand(I);
447 Args.push_back(Op);
448 }
449 if (const auto *CmpI = dyn_cast<FCmpInst>(&OldInst)) {
450 FCmpInst::Predicate Pred = CmpI->getPredicate();
451 StringRef PredName = FCmpInst::getPredicateName(Pred);
452 Args.push_back(MetadataAsValue::get(Ctx, MDString::get(Ctx, PredName)));
453 }
454
455 // The last arguments of a constrained intrinsic are metadata that
456 // represent rounding mode (absents in some intrinsics) and exception
457 // behavior. The inlined function uses default settings.
458 if (hasRoundingModeOperand(CIID))
459 Args.push_back(
460 MetadataAsValue::get(Ctx, MDString::get(Ctx, "round.tonearest")));
461 Args.push_back(
462 MetadataAsValue::get(Ctx, MDString::get(Ctx, "fpexcept.ignore")));
463
464 NewInst = CallInst::Create(IFn, Args, OldInst.getName() + ".strict");
465 }
466 }
467 if (!NewInst)
468 NewInst = II->clone();
469 return NewInst;
470}
471
472/// The specified block is found to be reachable, clone it and
473/// anything that it can reach.
474void PruningFunctionCloner::CloneBlock(
475 const BasicBlock *BB, BasicBlock::const_iterator StartingInst,
476 std::vector<const BasicBlock *> &ToClone) {
477 WeakTrackingVH &BBEntry = VMap[BB];
478
479 // Have we already cloned this block?
480 if (BBEntry)
481 return;
482
483 // Nope, clone it now.
484 BasicBlock *NewBB;
485 Twine NewName(BB->hasName() ? Twine(BB->getName()) + NameSuffix : "");
486 BBEntry = NewBB = BasicBlock::Create(BB->getContext(), NewName, NewFunc);
488
489 // It is only legal to clone a function if a block address within that
490 // function is never referenced outside of the function. Given that, we
491 // want to map block addresses from the old function to block addresses in
492 // the clone. (This is different from the generic ValueMapper
493 // implementation, which generates an invalid blockaddress when
494 // cloning a function.)
495 //
496 // Note that we don't need to fix the mapping for unreachable blocks;
497 // the default mapping there is safe.
498 if (BB->hasAddressTaken()) {
499 Constant *OldBBAddr = BlockAddress::get(const_cast<Function *>(OldFunc),
500 const_cast<BasicBlock *>(BB));
501 VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
502 }
503
504 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
505 bool hasMemProfMetadata = false;
506
507 // Keep a cursor pointing at the last place we cloned debug-info records from.
508 BasicBlock::const_iterator DbgCursor = StartingInst;
509 auto CloneDbgRecordsToHere =
510 [NewBB, &DbgCursor](Instruction *NewInst, BasicBlock::const_iterator II) {
511 if (!NewBB->IsNewDbgInfoFormat)
512 return;
513
514 // Clone debug-info records onto this instruction. Iterate through any
515 // source-instructions we've cloned and then subsequently optimised
516 // away, so that their debug-info doesn't go missing.
517 for (; DbgCursor != II; ++DbgCursor)
518 NewInst->cloneDebugInfoFrom(&*DbgCursor, std::nullopt, false);
519 NewInst->cloneDebugInfoFrom(&*II);
520 DbgCursor = std::next(II);
521 };
522
523 // Loop over all instructions, and copy them over, DCE'ing as we go. This
524 // loop doesn't include the terminator.
525 for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); II != IE;
526 ++II) {
527
528 Instruction *NewInst = cloneInstruction(II);
529 NewInst->insertInto(NewBB, NewBB->end());
530
531 if (HostFuncIsStrictFP) {
532 // All function calls in the inlined function must get 'strictfp'
533 // attribute to prevent undesirable optimizations.
534 if (auto *Call = dyn_cast<CallInst>(NewInst))
535 Call->addFnAttr(Attribute::StrictFP);
536 }
537
538 // Eagerly remap operands to the newly cloned instruction, except for PHI
539 // nodes for which we defer processing until we update the CFG. Also defer
540 // debug intrinsic processing because they may contain use-before-defs.
541 if (!isa<PHINode>(NewInst) && !isa<DbgVariableIntrinsic>(NewInst)) {
542 RemapInstruction(NewInst, VMap,
543 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
544
545 // Eagerly constant fold the newly cloned instruction. If successful, add
546 // a mapping to the new value. Non-constant operands may be incomplete at
547 // this stage, thus instruction simplification is performed after
548 // processing phi-nodes.
550 NewInst, BB->getModule()->getDataLayout())) {
551 if (isInstructionTriviallyDead(NewInst)) {
552 VMap[&*II] = V;
553 NewInst->eraseFromParent();
554 continue;
555 }
556 }
557 }
558
559 if (II->hasName())
560 NewInst->setName(II->getName() + NameSuffix);
561 VMap[&*II] = NewInst; // Add instruction map to value.
562 if (isa<CallInst>(II) && !II->isDebugOrPseudoInst()) {
563 hasCalls = true;
564 hasMemProfMetadata |= II->hasMetadata(LLVMContext::MD_memprof);
565 }
566
567 CloneDbgRecordsToHere(NewInst, II);
568
569 if (CodeInfo) {
570 CodeInfo->OrigVMap[&*II] = NewInst;
571 if (auto *CB = dyn_cast<CallBase>(&*II))
572 if (CB->hasOperandBundles())
573 CodeInfo->OperandBundleCallSites.push_back(NewInst);
574 }
575
576 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
577 if (isa<ConstantInt>(AI->getArraySize()))
578 hasStaticAllocas = true;
579 else
580 hasDynamicAllocas = true;
581 }
582 }
583
584 // Finally, clone over the terminator.
585 const Instruction *OldTI = BB->getTerminator();
586 bool TerminatorDone = false;
587 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
588 if (BI->isConditional()) {
589 // If the condition was a known constant in the callee...
590 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
591 // Or is a known constant in the caller...
592 if (!Cond) {
593 Value *V = VMap.lookup(BI->getCondition());
594 Cond = dyn_cast_or_null<ConstantInt>(V);
595 }
596
597 // Constant fold to uncond branch!
598 if (Cond) {
599 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
600 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
601 ToClone.push_back(Dest);
602 TerminatorDone = true;
603 }
604 }
605 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
606 // If switching on a value known constant in the caller.
607 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
608 if (!Cond) { // Or known constant after constant prop in the callee...
609 Value *V = VMap.lookup(SI->getCondition());
610 Cond = dyn_cast_or_null<ConstantInt>(V);
611 }
612 if (Cond) { // Constant fold to uncond branch!
613 SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
614 BasicBlock *Dest = const_cast<BasicBlock *>(Case.getCaseSuccessor());
615 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
616 ToClone.push_back(Dest);
617 TerminatorDone = true;
618 }
619 }
620
621 if (!TerminatorDone) {
622 Instruction *NewInst = OldTI->clone();
623 if (OldTI->hasName())
624 NewInst->setName(OldTI->getName() + NameSuffix);
625 NewInst->insertInto(NewBB, NewBB->end());
626
627 CloneDbgRecordsToHere(NewInst, OldTI->getIterator());
628
629 VMap[OldTI] = NewInst; // Add instruction map to value.
630
631 if (CodeInfo) {
632 CodeInfo->OrigVMap[OldTI] = NewInst;
633 if (auto *CB = dyn_cast<CallBase>(OldTI))
634 if (CB->hasOperandBundles())
635 CodeInfo->OperandBundleCallSites.push_back(NewInst);
636 }
637
638 // Recursively clone any reachable successor blocks.
639 append_range(ToClone, successors(BB->getTerminator()));
640 } else {
641 // If we didn't create a new terminator, clone DbgVariableRecords from the
642 // old terminator onto the new terminator.
643 Instruction *NewInst = NewBB->getTerminator();
644 assert(NewInst);
645
646 CloneDbgRecordsToHere(NewInst, OldTI->getIterator());
647 }
648
649 if (CodeInfo) {
650 CodeInfo->ContainsCalls |= hasCalls;
651 CodeInfo->ContainsMemProfMetadata |= hasMemProfMetadata;
652 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
653 CodeInfo->ContainsDynamicAllocas |=
654 hasStaticAllocas && BB != &BB->getParent()->front();
655 }
656}
657
658/// This works like CloneAndPruneFunctionInto, except that it does not clone the
659/// entire function. Instead it starts at an instruction provided by the caller
660/// and copies (and prunes) only the code reachable from that instruction.
662 const Instruction *StartingInst,
663 ValueToValueMapTy &VMap,
664 bool ModuleLevelChanges,
666 const char *NameSuffix,
667 ClonedCodeInfo *CodeInfo) {
668 assert(NameSuffix && "NameSuffix cannot be null!");
669
670 ValueMapTypeRemapper *TypeMapper = nullptr;
671 ValueMaterializer *Materializer = nullptr;
672
673#ifndef NDEBUG
674 // If the cloning starts at the beginning of the function, verify that
675 // the function arguments are mapped.
676 if (!StartingInst)
677 for (const Argument &II : OldFunc->args())
678 assert(VMap.count(&II) && "No mapping from source argument specified!");
679#endif
680
681 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
682 NameSuffix, CodeInfo);
683 const BasicBlock *StartingBB;
684 if (StartingInst)
685 StartingBB = StartingInst->getParent();
686 else {
687 StartingBB = &OldFunc->getEntryBlock();
688 StartingInst = &StartingBB->front();
689 }
690
691 // Collect debug intrinsics for remapping later.
693 for (const auto &BB : *OldFunc) {
694 for (const auto &I : BB) {
695 if (const auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I))
696 DbgIntrinsics.push_back(DVI);
697 }
698 }
699
700 // Clone the entry block, and anything recursively reachable from it.
701 std::vector<const BasicBlock *> CloneWorklist;
702 PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
703 while (!CloneWorklist.empty()) {
704 const BasicBlock *BB = CloneWorklist.back();
705 CloneWorklist.pop_back();
706 PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
707 }
708
709 // Loop over all of the basic blocks in the old function. If the block was
710 // reachable, we have cloned it and the old block is now in the value map:
711 // insert it into the new function in the right order. If not, ignore it.
712 //
713 // Defer PHI resolution until rest of function is resolved.
715 for (const BasicBlock &BI : *OldFunc) {
716 Value *V = VMap.lookup(&BI);
717 BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
718 if (!NewBB)
719 continue; // Dead block.
720
721 // Move the new block to preserve the order in the original function.
722 NewBB->moveBefore(NewFunc->end());
723
724 // Handle PHI nodes specially, as we have to remove references to dead
725 // blocks.
726 for (const PHINode &PN : BI.phis()) {
727 // PHI nodes may have been remapped to non-PHI nodes by the caller or
728 // during the cloning process.
729 if (isa<PHINode>(VMap[&PN]))
730 PHIToResolve.push_back(&PN);
731 else
732 break;
733 }
734
735 // Finally, remap the terminator instructions, as those can't be remapped
736 // until all BBs are mapped.
737 RemapInstruction(NewBB->getTerminator(), VMap,
738 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
739 TypeMapper, Materializer);
740 }
741
742 // Defer PHI resolution until rest of function is resolved, PHI resolution
743 // requires the CFG to be up-to-date.
744 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e;) {
745 const PHINode *OPN = PHIToResolve[phino];
746 unsigned NumPreds = OPN->getNumIncomingValues();
747 const BasicBlock *OldBB = OPN->getParent();
748 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
749
750 // Map operands for blocks that are live and remove operands for blocks
751 // that are dead.
752 for (; phino != PHIToResolve.size() &&
753 PHIToResolve[phino]->getParent() == OldBB;
754 ++phino) {
755 OPN = PHIToResolve[phino];
756 PHINode *PN = cast<PHINode>(VMap[OPN]);
757 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
758 Value *V = VMap.lookup(PN->getIncomingBlock(pred));
759 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
760 Value *InVal =
761 MapValue(PN->getIncomingValue(pred), VMap,
762 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
763 assert(InVal && "Unknown input value?");
764 PN->setIncomingValue(pred, InVal);
765 PN->setIncomingBlock(pred, MappedBlock);
766 } else {
767 PN->removeIncomingValue(pred, false);
768 --pred; // Revisit the next entry.
769 --e;
770 }
771 }
772 }
773
774 // The loop above has removed PHI entries for those blocks that are dead
775 // and has updated others. However, if a block is live (i.e. copied over)
776 // but its terminator has been changed to not go to this block, then our
777 // phi nodes will have invalid entries. Update the PHI nodes in this
778 // case.
779 PHINode *PN = cast<PHINode>(NewBB->begin());
780 NumPreds = pred_size(NewBB);
781 if (NumPreds != PN->getNumIncomingValues()) {
782 assert(NumPreds < PN->getNumIncomingValues());
783 // Count how many times each predecessor comes to this block.
784 std::map<BasicBlock *, unsigned> PredCount;
785 for (BasicBlock *Pred : predecessors(NewBB))
786 --PredCount[Pred];
787
788 // Figure out how many entries to remove from each PHI.
789 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
790 ++PredCount[PN->getIncomingBlock(i)];
791
792 // At this point, the excess predecessor entries are positive in the
793 // map. Loop over all of the PHIs and remove excess predecessor
794 // entries.
795 BasicBlock::iterator I = NewBB->begin();
796 for (; (PN = dyn_cast<PHINode>(I)); ++I) {
797 for (const auto &PCI : PredCount) {
798 BasicBlock *Pred = PCI.first;
799 for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
800 PN->removeIncomingValue(Pred, false);
801 }
802 }
803 }
804
805 // If the loops above have made these phi nodes have 0 or 1 operand,
806 // replace them with poison or the input value. We must do this for
807 // correctness, because 0-operand phis are not valid.
808 PN = cast<PHINode>(NewBB->begin());
809 if (PN->getNumIncomingValues() == 0) {
810 BasicBlock::iterator I = NewBB->begin();
811 BasicBlock::const_iterator OldI = OldBB->begin();
812 while ((PN = dyn_cast<PHINode>(I++))) {
813 Value *NV = PoisonValue::get(PN->getType());
814 PN->replaceAllUsesWith(NV);
815 assert(VMap[&*OldI] == PN && "VMap mismatch");
816 VMap[&*OldI] = NV;
817 PN->eraseFromParent();
818 ++OldI;
819 }
820 }
821 }
822
823 // Drop all incompatible return attributes that cannot be applied to NewFunc
824 // during cloning, so as to allow instruction simplification to reason on the
825 // old state of the function. The original attributes are restored later.
826 AttributeMask IncompatibleAttrs =
828 AttributeList Attrs = NewFunc->getAttributes();
829 NewFunc->removeRetAttrs(IncompatibleAttrs);
830
831 // As phi-nodes have been now remapped, allow incremental simplification of
832 // newly-cloned instructions.
833 const DataLayout &DL = NewFunc->getParent()->getDataLayout();
834 for (const auto &BB : *OldFunc) {
835 for (const auto &I : BB) {
836 auto *NewI = dyn_cast_or_null<Instruction>(VMap.lookup(&I));
837 if (!NewI)
838 continue;
839
840 // Skip over non-intrinsic callsites, we don't want to remove any nodes
841 // from the CGSCC.
842 CallBase *CB = dyn_cast<CallBase>(NewI);
843 if (CB && CB->getCalledFunction() &&
845 continue;
846
847 if (Value *V = simplifyInstruction(NewI, DL)) {
848 NewI->replaceAllUsesWith(V);
849
850 if (isInstructionTriviallyDead(NewI)) {
851 NewI->eraseFromParent();
852 } else {
853 // Did not erase it? Restore the new instruction into VMap previously
854 // dropped by `ValueIsRAUWd`.
855 VMap[&I] = NewI;
856 }
857 }
858 }
859 }
860
861 // Restore attributes.
862 NewFunc->setAttributes(Attrs);
863
864 // Remap debug intrinsic operands now that all values have been mapped.
865 // Doing this now (late) preserves use-before-defs in debug intrinsics. If
866 // we didn't do this, ValueAsMetadata(use-before-def) operands would be
867 // replaced by empty metadata. This would signal later cleanup passes to
868 // remove the debug intrinsics, potentially causing incorrect locations.
869 for (const auto *DVI : DbgIntrinsics) {
870 if (DbgVariableIntrinsic *NewDVI =
871 cast_or_null<DbgVariableIntrinsic>(VMap.lookup(DVI)))
872 RemapInstruction(NewDVI, VMap,
873 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
874 TypeMapper, Materializer);
875 }
876
877 // Do the same for DbgVariableRecords, touching all the instructions in the
878 // cloned range of blocks.
879 Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
880 for (BasicBlock &BB : make_range(Begin, NewFunc->end())) {
881 for (Instruction &I : BB) {
882 RemapDbgVariableRecordRange(I.getModule(), I.getDbgRecordRange(), VMap,
883 ModuleLevelChanges ? RF_None
885 TypeMapper, Materializer);
886 }
887 }
888
889 // Simplify conditional branches and switches with a constant operand. We try
890 // to prune these out when cloning, but if the simplification required
891 // looking through PHI nodes, those are only available after forming the full
892 // basic block. That may leave some here, and we still want to prune the dead
893 // code as early as possible.
894 for (BasicBlock &BB : make_range(Begin, NewFunc->end()))
896
897 // Some blocks may have become unreachable as a result. Find and delete them.
898 {
899 SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
901 Worklist.push_back(&*Begin);
902 while (!Worklist.empty()) {
903 BasicBlock *BB = Worklist.pop_back_val();
904 if (ReachableBlocks.insert(BB).second)
905 append_range(Worklist, successors(BB));
906 }
907
908 SmallVector<BasicBlock *, 16> UnreachableBlocks;
909 for (BasicBlock &BB : make_range(Begin, NewFunc->end()))
910 if (!ReachableBlocks.contains(&BB))
911 UnreachableBlocks.push_back(&BB);
912 DeleteDeadBlocks(UnreachableBlocks);
913 }
914
915 // Now that the inlined function body has been fully constructed, go through
916 // and zap unconditional fall-through branches. This happens all the time when
917 // specializing code: code specialization turns conditional branches into
918 // uncond branches, and this code folds them.
919 Function::iterator I = Begin;
920 while (I != NewFunc->end()) {
921 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
922 if (!BI || BI->isConditional()) {
923 ++I;
924 continue;
925 }
926
927 BasicBlock *Dest = BI->getSuccessor(0);
928 if (!Dest->getSinglePredecessor()) {
929 ++I;
930 continue;
931 }
932
933 // We shouldn't be able to get single-entry PHI nodes here, as instsimplify
934 // above should have zapped all of them..
935 assert(!isa<PHINode>(Dest->begin()));
936
937 // We know all single-entry PHI nodes in the inlined function have been
938 // removed, so we just need to splice the blocks.
939 BI->eraseFromParent();
940
941 // Make all PHI nodes that referred to Dest now refer to I as their source.
942 Dest->replaceAllUsesWith(&*I);
943
944 // Move all the instructions in the succ to the pred.
945 I->splice(I->end(), Dest);
946
947 // Remove the dest block.
948 Dest->eraseFromParent();
949
950 // Do not increment I, iteratively merge all things this block branches to.
951 }
952
953 // Make a final pass over the basic blocks from the old function to gather
954 // any return instructions which survived folding. We have to do this here
955 // because we can iteratively remove and merge returns above.
956 for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
957 E = NewFunc->end();
958 I != E; ++I)
959 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
960 Returns.push_back(RI);
961}
962
963/// This works exactly like CloneFunctionInto,
964/// except that it does some simple constant prop and DCE on the fly. The
965/// effect of this is to copy significantly less code in cases where (for
966/// example) a function call with constant arguments is inlined, and those
967/// constant arguments cause a significant amount of code in the callee to be
968/// dead. Since this doesn't produce an exact copy of the input, it can't be
969/// used for things like CloneFunction or CloneModule.
971 Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap,
972 bool ModuleLevelChanges, SmallVectorImpl<ReturnInst *> &Returns,
973 const char *NameSuffix, ClonedCodeInfo *CodeInfo) {
974 CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
975 ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
976}
977
978/// Remaps instructions in \p Blocks using the mapping in \p VMap.
980 ValueToValueMapTy &VMap) {
981 // Rewrite the code to refer to itself.
982 for (auto *BB : Blocks) {
983 for (auto &Inst : *BB) {
985 Inst.getModule(), Inst.getDbgRecordRange(), VMap,
987 RemapInstruction(&Inst, VMap,
989 }
990 }
991}
992
993/// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
994/// Blocks.
995///
996/// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
997/// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
999 Loop *OrigLoop, ValueToValueMapTy &VMap,
1000 const Twine &NameSuffix, LoopInfo *LI,
1001 DominatorTree *DT,
1003 Function *F = OrigLoop->getHeader()->getParent();
1004 Loop *ParentLoop = OrigLoop->getParentLoop();
1006
1007 Loop *NewLoop = LI->AllocateLoop();
1008 LMap[OrigLoop] = NewLoop;
1009 if (ParentLoop)
1010 ParentLoop->addChildLoop(NewLoop);
1011 else
1012 LI->addTopLevelLoop(NewLoop);
1013
1014 BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
1015 assert(OrigPH && "No preheader");
1016 BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
1017 // To rename the loop PHIs.
1018 VMap[OrigPH] = NewPH;
1019 Blocks.push_back(NewPH);
1020
1021 // Update LoopInfo.
1022 if (ParentLoop)
1023 ParentLoop->addBasicBlockToLoop(NewPH, *LI);
1024
1025 // Update DominatorTree.
1026 DT->addNewBlock(NewPH, LoopDomBB);
1027
1028 for (Loop *CurLoop : OrigLoop->getLoopsInPreorder()) {
1029 Loop *&NewLoop = LMap[CurLoop];
1030 if (!NewLoop) {
1031 NewLoop = LI->AllocateLoop();
1032
1033 // Establish the parent/child relationship.
1034 Loop *OrigParent = CurLoop->getParentLoop();
1035 assert(OrigParent && "Could not find the original parent loop");
1036 Loop *NewParentLoop = LMap[OrigParent];
1037 assert(NewParentLoop && "Could not find the new parent loop");
1038
1039 NewParentLoop->addChildLoop(NewLoop);
1040 }
1041 }
1042
1043 for (BasicBlock *BB : OrigLoop->getBlocks()) {
1044 Loop *CurLoop = LI->getLoopFor(BB);
1045 Loop *&NewLoop = LMap[CurLoop];
1046 assert(NewLoop && "Expecting new loop to be allocated");
1047
1048 BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
1049 VMap[BB] = NewBB;
1050
1051 // Update LoopInfo.
1052 NewLoop->addBasicBlockToLoop(NewBB, *LI);
1053
1054 // Add DominatorTree node. After seeing all blocks, update to correct
1055 // IDom.
1056 DT->addNewBlock(NewBB, NewPH);
1057
1058 Blocks.push_back(NewBB);
1059 }
1060
1061 for (BasicBlock *BB : OrigLoop->getBlocks()) {
1062 // Update loop headers.
1063 Loop *CurLoop = LI->getLoopFor(BB);
1064 if (BB == CurLoop->getHeader())
1065 LMap[CurLoop]->moveToHeader(cast<BasicBlock>(VMap[BB]));
1066
1067 // Update DominatorTree.
1068 BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
1069 DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
1070 cast<BasicBlock>(VMap[IDomBB]));
1071 }
1072
1073 // Move them physically from the end of the block list.
1074 F->splice(Before->getIterator(), F, NewPH->getIterator());
1075 F->splice(Before->getIterator(), F, NewLoop->getHeader()->getIterator(),
1076 F->end());
1077
1078 return NewLoop;
1079}
1080
1081/// Duplicate non-Phi instructions from the beginning of block up to
1082/// StopAt instruction into a split block between BB and its predecessor.
1084 BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt,
1085 ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU) {
1086
1087 assert(count(successors(PredBB), BB) == 1 &&
1088 "There must be a single edge between PredBB and BB!");
1089 // We are going to have to map operands from the original BB block to the new
1090 // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
1091 // account for entry from PredBB.
1092 BasicBlock::iterator BI = BB->begin();
1093 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
1094 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
1095
1096 BasicBlock *NewBB = SplitEdge(PredBB, BB);
1097 NewBB->setName(PredBB->getName() + ".split");
1098 Instruction *NewTerm = NewBB->getTerminator();
1099
1100 // FIXME: SplitEdge does not yet take a DTU, so we include the split edge
1101 // in the update set here.
1102 DTU.applyUpdates({{DominatorTree::Delete, PredBB, BB},
1103 {DominatorTree::Insert, PredBB, NewBB},
1104 {DominatorTree::Insert, NewBB, BB}});
1105
1106 // Clone the non-phi instructions of BB into NewBB, keeping track of the
1107 // mapping and using it to remap operands in the cloned instructions.
1108 // Stop once we see the terminator too. This covers the case where BB's
1109 // terminator gets replaced and StopAt == BB's terminator.
1110 for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) {
1111 Instruction *New = BI->clone();
1112 New->setName(BI->getName());
1113 New->insertBefore(NewTerm);
1114 New->cloneDebugInfoFrom(&*BI);
1115 ValueMapping[&*BI] = New;
1116
1117 // Remap operands to patch up intra-block references.
1118 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
1119 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
1120 auto I = ValueMapping.find(Inst);
1121 if (I != ValueMapping.end())
1122 New->setOperand(i, I->second);
1123 }
1124
1125 // Remap debug variable operands.
1126 remapDebugVariable(ValueMapping, New);
1127 }
1128
1129 return NewBB;
1130}
1131
1133 DenseMap<MDNode *, MDNode *> &ClonedScopes,
1134 StringRef Ext, LLVMContext &Context) {
1135 MDBuilder MDB(Context);
1136
1137 for (auto *ScopeList : NoAliasDeclScopes) {
1138 for (const auto &MDOperand : ScopeList->operands()) {
1139 if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) {
1140 AliasScopeNode SNANode(MD);
1141
1142 std::string Name;
1143 auto ScopeName = SNANode.getName();
1144 if (!ScopeName.empty())
1145 Name = (Twine(ScopeName) + ":" + Ext).str();
1146 else
1147 Name = std::string(Ext);
1148
1149 MDNode *NewScope = MDB.createAnonymousAliasScope(
1150 const_cast<MDNode *>(SNANode.getDomain()), Name);
1151 ClonedScopes.insert(std::make_pair(MD, NewScope));
1152 }
1153 }
1154 }
1155}
1156
1158 const DenseMap<MDNode *, MDNode *> &ClonedScopes,
1159 LLVMContext &Context) {
1160 auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * {
1161 bool NeedsReplacement = false;
1162 SmallVector<Metadata *, 8> NewScopeList;
1163 for (const auto &MDOp : ScopeList->operands()) {
1164 if (MDNode *MD = dyn_cast<MDNode>(MDOp)) {
1165 if (auto *NewMD = ClonedScopes.lookup(MD)) {
1166 NewScopeList.push_back(NewMD);
1167 NeedsReplacement = true;
1168 continue;
1169 }
1170 NewScopeList.push_back(MD);
1171 }
1172 }
1173 if (NeedsReplacement)
1174 return MDNode::get(Context, NewScopeList);
1175 return nullptr;
1176 };
1177
1178 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
1179 if (auto *NewScopeList = CloneScopeList(Decl->getScopeList()))
1180 Decl->setScopeList(NewScopeList);
1181
1182 auto replaceWhenNeeded = [&](unsigned MD_ID) {
1183 if (const MDNode *CSNoAlias = I->getMetadata(MD_ID))
1184 if (auto *NewScopeList = CloneScopeList(CSNoAlias))
1185 I->setMetadata(MD_ID, NewScopeList);
1186 };
1187 replaceWhenNeeded(LLVMContext::MD_noalias);
1188 replaceWhenNeeded(LLVMContext::MD_alias_scope);
1189}
1190
1192 ArrayRef<BasicBlock *> NewBlocks,
1193 LLVMContext &Context, StringRef Ext) {
1194 if (NoAliasDeclScopes.empty())
1195 return;
1196
1197 DenseMap<MDNode *, MDNode *> ClonedScopes;
1198 LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
1199 << NoAliasDeclScopes.size() << " node(s)\n");
1200
1201 cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
1202 // Identify instructions using metadata that needs adaptation
1203 for (BasicBlock *NewBlock : NewBlocks)
1204 for (Instruction &I : *NewBlock)
1205 adaptNoAliasScopes(&I, ClonedScopes, Context);
1206}
1207
1209 Instruction *IStart, Instruction *IEnd,
1210 LLVMContext &Context, StringRef Ext) {
1211 if (NoAliasDeclScopes.empty())
1212 return;
1213
1214 DenseMap<MDNode *, MDNode *> ClonedScopes;
1215 LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning "
1216 << NoAliasDeclScopes.size() << " node(s)\n");
1217
1218 cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context);
1219 // Identify instructions using metadata that needs adaptation
1220 assert(IStart->getParent() == IEnd->getParent() && "different basic block ?");
1221 auto ItStart = IStart->getIterator();
1222 auto ItEnd = IEnd->getIterator();
1223 ++ItEnd; // IEnd is included, increment ItEnd to get the end of the range
1224 for (auto &I : llvm::make_range(ItStart, ItEnd))
1225 adaptNoAliasScopes(&I, ClonedScopes, Context);
1226}
1227
1229 ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
1230 for (BasicBlock *BB : BBs)
1231 for (Instruction &I : *BB)
1232 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
1233 NoAliasDeclScopes.push_back(Decl->getScopeList());
1234}
1235
1238 SmallVectorImpl<MDNode *> &NoAliasDeclScopes) {
1239 for (Instruction &I : make_range(Start, End))
1240 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
1241 NoAliasDeclScopes.push_back(Decl->getScopeList());
1242}
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...
#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.
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:1494
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1742
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: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: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:787
BasicBlockListType::iterator iterator
Definition: Function.h:68
void setPrefixData(Constant *PrefixData)
Definition: Function.cpp:1924
const BasicBlock & front() const
Definition: Function.h:810
iterator_range< arg_iterator > args()
Definition: Function.h:842
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:864
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition: Function.h:855
Constant * getPrologueData() const
Get the prologue data associated with this function.
Definition: Function.cpp:1929
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1909
void setPersonalityFn(Constant *Fn)
Definition: Function.cpp:1914
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:340
arg_iterator arg_begin()
Definition: Function.h:818
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition: Function.h:237
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:851
void setPrologueData(Constant *PrologueData)
Definition: Function.cpp:1934
void removeRetAttrs(const AttributeMask &Attrs)
removes the attributes from the return value list of attributes.
Definition: Function.cpp:655
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:1919
iterator end()
Definition: Function.h:805
bool hasPrologueData() const
Check whether this function has prologue data.
Definition: Function.h:873
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:797
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: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.
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:167
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:1827
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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:1315
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:1471
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:2073
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:3688
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: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: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.
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.
#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:114
enum llvm::Intrinsic::IITDescriptor::IITDescriptorKind Kind
ArgKind getArgumentKind() const
Definition: Intrinsics.h:169