Skip to content

Commit 33d44b7

Browse files
committed
[OpaquePtr][Inline] Use byval type instead of pointee type
Reviewed By: #opaque-pointers, dblaikie Differential Revision: https://reviews.llvm.org/D105711
1 parent 06a4c85 commit 33d44b7

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,13 +1375,13 @@ static void UpdateCallGraphAfterInlining(CallBase &CB,
13751375
CallerNode->removeCallEdgeFor(*cast<CallBase>(&CB));
13761376
}
13771377

1378-
static void HandleByValArgumentInit(Value *Dst, Value *Src, Module *M,
1379-
BasicBlock *InsertBlock,
1378+
static void HandleByValArgumentInit(Type *ByValType, Value *Dst, Value *Src,
1379+
Module *M, BasicBlock *InsertBlock,
13801380
InlineFunctionInfo &IFI) {
1381-
Type *AggTy = cast<PointerType>(Src->getType())->getElementType();
13821381
IRBuilder<> Builder(InsertBlock, InsertBlock->begin());
13831382

1384-
Value *Size = Builder.getInt64(M->getDataLayout().getTypeStoreSize(AggTy));
1383+
Value *Size =
1384+
Builder.getInt64(M->getDataLayout().getTypeStoreSize(ByValType));
13851385

13861386
// Always generate a memcpy of alignment 1 here because we don't know
13871387
// the alignment of the src pointer. Other optimizations can infer
@@ -1392,13 +1392,13 @@ static void HandleByValArgumentInit(Value *Dst, Value *Src, Module *M,
13921392

13931393
/// When inlining a call site that has a byval argument,
13941394
/// we have to make the implicit memcpy explicit by adding it.
1395-
static Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
1395+
static Value *HandleByValArgument(Type *ByValType, Value *Arg,
1396+
Instruction *TheCall,
13961397
const Function *CalledFunc,
13971398
InlineFunctionInfo &IFI,
13981399
unsigned ByValAlignment) {
1399-
PointerType *ArgTy = cast<PointerType>(Arg->getType());
1400-
Type *AggTy = ArgTy->getElementType();
1401-
1400+
assert(cast<PointerType>(Arg->getType())
1401+
->isOpaqueOrPointeeTypeMatches(ByValType));
14021402
Function *Caller = TheCall->getFunction();
14031403
const DataLayout &DL = Caller->getParent()->getDataLayout();
14041404

@@ -1426,15 +1426,15 @@ static Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
14261426
}
14271427

14281428
// Create the alloca. If we have DataLayout, use nice alignment.
1429-
Align Alignment(DL.getPrefTypeAlignment(AggTy));
1429+
Align Alignment(DL.getPrefTypeAlignment(ByValType));
14301430

14311431
// If the byval had an alignment specified, we *must* use at least that
14321432
// alignment, as it is required by the byval argument (and uses of the
14331433
// pointer inside the callee).
14341434
Alignment = max(Alignment, MaybeAlign(ByValAlignment));
14351435

14361436
Value *NewAlloca =
1437-
new AllocaInst(AggTy, DL.getAllocaAddrSpace(), nullptr, Alignment,
1437+
new AllocaInst(ByValType, DL.getAllocaAddrSpace(), nullptr, Alignment,
14381438
Arg->getName(), &*Caller->begin()->begin());
14391439
IFI.StaticAllocas.push_back(cast<AllocaInst>(NewAlloca));
14401440

@@ -1894,8 +1894,13 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
18941894

18951895
{ // Scope to destroy VMap after cloning.
18961896
ValueToValueMapTy VMap;
1897+
struct ByValInit {
1898+
Value *Dst;
1899+
Value *Src;
1900+
Type *Ty;
1901+
};
18971902
// Keep a list of pair (dst, src) to emit byval initializations.
1898-
SmallVector<std::pair<Value*, Value*>, 4> ByValInit;
1903+
SmallVector<ByValInit, 4> ByValInits;
18991904

19001905
// When inlining a function that contains noalias scope metadata,
19011906
// this metadata needs to be cloned so that the inlined blocks
@@ -1920,10 +1925,12 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
19201925
// or readnone, because the copy would be unneeded: the callee doesn't
19211926
// modify the struct.
19221927
if (CB.isByValArgument(ArgNo)) {
1923-
ActualArg = HandleByValArgument(ActualArg, &CB, CalledFunc, IFI,
1928+
ActualArg = HandleByValArgument(CB.getParamByValType(ArgNo), ActualArg,
1929+
&CB, CalledFunc, IFI,
19241930
CalledFunc->getParamAlignment(ArgNo));
19251931
if (ActualArg != *AI)
1926-
ByValInit.push_back(std::make_pair(ActualArg, (Value*) *AI));
1932+
ByValInits.push_back(
1933+
{ActualArg, (Value *)*AI, CB.getParamByValType(ArgNo)});
19271934
}
19281935

19291936
VMap[&*I] = ActualArg;
@@ -1970,8 +1977,8 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
19701977
}
19711978

19721979
// Inject byval arguments initialization.
1973-
for (std::pair<Value*, Value*> &Init : ByValInit)
1974-
HandleByValArgumentInit(Init.first, Init.second, Caller->getParent(),
1980+
for (ByValInit &Init : ByValInits)
1981+
HandleByValArgumentInit(Init.Ty, Init.Dst, Init.Src, Caller->getParent(),
19751982
&*FirstNewBlock, IFI);
19761983

19771984
Optional<OperandBundleUse> ParentDeopt =

0 commit comments

Comments
 (0)