llvm.org GIT mirror llvm / 4ac1416
[ICP] Remove incompatible attributes at indirect-call promoted callsites. Summary: Removing ncompatible attributes at indirect-call promoted callsites, not removing it results in at least a IR verification error. Reviewers: davidxl, xur, mssimpso Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D54913 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@347605 91177308-0d34-0410-b5e6-96231b3b80d8 Xin Tong 11 months ago
2 changed file(s) with 59 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
392392 // to the correct type.
393393 auto CalleeType = Callee->getFunctionType();
394394 auto CalleeParamNum = CalleeType->getNumParams();
395
396 LLVMContext &Ctx = Callee->getContext();
397 const AttributeList &CallerPAL = CS.getAttributes();
398 // The new list of argument attributes.
399 SmallVector NewArgAttrs;
400 bool AttributeChanged = false;
401
395402 for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
396403 auto *Arg = CS.getArgument(ArgNo);
397404 Type *FormalTy = CalleeType->getParamType(ArgNo);
400407 auto *Cast = CastInst::CreateBitOrPointerCast(Arg, FormalTy, "",
401408 CS.getInstruction());
402409 CS.setArgument(ArgNo, Cast);
403 }
410
411 // Remove any incompatible attributes for the argument.
412 AttrBuilder ArgAttrs(CallerPAL.getParamAttributes(ArgNo));
413 ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy));
414 NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs));
415 AttributeChanged = true;
416 } else
417 NewArgAttrs.push_back(CallerPAL.getParamAttributes(ArgNo));
404418 }
405419
406420 // If the return type of the call site doesn't match that of the callee, cast
407421 // the returned value to the appropriate type.
408 if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy)
422 // Remove any incompatible return value attribute.
423 AttrBuilder RAttrs(CallerPAL, AttributeList::ReturnIndex);
424 if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) {
409425 createRetBitCast(CS, CallSiteRetTy, RetBitCast);
426 RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy));
427 AttributeChanged = true;
428 }
429
430 // Set the new callsite attribute.
431 if (AttributeChanged)
432 CS.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttributes(),
433 AttributeSet::get(Ctx, RAttrs),
434 NewArgAttrs));
410435
411436 return CS.getInstruction();
412437 }
0 ; RUN: opt -S -pgo-icall-prom -icp-total-percent-threshold=0 < %s 2>&1 | FileCheck %s
1
2 ; Test that CallPromotionUtils will promote calls which require pointer cast
3 ; safely, i.e. drop incompatible attributes.
4
5 @foo = common global i8* (i8*)* null, align 8
6
7 ; casting to i64 and pointer attribute at callsite dropped.
8 define i64 @func2(i64 %a) {
9 ret i64 undef
10 }
11
12 ; no casting needed, attribute at callsite preserved.
13 define i8* @func4(i8* %a) {
14 ret i8* undef
15 }
16
17 define i8* @bar(i8* %arg) {
18 %tmp = load i8* (i8*)*, i8* (i8*)** @foo, align 8
19
20 ; Make sure callsite attributes are preserved on arguments and retval.
21 ; CHECK: call noalias i8* @func4(i8* nonnull
22
23 ; Make sure callsite attributes are dropped on arguments and retval.
24 ; CHECK: [[ARG:%[0-9]+]] = ptrtoint i8* %arg to i64
25 ; CHECK-NEXT: call i64 @func2(i64 [[ARG]])
26
27 %call = call noalias i8* %tmp(i8* nonnull %arg), !prof !1
28 ret i8* %call
29 }
30
31 !1 = !{!"VP", i32 0, i64 1440, i64 7651369219802541373, i64 1030, i64 -4377547752858689819, i64 410}