Johannes Doerfert | 1825184 | 2019-01-19 05:19:06 +0000 | [diff] [blame] | 1 | //===-- AbstractCallSite.cpp - Implementation of abstract call sites ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Johannes Doerfert | 1825184 | 2019-01-19 05:19:06 +0000 | [diff] [blame] | 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 | /// Create an abstract call site from a use. |
| 37 | AbstractCallSite::AbstractCallSite(const Use *U) : CS(U->getUser()) { |
| 38 | |
| 39 | // First handle unknown users. |
| 40 | if (!CS) { |
| 41 | |
| 42 | // If the use is actually in a constant cast expression which itself |
| 43 | // has only one use, we look through the constant cast expression. |
| 44 | // This happens by updating the use @p U to the use of the constant |
| 45 | // cast expression and afterwards re-initializing CS accordingly. |
| 46 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U->getUser())) |
| 47 | if (CE->getNumUses() == 1 && CE->isCast()) { |
| 48 | U = &*CE->use_begin(); |
| 49 | CS = CallSite(U->getUser()); |
| 50 | } |
| 51 | |
| 52 | if (!CS) { |
| 53 | NumInvalidAbstractCallSitesUnknownUse++; |
| 54 | return; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Then handle direct or indirect calls. Thus, if U is the callee of the |
| 59 | // call site CS it is not a callback and we are done. |
| 60 | if (CS.isCallee(U)) { |
| 61 | NumDirectAbstractCallSites++; |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // If we cannot identify the broker function we cannot create a callback and |
| 66 | // invalidate the abstract call site. |
| 67 | Function *Callee = CS.getCalledFunction(); |
| 68 | if (!Callee) { |
| 69 | NumInvalidAbstractCallSitesUnknownCallee++; |
| 70 | CS = CallSite(); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | MDNode *CallbackMD = Callee->getMetadata(LLVMContext::MD_callback); |
| 75 | if (!CallbackMD) { |
| 76 | NumInvalidAbstractCallSitesNoCallback++; |
| 77 | CS = CallSite(); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | unsigned UseIdx = CS.getArgumentNo(U); |
| 82 | MDNode *CallbackEncMD = nullptr; |
| 83 | for (const MDOperand &Op : CallbackMD->operands()) { |
| 84 | MDNode *OpMD = cast<MDNode>(Op.get()); |
| 85 | auto *CBCalleeIdxAsCM = cast<ConstantAsMetadata>(OpMD->getOperand(0)); |
| 86 | uint64_t CBCalleeIdx = |
| 87 | cast<ConstantInt>(CBCalleeIdxAsCM->getValue())->getZExtValue(); |
| 88 | if (CBCalleeIdx != UseIdx) |
| 89 | continue; |
| 90 | CallbackEncMD = OpMD; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | if (!CallbackEncMD) { |
| 95 | NumInvalidAbstractCallSitesNoCallback++; |
| 96 | CS = CallSite(); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | NumCallbackCallSites++; |
| 101 | |
| 102 | assert(CallbackEncMD->getNumOperands() >= 2 && "Incomplete !callback metadata"); |
| 103 | |
| 104 | unsigned NumCallOperands = CS.getNumArgOperands(); |
| 105 | // Skip the var-arg flag at the end when reading the metadata. |
| 106 | for (unsigned u = 0, e = CallbackEncMD->getNumOperands() - 1; u < e; u++) { |
| 107 | Metadata *OpAsM = CallbackEncMD->getOperand(u).get(); |
| 108 | auto *OpAsCM = cast<ConstantAsMetadata>(OpAsM); |
| 109 | assert(OpAsCM->getType()->isIntegerTy(64) && |
| 110 | "Malformed !callback metadata"); |
| 111 | |
| 112 | int64_t Idx = cast<ConstantInt>(OpAsCM->getValue())->getSExtValue(); |
| 113 | assert(-1 <= Idx && Idx <= NumCallOperands && |
| 114 | "Out-of-bounds !callback metadata index"); |
| 115 | |
| 116 | CI.ParameterEncoding.push_back(Idx); |
| 117 | } |
| 118 | |
| 119 | if (!Callee->isVarArg()) |
| 120 | return; |
| 121 | |
| 122 | Metadata *VarArgFlagAsM = |
| 123 | CallbackEncMD->getOperand(CallbackEncMD->getNumOperands() - 1).get(); |
| 124 | auto *VarArgFlagAsCM = cast<ConstantAsMetadata>(VarArgFlagAsM); |
| 125 | assert(VarArgFlagAsCM->getType()->isIntegerTy(1) && |
| 126 | "Malformed !callback metadata var-arg flag"); |
| 127 | |
| 128 | if (VarArgFlagAsCM->getValue()->isNullValue()) |
| 129 | return; |
| 130 | |
| 131 | // Add all variadic arguments at the end. |
| 132 | for (unsigned u = Callee->arg_size(); u < NumCallOperands; u++) |
| 133 | CI.ParameterEncoding.push_back(u); |
| 134 | } |