Skip to content

Commit c1b9667

Browse files
committed
[InstCombine] Support opaque pointers in callee bitcast fold
To make this actually trigger, we also need to check whether the function types differ, which is a hidden cast under opaque pointers. The transform is somewhat less relevant there because it is primarily about pointer bitcasts, but it can also happen with other bit- or pointer-castable types. Byval handling is easier with opaque pointers because there is no need to adjust the byval type, we only need to make sure that it's still a pointer.
1 parent 6b3b3ef commit c1b9667

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -2834,10 +2834,12 @@ Instruction *InstCombinerImpl::visitCallBase(CallBase &Call) {
28342834
// If the callee is a pointer to a function, attempt to move any casts to the
28352835
// arguments of the call/callbr/invoke.
28362836
Value *Callee = Call.getCalledOperand();
2837-
if (!isa<Function>(Callee) && transformConstExprCastCall(Call))
2837+
Function *CalleeF = dyn_cast<Function>(Callee);
2838+
if ((!CalleeF || CalleeF->getFunctionType() != Call.getFunctionType()) &&
2839+
transformConstExprCastCall(Call))
28382840
return nullptr;
28392841

2840-
if (Function *CalleeF = dyn_cast<Function>(Callee)) {
2842+
if (CalleeF) {
28412843
// Remove the convergent attr on calls when the callee is not convergent.
28422844
if (Call.isConvergent() && !CalleeF->isConvergent() &&
28432845
!CalleeF->isIntrinsic()) {
@@ -3167,13 +3169,18 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
31673169
// sized type and the sized type has to have the same size as the old type.
31683170
if (ParamTy != ActTy && CallerPAL.hasParamAttr(i, Attribute::ByVal)) {
31693171
PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
3170-
if (!ParamPTy || !ParamPTy->getPointerElementType()->isSized())
3172+
if (!ParamPTy)
31713173
return false;
31723174

3173-
Type *CurElTy = Call.getParamByValType(i);
3174-
if (DL.getTypeAllocSize(CurElTy) !=
3175-
DL.getTypeAllocSize(ParamPTy->getPointerElementType()))
3176-
return false;
3175+
if (!ParamPTy->isOpaque()) {
3176+
Type *ParamElTy = ParamPTy->getNonOpaquePointerElementType();
3177+
if (!ParamElTy->isSized())
3178+
return false;
3179+
3180+
Type *CurElTy = Call.getParamByValType(i);
3181+
if (DL.getTypeAllocSize(CurElTy) != DL.getTypeAllocSize(ParamElTy))
3182+
return false;
3183+
}
31773184
}
31783185
}
31793186

@@ -3232,9 +3239,10 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {
32323239
Args.push_back(NewArg);
32333240

32343241
// Add any parameter attributes.
3235-
if (CallerPAL.hasParamAttr(i, Attribute::ByVal)) {
3242+
if (CallerPAL.hasParamAttr(i, Attribute::ByVal) &&
3243+
!ParamTy->isOpaquePointerTy()) {
32363244
AttrBuilder AB(FT->getContext(), CallerPAL.getParamAttrs(i));
3237-
AB.addByValAttr(NewArg->getType()->getPointerElementType());
3245+
AB.addByValAttr(ParamTy->getNonOpaquePointerElementType());
32383246
ArgAttrs.push_back(AttributeSet::get(Ctx, AB));
32393247
} else
32403248
ArgAttrs.push_back(CallerPAL.getParamAttrs(i));

llvm/test/Transforms/InstCombine/opaque-ptr.ll

+23
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,26 @@ define void @dse(ptr %p) {
493493
store i8 1, ptr %p
494494
ret void
495495
}
496+
497+
declare void @call_i64(i64)
498+
declare void @call_byval(i64, ptr byval(i64))
499+
500+
define void @call_cast_ptr_to_int(ptr %p) {
501+
; CHECK-LABEL: @call_cast_ptr_to_int(
502+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P:%.*]] to i64
503+
; CHECK-NEXT: call void @call_i64(i64 [[TMP1]])
504+
; CHECK-NEXT: ret void
505+
;
506+
call void @call_i64(ptr %p)
507+
ret void
508+
}
509+
510+
define void @call_cast_byval(ptr %p, ptr %p2) {
511+
; CHECK-LABEL: @call_cast_byval(
512+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P:%.*]] to i64
513+
; CHECK-NEXT: call void @call_byval(i64 [[TMP1]], ptr byval(double) [[P2:%.*]])
514+
; CHECK-NEXT: ret void
515+
;
516+
call void @call_byval(ptr %p, ptr byval(double) %p2)
517+
ret void
518+
}

0 commit comments

Comments
 (0)