1 //===-- AbstractCallSite.cpp - Implementation of abstract call sites ------===// 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 abstract call sites which unify the interface for 10 // direct, indirect, and callback call sites. 11 // 12 // For more information see: 13 // https://llvm.org/devmtg/2018-10/talk-abstracts.html#talk20 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/ADT/StringSwitch.h" 19 #include "llvm/IR/CallSite.h" 20 #include "llvm/Support/Debug.h" 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "abstract-call-sites" 25 26 STATISTIC(NumCallbackCallSites, "Number of callback call sites created"); 27 STATISTIC(NumDirectAbstractCallSites, 28 "Number of direct abstract call sites created"); 29 STATISTIC(NumInvalidAbstractCallSitesUnknownUse, 30 "Number of invalid abstract call sites created (unknown use)"); 31 STATISTIC(NumInvalidAbstractCallSitesUnknownCallee, 32 "Number of invalid abstract call sites created (unknown callee)"); 33 STATISTIC(NumInvalidAbstractCallSitesNoCallback, 34 "Number of invalid abstract call sites created (no callback)"); 35 36 void AbstractCallSite::getCallbackUses(ImmutableCallSite ICS, 37 SmallVectorImpl<const Use *> &CBUses) { 38 const Function *Callee = ICS.getCalledFunction(); 39 if (!Callee) 40 return; 41 42 MDNode *CallbackMD = Callee->getMetadata(LLVMContext::MD_callback); 43 if (!CallbackMD) 44 return; 45 46 for (const MDOperand &Op : CallbackMD->operands()) { 47 MDNode *OpMD = cast<MDNode>(Op.get()); 48 auto *CBCalleeIdxAsCM = cast<ConstantAsMetadata>(OpMD->getOperand(0)); 49 uint64_t CBCalleeIdx = 50 cast<ConstantInt>(CBCalleeIdxAsCM->getValue())->getZExtValue(); 51 if (CBCalleeIdx < ICS.arg_size()) 52 CBUses.push_back(ICS.arg_begin() + CBCalleeIdx); 53 } 54 } 55 56 /// Create an abstract call site from a use. 57 AbstractCallSite::AbstractCallSite(const Use *U) : CS(U->getUser()) { 58 59 // First handle unknown users. 60 if (!CS) { 61 62 // If the use is actually in a constant cast expression which itself 63 // has only one use, we look through the constant cast expression. 64 // This happens by updating the use @p U to the use of the constant 65 // cast expression and afterwards re-initializing CS accordingly. 66 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U->getUser())) 67 if (CE->getNumUses() == 1 && CE->isCast()) { 68 U = &*CE->use_begin(); 69 CS = CallSite(U->getUser()); 70 } 71 72 if (!CS) { 73 NumInvalidAbstractCallSitesUnknownUse++; 74 return; 75 } 76 } 77 78 // Then handle direct or indirect calls. Thus, if U is the callee of the 79 // call site CS it is not a callback and we are done. 80 if (CS.isCallee(U)) { 81 NumDirectAbstractCallSites++; 82 return; 83 } 84 85 // If we cannot identify the broker function we cannot create a callback and 86 // invalidate the abstract call site. 87 Function *Callee = CS.getCalledFunction(); 88 if (!Callee) { 89 NumInvalidAbstractCallSitesUnknownCallee++; 90 CS = CallSite(); 91 return; 92 } 93 94 MDNode *CallbackMD = Callee->getMetadata(LLVMContext::MD_callback); 95 if (!CallbackMD) { 96 NumInvalidAbstractCallSitesNoCallback++; 97 CS = CallSite(); 98 return; 99 } 100 101 unsigned UseIdx = CS.getArgumentNo(U); 102 MDNode *CallbackEncMD = nullptr; 103 for (const MDOperand &Op : CallbackMD->operands()) { 104 MDNode *OpMD = cast<MDNode>(Op.get()); 105 auto *CBCalleeIdxAsCM = cast<ConstantAsMetadata>(OpMD->getOperand(0)); 106 uint64_t CBCalleeIdx = 107 cast<ConstantInt>(CBCalleeIdxAsCM->getValue())->getZExtValue(); 108 if (CBCalleeIdx != UseIdx) 109 continue; 110 CallbackEncMD = OpMD; 111 break; 112 } 113 114 if (!CallbackEncMD) { 115 NumInvalidAbstractCallSitesNoCallback++; 116 CS = CallSite(); 117 return; 118 } 119 120 NumCallbackCallSites++; 121 122 assert(CallbackEncMD->getNumOperands() >= 2 && "Incomplete !callback metadata"); 123 124 unsigned NumCallOperands = CS.getNumArgOperands(); 125 // Skip the var-arg flag at the end when reading the metadata. 126 for (unsigned u = 0, e = CallbackEncMD->getNumOperands() - 1; u < e; u++) { 127 Metadata *OpAsM = CallbackEncMD->getOperand(u).get(); 128 auto *OpAsCM = cast<ConstantAsMetadata>(OpAsM); 129 assert(OpAsCM->getType()->isIntegerTy(64) && 130 "Malformed !callback metadata"); 131 132 int64_t Idx = cast<ConstantInt>(OpAsCM->getValue())->getSExtValue(); 133 assert(-1 <= Idx && Idx <= NumCallOperands && 134 "Out-of-bounds !callback metadata index"); 135 136 CI.ParameterEncoding.push_back(Idx); 137 } 138 139 if (!Callee->isVarArg()) 140 return; 141 142 Metadata *VarArgFlagAsM = 143 CallbackEncMD->getOperand(CallbackEncMD->getNumOperands() - 1).get(); 144 auto *VarArgFlagAsCM = cast<ConstantAsMetadata>(VarArgFlagAsM); 145 assert(VarArgFlagAsCM->getType()->isIntegerTy(1) && 146 "Malformed !callback metadata var-arg flag"); 147 148 if (VarArgFlagAsCM->getValue()->isNullValue()) 149 return; 150 151 // Add all variadic arguments at the end. 152 for (unsigned u = Callee->arg_size(); u < NumCallOperands; u++) 153 CI.ParameterEncoding.push_back(u); 154 } 155