2002-11-20 06:04:49 +08:00
|
|
|
//===- CloneFunction.cpp - Clone a function into another function ---------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-20 06:04:49 +08:00
|
|
|
//
|
|
|
|
// This file implements the CloneFunctionInto interface, which is used as the
|
|
|
|
// low-level function cloner. This is used by the CloneFunction and function
|
|
|
|
// inliner to do the dirty work of copying the body of a function around.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-30 03:03:54 +08:00
|
|
|
|
2016-08-04 12:24:02 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2015-07-11 02:55:09 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2018-06-05 05:23:21 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-03-06 08:46:21 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2014-03-12 22:42:51 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-03-28 16:38:27 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2010-08-25 02:50:07 +08:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2007-02-03 08:08:31 +08:00
|
|
|
#include <map>
|
2004-01-09 14:12:26 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2015-03-11 02:41:22 +08:00
|
|
|
/// See comments in Cloning.h.
|
2017-06-02 07:02:12 +08:00
|
|
|
BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
|
2010-01-28 03:58:47 +08:00
|
|
|
const Twine &NameSuffix, Function *F,
|
2017-06-02 07:02:12 +08:00
|
|
|
ClonedCodeInfo *CodeInfo,
|
|
|
|
DebugInfoFinder *DIFinder) {
|
2017-05-10 03:47:37 +08:00
|
|
|
DenseMap<const MDNode *, MDNode *> Cache;
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F);
|
2018-04-14 05:23:11 +08:00
|
|
|
if (BB->hasName())
|
|
|
|
NewBB->setName(BB->getName() + NameSuffix);
|
2003-04-18 11:50:09 +08:00
|
|
|
|
2006-01-14 02:39:17 +08:00
|
|
|
bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
|
[cloning] Do not duplicate types when cloning functions
Summary:
This is an addon to the change rl304488 cloning fixes. (Originally rl304226 reverted rl304228 and reapplied rl304488 https://reviews.llvm.org/D33655)
rl304488 works great when DILocalVariables that comes from the inlined function has a 'unique-ed' type, but,
in the case when the variable type is distinct we will create a second DILocalVariable in the scope of the original function that was inlined.
Consider cloning of the following function:
```
define private void @f() !dbg !5 {
%1 = alloca i32, !dbg !11
call void @llvm.dbg.declare(metadata i32* %1, metadata !14, metadata !12), !dbg !18
ret void, !dbg !18
}
!14 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !17) ; came from an inlined function
!15 = distinct !DISubprogram(name: "inlined", linkageName: "inlined", scope: null, file: !6, line: 8, type: !7, isLocal: true, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !0, variables: !16)
!16 = !{!14}
!17 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
```
Without this fix, when function 'f' is cloned, we will create another DILocalVariable for "inlined", due to its type being distinct.
```
define private void @f.1() !dbg !23 {
%1 = alloca i32, !dbg !26
call void @llvm.dbg.declare(metadata i32* %1, metadata !28, metadata !12), !dbg !30
ret void, !dbg !30
}
!14 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !17)
!15 = distinct !DISubprogram(name: "inlined", linkageName: "inlined", scope: null, file: !6, line: 8, type: !7, isLocal: true, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !0, variables: !16)
!16 = !{!14}
!17 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
;
!28 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !29) ; OOPS second DILocalVariable
!29 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
```
Now we have two DILocalVariable for "inlined" within the same scope. This result in assert in AsmPrinter/DwarfDebug.h:131: void llvm::DbgVariable::addMMIEntry(const llvm::DbgVariable &): Assertion `V.Var == Var && "conflicting variable"' failed.
(Full example: See: https://bugs.llvm.org/show_bug.cgi?id=33492)
In this change we prevent duplication of types so that when a metadata for DILocalVariable is cloned it will get uniqued to the same metadate node as an original variable.
Reviewers: loladiro, dblaikie, aprantl, echristo
Reviewed By: loladiro
Subscribers: EricWF, llvm-commits
Differential Revision: https://reviews.llvm.org/D35106
llvm-svn: 307418
2017-07-08 02:24:20 +08:00
|
|
|
Module *TheModule = F ? F->getParent() : nullptr;
|
|
|
|
|
2006-01-14 02:39:17 +08:00
|
|
|
// Loop over all instructions, and copy them over.
|
2018-04-14 05:23:11 +08:00
|
|
|
for (const Instruction &I : *BB) {
|
|
|
|
if (DIFinder && TheModule)
|
|
|
|
DIFinder->processInstruction(*TheModule, I);
|
2017-06-02 07:02:12 +08:00
|
|
|
|
2018-04-14 05:23:11 +08:00
|
|
|
Instruction *NewInst = I.clone();
|
|
|
|
if (I.hasName())
|
|
|
|
NewInst->setName(I.getName() + NameSuffix);
|
2003-04-18 11:50:09 +08:00
|
|
|
NewBB->getInstList().push_back(NewInst);
|
2018-04-14 05:23:11 +08:00
|
|
|
VMap[&I] = NewInst; // Add instruction map to value.
|
2015-10-13 10:39:05 +08:00
|
|
|
|
2018-04-14 05:23:11 +08:00
|
|
|
hasCalls |= (isa<CallInst>(I) && !isa<DbgInfoIntrinsic>(I));
|
|
|
|
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
|
2006-01-14 02:39:17 +08:00
|
|
|
if (isa<ConstantInt>(AI->getArraySize()))
|
|
|
|
hasStaticAllocas = true;
|
|
|
|
else
|
|
|
|
hasDynamicAllocas = true;
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 05:23:11 +08:00
|
|
|
|
2006-01-14 02:39:17 +08:00
|
|
|
if (CodeInfo) {
|
|
|
|
CodeInfo->ContainsCalls |= hasCalls;
|
|
|
|
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
|
2018-04-14 05:23:11 +08:00
|
|
|
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
|
2007-03-23 00:38:57 +08:00
|
|
|
BB != &BB->getParent()->getEntryBlock();
|
2003-04-18 11:50:09 +08:00
|
|
|
}
|
|
|
|
return NewBB;
|
|
|
|
}
|
|
|
|
|
2002-03-30 03:03:54 +08:00
|
|
|
// Clone OldFunc into NewFunc, transforming the old arguments into references to
|
2010-08-26 23:41:53 +08:00
|
|
|
// VMap values.
|
2002-03-30 03:03:54 +08:00
|
|
|
//
|
2004-01-09 14:12:26 +08:00
|
|
|
void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
|
2010-06-24 08:00:42 +08:00
|
|
|
ValueToValueMapTy &VMap,
|
2010-08-26 23:41:53 +08:00
|
|
|
bool ModuleLevelChanges,
|
2009-08-27 12:02:30 +08:00
|
|
|
SmallVectorImpl<ReturnInst*> &Returns,
|
2011-12-23 10:18:32 +08:00
|
|
|
const char *NameSuffix, ClonedCodeInfo *CodeInfo,
|
2013-05-28 23:17:05 +08:00
|
|
|
ValueMapTypeRemapper *TypeMapper,
|
|
|
|
ValueMaterializer *Materializer) {
|
2002-11-20 05:54:07 +08:00
|
|
|
assert(NameSuffix && "NameSuffix cannot be null!");
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 06:54:01 +08:00
|
|
|
#ifndef NDEBUG
|
2015-10-13 10:39:05 +08:00
|
|
|
for (const Argument &I : OldFunc->args())
|
|
|
|
assert(VMap.count(&I) && "No mapping from source argument specified!");
|
2002-11-20 06:54:01 +08:00
|
|
|
#endif
|
2002-03-30 03:03:54 +08:00
|
|
|
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
// Copy all attributes other than those stored in the AttributeList. We need
|
|
|
|
// to remap the parameter indices of the AttributeList.
|
|
|
|
AttributeList NewAttrs = NewFunc->getAttributes();
|
2014-03-27 06:26:35 +08:00
|
|
|
NewFunc->copyAttributesFrom(OldFunc);
|
|
|
|
NewFunc->setAttributes(NewAttrs);
|
|
|
|
|
2015-11-16 13:13:30 +08:00
|
|
|
// Fix up the personality function that got copied over.
|
|
|
|
if (OldFunc->hasPersonalityFn())
|
|
|
|
NewFunc->setPersonalityFn(
|
|
|
|
MapValue(OldFunc->getPersonalityFn(), VMap,
|
|
|
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
|
|
|
|
TypeMapper, Materializer));
|
|
|
|
|
2017-04-13 08:58:09 +08:00
|
|
|
SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size());
|
Rename AttributeSet to AttributeList
Summary:
This class is a list of AttributeSetNodes corresponding the function
prototype of a call or function declaration. This class used to be
called ParamAttrListPtr, then AttrListPtr, then AttributeSet. It is
typically accessed by parameter and return value index, so
"AttributeList" seems like a more intuitive name.
Rename AttributeSetImpl to AttributeListImpl to follow suit.
It's useful to rename this class so that we can rename AttributeSetNode
to AttributeSet later. AttributeSet is the set of attributes that apply
to a single function, argument, or return value.
Reviewers: sanjoy, javed.absar, chandlerc, pete
Reviewed By: pete
Subscribers: pete, jholewinski, arsenm, dschuff, mehdi_amini, jfb, nhaehnle, sbc100, void, llvm-commits
Differential Revision: https://reviews.llvm.org/D31102
llvm-svn: 298393
2017-03-22 00:57:19 +08:00
|
|
|
AttributeList OldAttrs = OldFunc->getAttributes();
|
2017-04-11 07:31:05 +08:00
|
|
|
|
2013-04-10 18:37:38 +08:00
|
|
|
// Clone any argument attributes that are present in the VMap.
|
2017-04-13 08:58:09 +08:00
|
|
|
for (const Argument &OldArg : OldFunc->args()) {
|
2014-03-27 06:26:35 +08:00
|
|
|
if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) {
|
2017-04-13 08:58:09 +08:00
|
|
|
NewArgAttrs[NewArg->getArgNo()] =
|
2017-04-14 07:12:13 +08:00
|
|
|
OldAttrs.getParamAttributes(OldArg.getArgNo());
|
2013-04-10 18:37:38 +08:00
|
|
|
}
|
2017-04-13 08:58:09 +08:00
|
|
|
}
|
2008-10-08 02:08:38 +08:00
|
|
|
|
2017-04-13 08:58:09 +08:00
|
|
|
NewFunc->setAttributes(
|
|
|
|
AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(),
|
|
|
|
OldAttrs.getRetAttributes(), NewArgAttrs));
|
2008-03-24 00:03:00 +08:00
|
|
|
|
2017-06-02 07:02:12 +08:00
|
|
|
bool MustCloneSP =
|
|
|
|
OldFunc->getParent() && OldFunc->getParent() == NewFunc->getParent();
|
|
|
|
DISubprogram *SP = OldFunc->getSubprogram();
|
|
|
|
if (SP) {
|
|
|
|
assert(!MustCloneSP || ModuleLevelChanges);
|
|
|
|
// Add mappings for some DebugInfo nodes that we don't want duplicated
|
|
|
|
// even if they're distinct.
|
|
|
|
auto &MD = VMap.MD();
|
|
|
|
MD[SP->getUnit()].reset(SP->getUnit());
|
|
|
|
MD[SP->getType()].reset(SP->getType());
|
|
|
|
MD[SP->getFile()].reset(SP->getFile());
|
|
|
|
// If we're not cloning into the same module, no need to clone the
|
|
|
|
// subprogram
|
|
|
|
if (!MustCloneSP)
|
|
|
|
MD[SP].reset(SP);
|
|
|
|
}
|
|
|
|
|
2016-03-31 06:05:13 +08:00
|
|
|
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
|
|
|
|
OldFunc->getAllMetadata(MDs);
|
2017-05-10 03:47:37 +08:00
|
|
|
for (auto MD : MDs) {
|
2017-06-02 07:02:12 +08:00
|
|
|
NewFunc->addMetadata(
|
|
|
|
MD.first,
|
|
|
|
*MapMetadata(MD.second, VMap,
|
|
|
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
|
|
|
|
TypeMapper, Materializer));
|
2017-05-10 03:47:37 +08:00
|
|
|
}
|
2016-03-31 06:05:13 +08:00
|
|
|
|
2017-06-02 07:02:12 +08:00
|
|
|
// When we remap instructions, we want to avoid duplicating inlined
|
|
|
|
// DISubprograms, so record all subprograms we find as we duplicate
|
|
|
|
// instructions and then freeze them in the MD map.
|
[cloning] Do not duplicate types when cloning functions
Summary:
This is an addon to the change rl304488 cloning fixes. (Originally rl304226 reverted rl304228 and reapplied rl304488 https://reviews.llvm.org/D33655)
rl304488 works great when DILocalVariables that comes from the inlined function has a 'unique-ed' type, but,
in the case when the variable type is distinct we will create a second DILocalVariable in the scope of the original function that was inlined.
Consider cloning of the following function:
```
define private void @f() !dbg !5 {
%1 = alloca i32, !dbg !11
call void @llvm.dbg.declare(metadata i32* %1, metadata !14, metadata !12), !dbg !18
ret void, !dbg !18
}
!14 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !17) ; came from an inlined function
!15 = distinct !DISubprogram(name: "inlined", linkageName: "inlined", scope: null, file: !6, line: 8, type: !7, isLocal: true, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !0, variables: !16)
!16 = !{!14}
!17 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
```
Without this fix, when function 'f' is cloned, we will create another DILocalVariable for "inlined", due to its type being distinct.
```
define private void @f.1() !dbg !23 {
%1 = alloca i32, !dbg !26
call void @llvm.dbg.declare(metadata i32* %1, metadata !28, metadata !12), !dbg !30
ret void, !dbg !30
}
!14 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !17)
!15 = distinct !DISubprogram(name: "inlined", linkageName: "inlined", scope: null, file: !6, line: 8, type: !7, isLocal: true, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !0, variables: !16)
!16 = !{!14}
!17 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
;
!28 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !29) ; OOPS second DILocalVariable
!29 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
```
Now we have two DILocalVariable for "inlined" within the same scope. This result in assert in AsmPrinter/DwarfDebug.h:131: void llvm::DbgVariable::addMMIEntry(const llvm::DbgVariable &): Assertion `V.Var == Var && "conflicting variable"' failed.
(Full example: See: https://bugs.llvm.org/show_bug.cgi?id=33492)
In this change we prevent duplication of types so that when a metadata for DILocalVariable is cloned it will get uniqued to the same metadate node as an original variable.
Reviewers: loladiro, dblaikie, aprantl, echristo
Reviewed By: loladiro
Subscribers: EricWF, llvm-commits
Differential Revision: https://reviews.llvm.org/D35106
llvm-svn: 307418
2017-07-08 02:24:20 +08:00
|
|
|
// We also record information about dbg.value and dbg.declare to avoid
|
|
|
|
// duplicating the types.
|
2017-06-02 07:02:12 +08:00
|
|
|
DebugInfoFinder DIFinder;
|
|
|
|
|
2002-03-30 03:03:54 +08:00
|
|
|
// Loop over all of the basic blocks in the function, cloning them as
|
2002-11-20 05:54:07 +08:00
|
|
|
// appropriate. Note that we save BE this way in order to handle cloning of
|
|
|
|
// recursive functions into themselves.
|
2002-03-30 03:03:54 +08:00
|
|
|
//
|
|
|
|
for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
|
|
|
|
BI != BE; ++BI) {
|
2002-06-26 00:12:52 +08:00
|
|
|
const BasicBlock &BB = *BI;
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2003-04-18 11:50:09 +08:00
|
|
|
// Create a new basic block and copy instructions into it!
|
2017-06-02 07:02:12 +08:00
|
|
|
BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo,
|
[DebugInfo][OPT] Fixing a couple of DI duplication bugs of CloneModule
As demonstrated by the regression tests added in this patch, the
following cases are valid cases:
1. A Function with no DISubprogram attached, but various debug info
related to its instructions, coming, for instance, from an inlined
function, also defined somewhere else in the same module;
2. ... or coming exclusively from the functions inlined and eliminated
from the module entirely.
The ValueMap shared between CloneFunctionInto calls within CloneModule
needs to contain identity mappings for all of the DISubprogram's to
prevent them from being duplicated by MapMetadata / RemapInstruction
calls, this is achieved via DebugInfoFinder collecting all the
DISubprogram's. However, CloneFunctionInto was missing calls into
DebugInfoFinder for functions w/o DISubprogram's attached, but still
referring DISubprogram's from within (case 1). This patch fixes that.
The fix above, however, exposes another issue: if a module contains a
DISubprogram referenced only indirectly from other debug info
metadata, but not attached to any Function defined within the module
(case 2), cloning such a module causes a DICompileUnit duplication: it
will be moved in indirecty via a DISubprogram by DebugInfoFinder first
(because of the first bug fix described above), without being
self-mapped within the shared ValueMap, and then will be copied during
named metadata cloning. So this patch makes sure DebugInfoFinder
visits DICompileUnit's referenced from DISubprogram's as it goes w/o
re-processing llvm.dbg.cu list over and over again for every function
cloned, and makes sure that CloneFunctionInto self-maps
DICompileUnit's referenced from the entire function, not just its own
DISubprogram attached that may also be missing.
The most convenient way of tesing CloneModule I found is to rely on
CloneModule call from `opt -run-twice`, instead of writing tedious
unit tests. That feature has a couple of properties that makes it hard
to use for this purpose though:
1. CloneModule doesn't copy source filename, making `opt -run-twice`
report it as a difference.
2. `opt -run-twice` does the second run on the original module, not
its clone, making the result of cloning completely invisible in opt's
actual output with and without `-run-twice` both, which directly
contradicts `opt -run-twice`s own error message.
This patch fixes this as well.
Reviewed By: aprantl
Reviewers: loladiro, GorNishanov, espindola, echristo, dexonsmith
Subscribers: vsk, debug-info, JDevlieghere, llvm-commits
Differential Revision: https://reviews.llvm.org/D45593
llvm-svn: 330069
2018-04-14 05:22:24 +08:00
|
|
|
ModuleLevelChanges ? &DIFinder : nullptr);
|
2002-03-30 03:03:54 +08:00
|
|
|
|
2011-10-22 04:45:19 +08:00
|
|
|
// Add basic block mapping.
|
|
|
|
VMap[&BB] = CBB;
|
|
|
|
|
|
|
|
// It is only legal to clone a function if a block address within that
|
|
|
|
// function is never referenced outside of the function. Given that, we
|
|
|
|
// want to map block addresses from the old function to block addresses in
|
|
|
|
// the clone. (This is different from the generic ValueMapper
|
|
|
|
// implementation, which generates an invalid blockaddress when
|
|
|
|
// cloning a function.)
|
|
|
|
if (BB.hasAddressTaken()) {
|
|
|
|
Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
|
|
|
|
const_cast<BasicBlock*>(&BB));
|
2015-12-10 04:33:45 +08:00
|
|
|
VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB);
|
2011-10-22 04:45:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note return instructions for the caller.
|
2002-11-20 05:54:07 +08:00
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
|
|
|
|
Returns.push_back(RI);
|
2002-03-30 03:03:54 +08:00
|
|
|
}
|
|
|
|
|
2018-04-14 05:23:11 +08:00
|
|
|
for (DISubprogram *ISP : DIFinder.subprograms())
|
|
|
|
if (ISP != SP)
|
2017-06-02 07:02:12 +08:00
|
|
|
VMap.MD()[ISP].reset(ISP);
|
|
|
|
|
[DebugInfo][OPT] Fixing a couple of DI duplication bugs of CloneModule
As demonstrated by the regression tests added in this patch, the
following cases are valid cases:
1. A Function with no DISubprogram attached, but various debug info
related to its instructions, coming, for instance, from an inlined
function, also defined somewhere else in the same module;
2. ... or coming exclusively from the functions inlined and eliminated
from the module entirely.
The ValueMap shared between CloneFunctionInto calls within CloneModule
needs to contain identity mappings for all of the DISubprogram's to
prevent them from being duplicated by MapMetadata / RemapInstruction
calls, this is achieved via DebugInfoFinder collecting all the
DISubprogram's. However, CloneFunctionInto was missing calls into
DebugInfoFinder for functions w/o DISubprogram's attached, but still
referring DISubprogram's from within (case 1). This patch fixes that.
The fix above, however, exposes another issue: if a module contains a
DISubprogram referenced only indirectly from other debug info
metadata, but not attached to any Function defined within the module
(case 2), cloning such a module causes a DICompileUnit duplication: it
will be moved in indirecty via a DISubprogram by DebugInfoFinder first
(because of the first bug fix described above), without being
self-mapped within the shared ValueMap, and then will be copied during
named metadata cloning. So this patch makes sure DebugInfoFinder
visits DICompileUnit's referenced from DISubprogram's as it goes w/o
re-processing llvm.dbg.cu list over and over again for every function
cloned, and makes sure that CloneFunctionInto self-maps
DICompileUnit's referenced from the entire function, not just its own
DISubprogram attached that may also be missing.
The most convenient way of tesing CloneModule I found is to rely on
CloneModule call from `opt -run-twice`, instead of writing tedious
unit tests. That feature has a couple of properties that makes it hard
to use for this purpose though:
1. CloneModule doesn't copy source filename, making `opt -run-twice`
report it as a difference.
2. `opt -run-twice` does the second run on the original module, not
its clone, making the result of cloning completely invisible in opt's
actual output with and without `-run-twice` both, which directly
contradicts `opt -run-twice`s own error message.
This patch fixes this as well.
Reviewed By: aprantl
Reviewers: loladiro, GorNishanov, espindola, echristo, dexonsmith
Subscribers: vsk, debug-info, JDevlieghere, llvm-commits
Differential Revision: https://reviews.llvm.org/D45593
llvm-svn: 330069
2018-04-14 05:22:24 +08:00
|
|
|
for (DICompileUnit *CU : DIFinder.compile_units())
|
|
|
|
VMap.MD()[CU].reset(CU);
|
|
|
|
|
2018-04-14 05:23:11 +08:00
|
|
|
for (DIType *Type : DIFinder.types())
|
[cloning] Do not duplicate types when cloning functions
Summary:
This is an addon to the change rl304488 cloning fixes. (Originally rl304226 reverted rl304228 and reapplied rl304488 https://reviews.llvm.org/D33655)
rl304488 works great when DILocalVariables that comes from the inlined function has a 'unique-ed' type, but,
in the case when the variable type is distinct we will create a second DILocalVariable in the scope of the original function that was inlined.
Consider cloning of the following function:
```
define private void @f() !dbg !5 {
%1 = alloca i32, !dbg !11
call void @llvm.dbg.declare(metadata i32* %1, metadata !14, metadata !12), !dbg !18
ret void, !dbg !18
}
!14 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !17) ; came from an inlined function
!15 = distinct !DISubprogram(name: "inlined", linkageName: "inlined", scope: null, file: !6, line: 8, type: !7, isLocal: true, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !0, variables: !16)
!16 = !{!14}
!17 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
```
Without this fix, when function 'f' is cloned, we will create another DILocalVariable for "inlined", due to its type being distinct.
```
define private void @f.1() !dbg !23 {
%1 = alloca i32, !dbg !26
call void @llvm.dbg.declare(metadata i32* %1, metadata !28, metadata !12), !dbg !30
ret void, !dbg !30
}
!14 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !17)
!15 = distinct !DISubprogram(name: "inlined", linkageName: "inlined", scope: null, file: !6, line: 8, type: !7, isLocal: true, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !0, variables: !16)
!16 = !{!14}
!17 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
;
!28 = !DILocalVariable(name: "inlined", scope: !15, file: !6, line: 5, type: !29) ; OOPS second DILocalVariable
!29 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "some_struct", size: 32, align: 32)
```
Now we have two DILocalVariable for "inlined" within the same scope. This result in assert in AsmPrinter/DwarfDebug.h:131: void llvm::DbgVariable::addMMIEntry(const llvm::DbgVariable &): Assertion `V.Var == Var && "conflicting variable"' failed.
(Full example: See: https://bugs.llvm.org/show_bug.cgi?id=33492)
In this change we prevent duplication of types so that when a metadata for DILocalVariable is cloned it will get uniqued to the same metadate node as an original variable.
Reviewers: loladiro, dblaikie, aprantl, echristo
Reviewed By: loladiro
Subscribers: EricWF, llvm-commits
Differential Revision: https://reviews.llvm.org/D35106
llvm-svn: 307418
2017-07-08 02:24:20 +08:00
|
|
|
VMap.MD()[Type].reset(Type);
|
|
|
|
|
2005-04-22 07:48:37 +08:00
|
|
|
// Loop over all of the instructions in the function, fixing up operand
|
2010-06-24 07:55:51 +08:00
|
|
|
// references as we go. This uses VMap to do all the hard work.
|
2015-10-13 10:39:05 +08:00
|
|
|
for (Function::iterator BB =
|
|
|
|
cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(),
|
|
|
|
BE = NewFunc->end();
|
|
|
|
BB != BE; ++BB)
|
2002-03-30 03:03:54 +08:00
|
|
|
// Loop over all instructions, fixing each one as we find it...
|
2015-10-13 10:39:05 +08:00
|
|
|
for (Instruction &II : *BB)
|
|
|
|
RemapInstruction(&II, VMap,
|
2011-12-23 10:18:32 +08:00
|
|
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
|
2013-05-28 23:17:05 +08:00
|
|
|
TypeMapper, Materializer);
|
2002-03-30 03:03:54 +08:00
|
|
|
}
|
2002-11-20 07:12:22 +08:00
|
|
|
|
2016-05-11 04:23:24 +08:00
|
|
|
/// Return a copy of the specified function and add it to that function's
|
|
|
|
/// module. Also, any references specified in the VMap are changed to refer to
|
|
|
|
/// their mapped value instead of the original one. If any of the arguments to
|
|
|
|
/// the function are in the VMap, the arguments are deleted from the resultant
|
|
|
|
/// function. The VMap is updated to include mappings from all of the
|
|
|
|
/// instructions and basicblocks in the function from their old to new values.
|
2002-11-20 07:12:22 +08:00
|
|
|
///
|
2016-05-11 04:23:24 +08:00
|
|
|
Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
|
2006-01-14 02:39:17 +08:00
|
|
|
ClonedCodeInfo *CodeInfo) {
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> ArgTypes;
|
2002-11-20 07:12:22 +08:00
|
|
|
|
|
|
|
// The user might be deleting arguments to the function by specifying them in
|
2010-06-24 07:55:51 +08:00
|
|
|
// the VMap. If so, we need to not add the arguments to the arg ty vector
|
2002-11-20 07:12:22 +08:00
|
|
|
//
|
2015-10-13 10:39:05 +08:00
|
|
|
for (const Argument &I : F->args())
|
|
|
|
if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet?
|
|
|
|
ArgTypes.push_back(I.getType());
|
2002-11-20 07:12:22 +08:00
|
|
|
|
|
|
|
// Create a new function type...
|
2009-07-30 06:17:13 +08:00
|
|
|
FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
|
2002-11-20 07:12:22 +08:00
|
|
|
ArgTypes, F->getFunctionType()->isVarArg());
|
|
|
|
|
|
|
|
// Create the new function...
|
2018-08-23 17:25:17 +08:00
|
|
|
Function *NewF = Function::Create(FTy, F->getLinkage(), F->getAddressSpace(),
|
|
|
|
F->getName(), F->getParent());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-11-20 07:12:22 +08:00
|
|
|
// Loop over the arguments, copying the names of the mapped arguments over...
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator DestI = NewF->arg_begin();
|
2015-10-13 10:39:05 +08:00
|
|
|
for (const Argument & I : F->args())
|
|
|
|
if (VMap.count(&I) == 0) { // Is this argument preserved?
|
|
|
|
DestI->setName(I.getName()); // Copy the name over...
|
|
|
|
VMap[&I] = &*DestI++; // Add mapping to VMap
|
2002-11-20 07:12:22 +08:00
|
|
|
}
|
|
|
|
|
2009-08-27 12:02:30 +08:00
|
|
|
SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
|
2017-06-02 07:02:12 +08:00
|
|
|
CloneFunctionInto(NewF, F, VMap, F->getSubprogram() != nullptr, Returns, "",
|
2016-05-11 04:23:24 +08:00
|
|
|
CodeInfo);
|
|
|
|
|
2005-04-22 07:48:37 +08:00
|
|
|
return NewF;
|
2002-11-20 07:12:22 +08:00
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2015-03-11 02:41:22 +08:00
|
|
|
/// This is a private class used to implement CloneAndPruneFunctionInto.
|
2009-10-25 14:33:48 +08:00
|
|
|
struct PruningFunctionCloner {
|
2006-05-27 09:22:24 +08:00
|
|
|
Function *NewFunc;
|
|
|
|
const Function *OldFunc;
|
2010-06-24 08:00:42 +08:00
|
|
|
ValueToValueMapTy &VMap;
|
2010-08-26 23:41:53 +08:00
|
|
|
bool ModuleLevelChanges;
|
2006-05-27 09:22:24 +08:00
|
|
|
const char *NameSuffix;
|
|
|
|
ClonedCodeInfo *CodeInfo;
|
2015-02-19 02:31:51 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
public:
|
|
|
|
PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
|
2015-03-10 10:37:25 +08:00
|
|
|
ValueToValueMapTy &valueMap, bool moduleLevelChanges,
|
2016-01-09 02:23:17 +08:00
|
|
|
const char *nameSuffix, ClonedCodeInfo *codeInfo)
|
2015-03-10 10:37:25 +08:00
|
|
|
: NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap),
|
|
|
|
ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix),
|
2016-01-09 02:23:17 +08:00
|
|
|
CodeInfo(codeInfo) {}
|
2006-05-27 09:22:24 +08:00
|
|
|
|
2015-03-11 02:41:22 +08:00
|
|
|
/// The specified block is found to be reachable, clone it and
|
2014-05-20 00:04:10 +08:00
|
|
|
/// anything that it can reach.
|
2018-05-14 16:24:29 +08:00
|
|
|
void CloneBlock(const BasicBlock *BB,
|
2015-02-19 02:31:51 +08:00
|
|
|
BasicBlock::const_iterator StartingInst,
|
2016-03-08 08:36:35 +08:00
|
|
|
std::vector<const BasicBlock*> &ToClone);
|
2006-05-27 09:22:24 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2006-05-27 09:22:24 +08:00
|
|
|
|
2015-03-11 02:41:22 +08:00
|
|
|
/// The specified block is found to be reachable, clone it and
|
2014-05-20 00:04:10 +08:00
|
|
|
/// anything that it can reach.
|
2007-03-02 11:11:20 +08:00
|
|
|
void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
|
2015-02-19 02:31:51 +08:00
|
|
|
BasicBlock::const_iterator StartingInst,
|
2016-03-08 08:36:35 +08:00
|
|
|
std::vector<const BasicBlock*> &ToClone){
|
2017-05-02 01:07:49 +08:00
|
|
|
WeakTrackingVH &BBEntry = VMap[BB];
|
2014-04-26 13:43:41 +08:00
|
|
|
|
2014-05-20 00:04:10 +08:00
|
|
|
// Have we already cloned this block?
|
|
|
|
if (BBEntry) return;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2014-05-01 06:05:02 +08:00
|
|
|
// Nope, clone it now.
|
2014-05-20 00:04:10 +08:00
|
|
|
BasicBlock *NewBB;
|
|
|
|
BBEntry = NewBB = BasicBlock::Create(BB->getContext());
|
|
|
|
if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
|
|
|
|
|
|
|
|
// It is only legal to clone a function if a block address within that
|
|
|
|
// function is never referenced outside of the function. Given that, we
|
|
|
|
// want to map block addresses from the old function to block addresses in
|
|
|
|
// the clone. (This is different from the generic ValueMapper
|
|
|
|
// implementation, which generates an invalid blockaddress when
|
|
|
|
// cloning a function.)
|
|
|
|
//
|
|
|
|
// Note that we don't need to fix the mapping for unreachable blocks;
|
|
|
|
// the default mapping there is safe.
|
|
|
|
if (BB->hasAddressTaken()) {
|
|
|
|
Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
|
|
|
|
const_cast<BasicBlock*>(BB));
|
|
|
|
VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
|
|
|
|
}
|
2011-10-22 04:45:19 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
|
2015-02-19 02:31:51 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
// Loop over all instructions, and copy them over, DCE'ing as we go. This
|
|
|
|
// loop doesn't include the terminator.
|
2015-02-19 02:31:51 +08:00
|
|
|
for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end();
|
2006-05-27 09:22:24 +08:00
|
|
|
II != IE; ++II) {
|
2015-02-19 02:31:51 +08:00
|
|
|
|
2012-03-25 12:03:40 +08:00
|
|
|
Instruction *NewInst = II->clone();
|
|
|
|
|
|
|
|
// Eagerly remap operands to the newly cloned instruction, except for PHI
|
|
|
|
// nodes for which we defer processing until we update the CFG.
|
|
|
|
if (!isa<PHINode>(NewInst)) {
|
|
|
|
RemapInstruction(NewInst, VMap,
|
2016-01-09 02:23:17 +08:00
|
|
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
|
2012-03-25 12:03:40 +08:00
|
|
|
|
|
|
|
// If we can simplify this instruction to some other value, simply add
|
|
|
|
// a mapping to that value rather than inserting a new instruction into
|
|
|
|
// the basic block.
|
2015-03-10 10:37:25 +08:00
|
|
|
if (Value *V =
|
|
|
|
SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) {
|
2017-07-01 11:29:33 +08:00
|
|
|
// On the off-chance that this simplifies to an instruction in the old
|
|
|
|
// function, map it back into the new function.
|
2017-08-19 14:56:11 +08:00
|
|
|
if (NewFunc != OldFunc)
|
|
|
|
if (Value *MappedV = VMap.lookup(V))
|
|
|
|
V = MappedV;
|
2012-03-25 12:03:40 +08:00
|
|
|
|
2016-06-25 08:04:10 +08:00
|
|
|
if (!NewInst->mayHaveSideEffects()) {
|
|
|
|
VMap[&*II] = V;
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-19 01:24:10 +08:00
|
|
|
NewInst->deleteValue();
|
2016-06-25 08:04:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-25 12:03:40 +08:00
|
|
|
}
|
2006-05-27 09:22:24 +08:00
|
|
|
}
|
2009-02-10 15:48:18 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
if (II->hasName())
|
|
|
|
NewInst->setName(II->getName()+NameSuffix);
|
2016-06-25 06:52:39 +08:00
|
|
|
VMap[&*II] = NewInst; // Add instruction map to value.
|
2012-03-25 12:03:40 +08:00
|
|
|
NewBB->getInstList().push_back(NewInst);
|
2009-03-11 06:20:02 +08:00
|
|
|
hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
|
2015-11-18 14:23:38 +08:00
|
|
|
|
|
|
|
if (CodeInfo)
|
|
|
|
if (auto CS = ImmutableCallSite(&*II))
|
|
|
|
if (CS.hasOperandBundles())
|
|
|
|
CodeInfo->OperandBundleCallSites.push_back(NewInst);
|
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
|
|
|
|
if (isa<ConstantInt>(AI->getArraySize()))
|
|
|
|
hasStaticAllocas = true;
|
|
|
|
else
|
|
|
|
hasDynamicAllocas = true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// Finally, clone over the terminator.
|
2018-10-15 18:04:59 +08:00
|
|
|
const Instruction *OldTI = BB->getTerminator();
|
2006-06-02 03:19:23 +08:00
|
|
|
bool TerminatorDone = false;
|
|
|
|
if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
|
|
|
|
if (BI->isConditional()) {
|
|
|
|
// If the condition was a known constant in the callee...
|
2007-01-11 20:24:14 +08:00
|
|
|
ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
|
|
|
|
// Or is a known constant in the caller...
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!Cond) {
|
2016-04-18 02:53:24 +08:00
|
|
|
Value *V = VMap.lookup(BI->getCondition());
|
2010-10-13 10:08:17 +08:00
|
|
|
Cond = dyn_cast_or_null<ConstantInt>(V);
|
|
|
|
}
|
2007-01-11 20:24:14 +08:00
|
|
|
|
|
|
|
// Constant fold to uncond branch!
|
|
|
|
if (Cond) {
|
2007-01-12 12:24:46 +08:00
|
|
|
BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
|
2010-06-24 07:55:51 +08:00
|
|
|
VMap[OldTI] = BranchInst::Create(Dest, NewBB);
|
2007-03-02 11:11:20 +08:00
|
|
|
ToClone.push_back(Dest);
|
2006-06-02 03:19:23 +08:00
|
|
|
TerminatorDone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
|
|
|
|
// If switching on a value known constant in the caller.
|
|
|
|
ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!Cond) { // Or known constant after constant prop in the callee...
|
2016-04-18 04:11:09 +08:00
|
|
|
Value *V = VMap.lookup(SI->getCondition());
|
2010-10-13 10:08:17 +08:00
|
|
|
Cond = dyn_cast_or_null<ConstantInt>(V);
|
|
|
|
}
|
2006-06-02 03:19:23 +08:00
|
|
|
if (Cond) { // Constant fold to uncond branch!
|
2017-04-12 15:27:28 +08:00
|
|
|
SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond);
|
2012-03-08 15:06:20 +08:00
|
|
|
BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor());
|
2010-06-24 07:55:51 +08:00
|
|
|
VMap[OldTI] = BranchInst::Create(Dest, NewBB);
|
2007-03-02 11:11:20 +08:00
|
|
|
ToClone.push_back(Dest);
|
2006-06-02 03:19:23 +08:00
|
|
|
TerminatorDone = true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
if (!TerminatorDone) {
|
2009-09-27 15:38:41 +08:00
|
|
|
Instruction *NewInst = OldTI->clone();
|
2006-06-02 03:19:23 +08:00
|
|
|
if (OldTI->hasName())
|
|
|
|
NewInst->setName(OldTI->getName()+NameSuffix);
|
|
|
|
NewBB->getInstList().push_back(NewInst);
|
2010-06-24 07:55:51 +08:00
|
|
|
VMap[OldTI] = NewInst; // Add instruction map to value.
|
2015-11-18 14:23:38 +08:00
|
|
|
|
|
|
|
if (CodeInfo)
|
|
|
|
if (auto CS = ImmutableCallSite(OldTI))
|
|
|
|
if (CS.hasOperandBundles())
|
|
|
|
CodeInfo->OperandBundleCallSites.push_back(NewInst);
|
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// Recursively clone any reachable successor blocks.
|
2018-10-15 18:04:59 +08:00
|
|
|
const Instruction *TI = BB->getTerminator();
|
2018-08-26 16:41:15 +08:00
|
|
|
for (const BasicBlock *Succ : successors(TI))
|
2015-08-07 04:22:46 +08:00
|
|
|
ToClone.push_back(Succ);
|
2006-06-02 03:19:23 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
if (CodeInfo) {
|
|
|
|
CodeInfo->ContainsCalls |= hasCalls;
|
|
|
|
CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
|
2018-07-31 03:41:25 +08:00
|
|
|
CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
|
2006-05-27 09:22:24 +08:00
|
|
|
BB != &BB->getParent()->front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 02:41:22 +08:00
|
|
|
/// This works like CloneAndPruneFunctionInto, except that it does not clone the
|
|
|
|
/// entire function. Instead it starts at an instruction provided by the caller
|
|
|
|
/// and copies (and prunes) only the code reachable from that instruction.
|
2016-03-08 08:36:35 +08:00
|
|
|
void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
|
|
|
|
const Instruction *StartingInst,
|
|
|
|
ValueToValueMapTy &VMap,
|
|
|
|
bool ModuleLevelChanges,
|
|
|
|
SmallVectorImpl<ReturnInst *> &Returns,
|
|
|
|
const char *NameSuffix,
|
|
|
|
ClonedCodeInfo *CodeInfo) {
|
2006-05-27 09:22:24 +08:00
|
|
|
assert(NameSuffix && "NameSuffix cannot be null!");
|
2015-02-19 02:31:51 +08:00
|
|
|
|
2015-02-24 04:01:56 +08:00
|
|
|
ValueMapTypeRemapper *TypeMapper = nullptr;
|
|
|
|
ValueMaterializer *Materializer = nullptr;
|
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
#ifndef NDEBUG
|
2015-08-09 02:27:36 +08:00
|
|
|
// If the cloning starts at the beginning of the function, verify that
|
2015-02-19 02:31:51 +08:00
|
|
|
// the function arguments are mapped.
|
|
|
|
if (!StartingInst)
|
2015-10-13 10:39:05 +08:00
|
|
|
for (const Argument &II : OldFunc->args())
|
|
|
|
assert(VMap.count(&II) && "No mapping from source argument specified!");
|
2006-05-27 09:22:24 +08:00
|
|
|
#endif
|
2008-05-27 03:58:59 +08:00
|
|
|
|
2010-08-26 23:41:53 +08:00
|
|
|
PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
|
2016-01-09 02:23:17 +08:00
|
|
|
NameSuffix, CodeInfo);
|
2015-02-19 02:31:51 +08:00
|
|
|
const BasicBlock *StartingBB;
|
|
|
|
if (StartingInst)
|
|
|
|
StartingBB = StartingInst->getParent();
|
|
|
|
else {
|
|
|
|
StartingBB = &OldFunc->getEntryBlock();
|
2015-10-13 10:39:05 +08:00
|
|
|
StartingInst = &StartingBB->front();
|
2015-02-19 02:31:51 +08:00
|
|
|
}
|
2006-05-27 09:22:24 +08:00
|
|
|
|
2014-05-20 00:04:10 +08:00
|
|
|
// Clone the entry block, and anything recursively reachable from it.
|
2007-03-02 11:11:20 +08:00
|
|
|
std::vector<const BasicBlock*> CloneWorklist;
|
2016-03-08 08:36:35 +08:00
|
|
|
PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist);
|
2007-03-02 11:11:20 +08:00
|
|
|
while (!CloneWorklist.empty()) {
|
|
|
|
const BasicBlock *BB = CloneWorklist.back();
|
|
|
|
CloneWorklist.pop_back();
|
2016-03-08 08:36:35 +08:00
|
|
|
PFC.CloneBlock(BB, BB->begin(), CloneWorklist);
|
2007-03-02 11:11:20 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
// Loop over all of the basic blocks in the old function. If the block was
|
|
|
|
// reachable, we have cloned it and the old block is now in the value map:
|
|
|
|
// insert it into the new function in the right order. If not, ignore it.
|
|
|
|
//
|
2006-06-02 03:19:23 +08:00
|
|
|
// Defer PHI resolution until rest of function is resolved.
|
2009-08-27 12:02:30 +08:00
|
|
|
SmallVector<const PHINode*, 16> PHIToResolve;
|
2015-10-13 10:39:05 +08:00
|
|
|
for (const BasicBlock &BI : *OldFunc) {
|
2016-04-18 02:53:24 +08:00
|
|
|
Value *V = VMap.lookup(&BI);
|
2010-10-13 10:08:17 +08:00
|
|
|
BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
|
2014-05-20 00:04:10 +08:00
|
|
|
if (!NewBB) continue; // Dead block.
|
2006-06-02 03:19:23 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
// Add the new block to the new function.
|
|
|
|
NewFunc->getBasicBlockList().push_back(NewBB);
|
2009-11-11 07:06:00 +08:00
|
|
|
|
2006-05-27 09:22:24 +08:00
|
|
|
// Handle PHI nodes specially, as we have to remove references to dead
|
|
|
|
// blocks.
|
2017-12-30 23:27:33 +08:00
|
|
|
for (const PHINode &PN : BI.phis()) {
|
2015-03-21 05:42:54 +08:00
|
|
|
// PHI nodes may have been remapped to non-PHI nodes by the caller or
|
|
|
|
// during the cloning process.
|
2017-12-30 23:27:33 +08:00
|
|
|
if (isa<PHINode>(VMap[&PN]))
|
|
|
|
PHIToResolve.push_back(&PN);
|
|
|
|
else
|
2012-03-25 12:03:40 +08:00
|
|
|
break;
|
2015-03-21 05:42:54 +08:00
|
|
|
}
|
2012-03-25 12:03:40 +08:00
|
|
|
|
|
|
|
// Finally, remap the terminator instructions, as those can't be remapped
|
|
|
|
// until all BBs are mapped.
|
|
|
|
RemapInstruction(NewBB->getTerminator(), VMap,
|
2015-02-24 04:01:56 +08:00
|
|
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
|
|
|
|
TypeMapper, Materializer);
|
2006-05-27 09:22:24 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// Defer PHI resolution until rest of function is resolved, PHI resolution
|
|
|
|
// requires the CFG to be up-to-date.
|
|
|
|
for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
|
|
|
|
const PHINode *OPN = PHIToResolve[phino];
|
|
|
|
unsigned NumPreds = OPN->getNumIncomingValues();
|
|
|
|
const BasicBlock *OldBB = OPN->getParent();
|
2010-06-24 07:55:51 +08:00
|
|
|
BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
|
2006-06-02 03:19:23 +08:00
|
|
|
|
|
|
|
// Map operands for blocks that are live and remove operands for blocks
|
|
|
|
// that are dead.
|
|
|
|
for (; phino != PHIToResolve.size() &&
|
|
|
|
PHIToResolve[phino]->getParent() == OldBB; ++phino) {
|
|
|
|
OPN = PHIToResolve[phino];
|
2010-06-24 07:55:51 +08:00
|
|
|
PHINode *PN = cast<PHINode>(VMap[OPN]);
|
2006-06-02 03:19:23 +08:00
|
|
|
for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
|
2016-04-18 04:11:09 +08:00
|
|
|
Value *V = VMap.lookup(PN->getIncomingBlock(pred));
|
2011-01-08 16:15:20 +08:00
|
|
|
if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
|
2009-07-06 06:41:43 +08:00
|
|
|
Value *InVal = MapValue(PN->getIncomingValue(pred),
|
2018-07-31 03:41:25 +08:00
|
|
|
VMap,
|
2011-01-08 16:15:20 +08:00
|
|
|
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
|
2006-06-02 03:19:23 +08:00
|
|
|
assert(InVal && "Unknown input value?");
|
|
|
|
PN->setIncomingValue(pred, InVal);
|
|
|
|
PN->setIncomingBlock(pred, MappedBlock);
|
|
|
|
} else {
|
|
|
|
PN->removeIncomingValue(pred, false);
|
2016-02-19 06:09:30 +08:00
|
|
|
--pred; // Revisit the next entry.
|
|
|
|
--e;
|
2006-06-02 03:19:23 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
}
|
2006-06-02 03:19:23 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// The loop above has removed PHI entries for those blocks that are dead
|
|
|
|
// and has updated others. However, if a block is live (i.e. copied over)
|
|
|
|
// but its terminator has been changed to not go to this block, then our
|
|
|
|
// phi nodes will have invalid entries. Update the PHI nodes in this
|
|
|
|
// case.
|
|
|
|
PHINode *PN = cast<PHINode>(NewBB->begin());
|
2018-05-11 07:01:54 +08:00
|
|
|
NumPreds = pred_size(NewBB);
|
2006-06-02 03:19:23 +08:00
|
|
|
if (NumPreds != PN->getNumIncomingValues()) {
|
|
|
|
assert(NumPreds < PN->getNumIncomingValues());
|
|
|
|
// Count how many times each predecessor comes to this block.
|
|
|
|
std::map<BasicBlock*, unsigned> PredCount;
|
2014-07-22 01:06:51 +08:00
|
|
|
for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
|
|
|
|
PI != E; ++PI)
|
|
|
|
--PredCount[*PI];
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// Figure out how many entries to remove from each PHI.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
|
|
|
++PredCount[PN->getIncomingBlock(i)];
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// At this point, the excess predecessor entries are positive in the
|
|
|
|
// map. Loop over all of the PHIs and remove excess predecessor
|
|
|
|
// entries.
|
|
|
|
BasicBlock::iterator I = NewBB->begin();
|
|
|
|
for (; (PN = dyn_cast<PHINode>(I)); ++I) {
|
2016-06-26 20:28:59 +08:00
|
|
|
for (const auto &PCI : PredCount) {
|
|
|
|
BasicBlock *Pred = PCI.first;
|
|
|
|
for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove)
|
2006-06-02 03:19:23 +08:00
|
|
|
PN->removeIncomingValue(Pred, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-06-02 03:19:23 +08:00
|
|
|
// If the loops above have made these phi nodes have 0 or 1 operand,
|
|
|
|
// replace them with undef or the input value. We must do this for
|
|
|
|
// correctness, because 0-operand phis are not valid.
|
|
|
|
PN = cast<PHINode>(NewBB->begin());
|
|
|
|
if (PN->getNumIncomingValues() == 0) {
|
|
|
|
BasicBlock::iterator I = NewBB->begin();
|
|
|
|
BasicBlock::const_iterator OldI = OldBB->begin();
|
|
|
|
while ((PN = dyn_cast<PHINode>(I++))) {
|
2009-07-31 07:03:37 +08:00
|
|
|
Value *NV = UndefValue::get(PN->getType());
|
2006-06-02 03:19:23 +08:00
|
|
|
PN->replaceAllUsesWith(NV);
|
2015-10-13 10:39:05 +08:00
|
|
|
assert(VMap[&*OldI] == PN && "VMap mismatch");
|
|
|
|
VMap[&*OldI] = NV;
|
2006-06-02 03:19:23 +08:00
|
|
|
PN->eraseFromParent();
|
|
|
|
++OldI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-25 18:34:54 +08:00
|
|
|
|
|
|
|
// Make a second pass over the PHINodes now that all of them have been
|
|
|
|
// remapped into the new function, simplifying the PHINode and performing any
|
|
|
|
// recursive simplifications exposed. This will transparently update the
|
2017-05-02 01:07:49 +08:00
|
|
|
// WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
|
2012-03-25 18:34:54 +08:00
|
|
|
// two PHINodes, the iteration over the old PHIs remains valid, and the
|
|
|
|
// mapping will just map us to the new node (which may not even be a PHI
|
|
|
|
// node).
|
2016-08-04 12:24:02 +08:00
|
|
|
const DataLayout &DL = NewFunc->getParent()->getDataLayout();
|
|
|
|
SmallSetVector<const Value *, 8> Worklist;
|
2012-03-25 18:34:54 +08:00
|
|
|
for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx)
|
2016-08-04 12:24:02 +08:00
|
|
|
if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
|
|
|
|
Worklist.insert(PHIToResolve[Idx]);
|
|
|
|
|
|
|
|
// Note that we must test the size on each iteration, the worklist can grow.
|
|
|
|
for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
|
|
|
|
const Value *OrigV = Worklist[Idx];
|
2016-08-04 12:47:18 +08:00
|
|
|
auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV));
|
2016-08-04 12:24:02 +08:00
|
|
|
if (!I)
|
|
|
|
continue;
|
2016-08-20 00:37:40 +08:00
|
|
|
|
|
|
|
// Skip over non-intrinsic callsites, we don't want to remove any nodes from
|
|
|
|
// the CGSCC.
|
|
|
|
CallSite CS = CallSite(I);
|
|
|
|
if (CS && CS.getCalledFunction() && !CS.getCalledFunction()->isIntrinsic())
|
|
|
|
continue;
|
2016-08-04 12:24:02 +08:00
|
|
|
|
|
|
|
// See if this instruction simplifies.
|
|
|
|
Value *SimpleV = SimplifyInstruction(I, DL);
|
|
|
|
if (!SimpleV)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Stash away all the uses of the old instruction so we can check them for
|
|
|
|
// recursive simplifications after a RAUW. This is cheaper than checking all
|
|
|
|
// uses of To on the recursive step in most cases.
|
|
|
|
for (const User *U : OrigV->users())
|
|
|
|
Worklist.insert(cast<Instruction>(U));
|
|
|
|
|
|
|
|
// Replace the instruction with its simplified value.
|
|
|
|
I->replaceAllUsesWith(SimpleV);
|
|
|
|
|
|
|
|
// If the original instruction had no side effects, remove it.
|
|
|
|
if (isInstructionTriviallyDead(I))
|
|
|
|
I->eraseFromParent();
|
|
|
|
else
|
|
|
|
VMap[OrigV] = I;
|
|
|
|
}
|
2012-03-25 18:34:54 +08:00
|
|
|
|
2006-09-14 05:27:00 +08:00
|
|
|
// Now that the inlined function body has been fully constructed, go through
|
2015-03-11 02:37:05 +08:00
|
|
|
// and zap unconditional fall-through branches. This happens all the time when
|
2006-09-14 05:27:00 +08:00
|
|
|
// specializing code: code specialization turns conditional branches into
|
|
|
|
// uncond branches, and this code folds them.
|
2015-10-13 10:39:05 +08:00
|
|
|
Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator();
|
2012-03-28 16:38:27 +08:00
|
|
|
Function::iterator I = Begin;
|
2006-09-14 05:27:00 +08:00
|
|
|
while (I != NewFunc->end()) {
|
[CloneFunction] Constant fold terminators before checking single predecessor
Summary:
This fixes PR31105.
There is code trying to delete dead code that does so by e.g. checking if
the single predecessor of a block is the block itself.
That check fails on a block like this
bb:
br i1 undef, label %bb, label %bb
since that has two (identical) predecessors.
However, after the check for dead blocks there is a call to
ConstantFoldTerminator on the basic block, and that call simplifies the
block to
bb:
br label %bb
Therefore we now do the call to ConstantFoldTerminator before the check if
the block is dead, so it can realize that it really is.
The original behavior lead to the block not being removed, but it was
simplified as above, and then we did a call to
Dest->replaceAllUsesWith(&*I);
with old and new being equal, and an assertion triggered.
Reviewers: chandlerc, fhahn
Reviewed By: fhahn
Subscribers: eraman, llvm-commits
Differential Revision: https://reviews.llvm.org/D51280
llvm-svn: 340820
2018-08-28 20:40:11 +08:00
|
|
|
// We need to simplify conditional branches and switches with a constant
|
|
|
|
// operand. We try to prune these out when cloning, but if the
|
|
|
|
// simplification required looking through PHI nodes, those are only
|
|
|
|
// available after forming the full basic block. That may leave some here,
|
|
|
|
// and we still want to prune the dead code as early as possible.
|
|
|
|
//
|
|
|
|
// Do the folding before we check if the block is dead since we want code
|
|
|
|
// like
|
|
|
|
// bb:
|
|
|
|
// br i1 undef, label %bb, label %bb
|
|
|
|
// to be simplified to
|
|
|
|
// bb:
|
|
|
|
// br label %bb
|
|
|
|
// before we call I->getSinglePredecessor().
|
|
|
|
ConstantFoldTerminator(&*I);
|
|
|
|
|
2012-03-28 16:38:27 +08:00
|
|
|
// Check if this block has become dead during inlining or other
|
|
|
|
// simplifications. Note that the first block will appear dead, as it has
|
|
|
|
// not yet been wired up properly.
|
2015-10-13 10:39:05 +08:00
|
|
|
if (I != Begin && (pred_begin(&*I) == pred_end(&*I) ||
|
|
|
|
I->getSinglePredecessor() == &*I)) {
|
|
|
|
BasicBlock *DeadBB = &*I++;
|
2012-03-28 16:38:27 +08:00
|
|
|
DeleteDeadBlock(DeadBB);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-09-14 05:27:00 +08:00
|
|
|
BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
|
|
|
|
if (!BI || BI->isConditional()) { ++I; continue; }
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-09-14 05:27:00 +08:00
|
|
|
BasicBlock *Dest = BI->getSuccessor(0);
|
2012-03-25 18:34:54 +08:00
|
|
|
if (!Dest->getSinglePredecessor()) {
|
2007-02-02 02:48:38 +08:00
|
|
|
++I; continue;
|
|
|
|
}
|
2012-03-25 18:34:54 +08:00
|
|
|
|
|
|
|
// We shouldn't be able to get single-entry PHI nodes here, as instsimplify
|
|
|
|
// above should have zapped all of them..
|
|
|
|
assert(!isa<PHINode>(Dest->begin()));
|
|
|
|
|
2006-09-14 05:27:00 +08:00
|
|
|
// We know all single-entry PHI nodes in the inlined function have been
|
|
|
|
// removed, so we just need to splice the blocks.
|
|
|
|
BI->eraseFromParent();
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2011-06-23 14:24:52 +08:00
|
|
|
// Make all PHI nodes that referred to Dest now refer to I as their source.
|
2015-10-13 10:39:05 +08:00
|
|
|
Dest->replaceAllUsesWith(&*I);
|
2011-06-23 14:24:52 +08:00
|
|
|
|
2011-06-23 17:09:15 +08:00
|
|
|
// Move all the instructions in the succ to the pred.
|
|
|
|
I->getInstList().splice(I->end(), Dest->getInstList());
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-09-14 05:27:00 +08:00
|
|
|
// Remove the dest block.
|
|
|
|
Dest->eraseFromParent();
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2006-09-14 05:27:00 +08:00
|
|
|
// Do not increment I, iteratively merge all things this block branches to.
|
|
|
|
}
|
2012-04-07 01:21:31 +08:00
|
|
|
|
2015-03-11 02:37:05 +08:00
|
|
|
// Make a final pass over the basic blocks from the old function to gather
|
2012-04-07 01:21:31 +08:00
|
|
|
// any return instructions which survived folding. We have to do this here
|
|
|
|
// because we can iteratively remove and merge returns above.
|
2015-10-13 10:39:05 +08:00
|
|
|
for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(),
|
2012-04-07 01:21:31 +08:00
|
|
|
E = NewFunc->end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
|
|
|
|
Returns.push_back(RI);
|
2006-09-14 05:27:00 +08:00
|
|
|
}
|
2015-02-19 02:31:51 +08:00
|
|
|
|
|
|
|
|
2015-03-11 02:41:22 +08:00
|
|
|
/// This works exactly like CloneFunctionInto,
|
2015-02-19 02:31:51 +08:00
|
|
|
/// except that it does some simple constant prop and DCE on the fly. The
|
|
|
|
/// effect of this is to copy significantly less code in cases where (for
|
|
|
|
/// example) a function call with constant arguments is inlined, and those
|
|
|
|
/// constant arguments cause a significant amount of code in the callee to be
|
|
|
|
/// dead. Since this doesn't produce an exact copy of the input, it can't be
|
|
|
|
/// used for things like CloneFunction or CloneModule.
|
2016-03-08 08:36:35 +08:00
|
|
|
void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
|
|
|
|
ValueToValueMapTy &VMap,
|
|
|
|
bool ModuleLevelChanges,
|
|
|
|
SmallVectorImpl<ReturnInst*> &Returns,
|
2018-07-31 03:41:25 +08:00
|
|
|
const char *NameSuffix,
|
2016-03-08 08:36:35 +08:00
|
|
|
ClonedCodeInfo *CodeInfo,
|
|
|
|
Instruction *TheCall) {
|
2015-10-13 10:39:05 +08:00
|
|
|
CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap,
|
2016-03-08 08:36:35 +08:00
|
|
|
ModuleLevelChanges, Returns, NameSuffix, CodeInfo);
|
2015-02-19 02:31:51 +08:00
|
|
|
}
|
2015-07-11 02:55:09 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Remaps instructions in \p Blocks using the mapping in \p VMap.
|
2015-07-11 02:55:09 +08:00
|
|
|
void llvm::remapInstructionsInBlocks(
|
|
|
|
const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) {
|
|
|
|
// Rewrite the code to refer to itself.
|
|
|
|
for (auto *BB : Blocks)
|
|
|
|
for (auto &Inst : *BB)
|
|
|
|
RemapInstruction(&Inst, VMap,
|
2016-04-07 08:26:43 +08:00
|
|
|
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
|
2015-07-11 02:55:09 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
|
2015-07-11 02:55:09 +08:00
|
|
|
/// Blocks.
|
|
|
|
///
|
|
|
|
/// Updates LoopInfo and DominatorTree assuming the loop is dominated by block
|
|
|
|
/// \p LoopDomBB. Insert the new blocks before block specified in \p Before.
|
|
|
|
Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB,
|
|
|
|
Loop *OrigLoop, ValueToValueMapTy &VMap,
|
|
|
|
const Twine &NameSuffix, LoopInfo *LI,
|
|
|
|
DominatorTree *DT,
|
|
|
|
SmallVectorImpl<BasicBlock *> &Blocks) {
|
2018-07-31 03:41:25 +08:00
|
|
|
assert(OrigLoop->getSubLoops().empty() &&
|
2016-04-27 13:25:09 +08:00
|
|
|
"Loop to be cloned cannot have inner loop");
|
2015-07-11 02:55:09 +08:00
|
|
|
Function *F = OrigLoop->getHeader()->getParent();
|
|
|
|
Loop *ParentLoop = OrigLoop->getParentLoop();
|
|
|
|
|
2017-09-28 10:45:42 +08:00
|
|
|
Loop *NewLoop = LI->AllocateLoop();
|
2015-07-11 02:55:09 +08:00
|
|
|
if (ParentLoop)
|
|
|
|
ParentLoop->addChildLoop(NewLoop);
|
|
|
|
else
|
|
|
|
LI->addTopLevelLoop(NewLoop);
|
|
|
|
|
|
|
|
BasicBlock *OrigPH = OrigLoop->getLoopPreheader();
|
|
|
|
assert(OrigPH && "No preheader");
|
|
|
|
BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F);
|
|
|
|
// To rename the loop PHIs.
|
|
|
|
VMap[OrigPH] = NewPH;
|
|
|
|
Blocks.push_back(NewPH);
|
|
|
|
|
|
|
|
// Update LoopInfo.
|
|
|
|
if (ParentLoop)
|
|
|
|
ParentLoop->addBasicBlockToLoop(NewPH, *LI);
|
|
|
|
|
|
|
|
// Update DominatorTree.
|
|
|
|
DT->addNewBlock(NewPH, LoopDomBB);
|
|
|
|
|
|
|
|
for (BasicBlock *BB : OrigLoop->getBlocks()) {
|
|
|
|
BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F);
|
|
|
|
VMap[BB] = NewBB;
|
|
|
|
|
|
|
|
// Update LoopInfo.
|
|
|
|
NewLoop->addBasicBlockToLoop(NewBB, *LI);
|
|
|
|
|
2016-06-12 00:41:10 +08:00
|
|
|
// Add DominatorTree node. After seeing all blocks, update to correct IDom.
|
|
|
|
DT->addNewBlock(NewBB, NewPH);
|
2015-07-11 02:55:09 +08:00
|
|
|
|
|
|
|
Blocks.push_back(NewBB);
|
|
|
|
}
|
|
|
|
|
2016-06-12 00:41:10 +08:00
|
|
|
for (BasicBlock *BB : OrigLoop->getBlocks()) {
|
|
|
|
// Update DominatorTree.
|
|
|
|
BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock();
|
|
|
|
DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]),
|
|
|
|
cast<BasicBlock>(VMap[IDomBB]));
|
|
|
|
}
|
|
|
|
|
2015-07-11 02:55:09 +08:00
|
|
|
// Move them physically from the end of the block list.
|
2015-10-13 10:39:05 +08:00
|
|
|
F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
|
|
|
|
NewPH);
|
|
|
|
F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(),
|
|
|
|
NewLoop->getHeader()->getIterator(), F->end());
|
2015-07-11 02:55:09 +08:00
|
|
|
|
|
|
|
return NewLoop;
|
|
|
|
}
|
2017-02-17 12:21:14 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Duplicate non-Phi instructions from the beginning of block up to
|
2017-02-17 12:21:14 +08:00
|
|
|
/// StopAt instruction into a split block between BB and its predecessor.
|
|
|
|
BasicBlock *
|
|
|
|
llvm::DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB,
|
|
|
|
Instruction *StopAt,
|
2018-03-22 19:38:53 +08:00
|
|
|
ValueToValueMapTy &ValueMapping,
|
|
|
|
DominatorTree *DT) {
|
2017-02-17 12:21:14 +08:00
|
|
|
// We are going to have to map operands from the original BB block to the new
|
|
|
|
// copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
|
|
|
|
// account for entry from PredBB.
|
|
|
|
BasicBlock::iterator BI = BB->begin();
|
|
|
|
for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
|
|
|
|
ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
|
|
|
|
|
2018-03-22 19:38:53 +08:00
|
|
|
BasicBlock *NewBB = SplitEdge(PredBB, BB, DT);
|
2017-02-17 12:21:14 +08:00
|
|
|
NewBB->setName(PredBB->getName() + ".split");
|
|
|
|
Instruction *NewTerm = NewBB->getTerminator();
|
|
|
|
|
|
|
|
// Clone the non-phi instructions of BB into NewBB, keeping track of the
|
|
|
|
// mapping and using it to remap operands in the cloned instructions.
|
2018-03-06 21:12:32 +08:00
|
|
|
// Stop once we see the terminator too. This covers the case where BB's
|
|
|
|
// terminator gets replaced and StopAt == BB's terminator.
|
|
|
|
for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) {
|
2017-02-17 12:21:14 +08:00
|
|
|
Instruction *New = BI->clone();
|
|
|
|
New->setName(BI->getName());
|
|
|
|
New->insertBefore(NewTerm);
|
|
|
|
ValueMapping[&*BI] = New;
|
|
|
|
|
|
|
|
// Remap operands to patch up intra-block references.
|
|
|
|
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
|
|
|
|
auto I = ValueMapping.find(Inst);
|
|
|
|
if (I != ValueMapping.end())
|
|
|
|
New->setOperand(i, I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewBB;
|
|
|
|
}
|