[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / IR / AbstractCallSite.cpp
blobb7a81030f41cade00f5b38e92a0e7b824f37ebbf
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.
12 // For more information see:
13 // https://llvm.org/devmtg/2018-10/talk-abstracts.html#talk20
15 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/Support/Debug.h"
22 using namespace llvm;
24 #define DEBUG_TYPE "abstract-call-sites"
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)");
36 /// Create an abstract call site from a use.
37 AbstractCallSite::AbstractCallSite(const Use *U) : CS(U->getUser()) {
39 // First handle unknown users.
40 if (!CS) {
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());
52 if (!CS) {
53 NumInvalidAbstractCallSitesUnknownUse++;
54 return;
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;
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;
74 MDNode *CallbackMD = Callee->getMetadata(LLVMContext::MD_callback);
75 if (!CallbackMD) {
76 NumInvalidAbstractCallSitesNoCallback++;
77 CS = CallSite();
78 return;
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;
94 if (!CallbackEncMD) {
95 NumInvalidAbstractCallSitesNoCallback++;
96 CS = CallSite();
97 return;
100 NumCallbackCallSites++;
102 assert(CallbackEncMD->getNumOperands() >= 2 && "Incomplete !callback metadata");
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");
112 int64_t Idx = cast<ConstantInt>(OpAsCM->getValue())->getSExtValue();
113 assert(-1 <= Idx && Idx <= NumCallOperands &&
114 "Out-of-bounds !callback metadata index");
116 CI.ParameterEncoding.push_back(Idx);
119 if (!Callee->isVarArg())
120 return;
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");
128 if (VarArgFlagAsCM->getValue()->isNullValue())
129 return;
131 // Add all variadic arguments at the end.
132 for (unsigned u = Callee->arg_size(); u < NumCallOperands; u++)
133 CI.ParameterEncoding.push_back(u);