1 //===-- iCall.cpp - Implement the call & invoke instructions --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the call and invoke instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/iOther.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
22 //===----------------------------------------------------------------------===//
23 // CallInst Implementation
24 //===----------------------------------------------------------------------===//
26 CallInst::CallInst(Value *Func, const std::vector<Value*> ¶ms,
27 const std::string &Name, Instruction *InsertBefore)
28 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
29 ->getElementType())->getReturnType(),
30 Instruction::Call, Name, InsertBefore) {
31 Operands.reserve(1+params.size());
32 Operands.push_back(Use(Func, this));
34 const FunctionType *MTy =
35 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
37 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
38 assert(params.size() == PL.size() ||
39 (MTy->isVarArg() && params.size() > PL.size()) &&
40 "Calling a function with bad signature");
41 for (unsigned i = 0; i < params.size(); i++)
42 Operands.push_back(Use(params[i], this));
45 CallInst::CallInst(Value *Func, const std::string &Name,
46 Instruction *InsertBefore)
47 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
48 ->getElementType())->getReturnType(),
49 Instruction::Call, Name, InsertBefore) {
51 Operands.push_back(Use(Func, this));
53 const FunctionType *MTy =
54 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
56 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
57 assert(PL.empty() && "Calling a function with bad signature");
60 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
61 Instruction *InsertBefore)
62 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
63 ->getElementType())->getReturnType(),
64 Instruction::Call, Name, InsertBefore) {
66 Operands.push_back(Use(Func, this));
68 const FunctionType *MTy =
69 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
71 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
72 assert(PL.size() == 1 || (MTy->isVarArg() && PL.empty()) &&
73 "Calling a function with bad signature");
74 Operands.push_back(Use(A, this));
77 CallInst::CallInst(const CallInst &CI)
78 : Instruction(CI.getType(), Instruction::Call) {
79 Operands.reserve(CI.Operands.size());
80 for (unsigned i = 0; i < CI.Operands.size(); ++i)
81 Operands.push_back(Use(CI.Operands[i], this));
84 const Function *CallInst::getCalledFunction() const {
85 if (const Function *F = dyn_cast<Function>(Operands[0]))
87 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
88 return cast<Function>(CPR->getValue());
91 Function *CallInst::getCalledFunction() {
92 if (Function *F = dyn_cast<Function>(Operands[0]))
94 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
95 return cast<Function>(CPR->getValue());
100 //===----------------------------------------------------------------------===//
101 // InvokeInst Implementation
102 //===----------------------------------------------------------------------===//
104 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
105 BasicBlock *IfException,
106 const std::vector<Value*> ¶ms,
107 const std::string &Name, Instruction *InsertBefore)
108 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
109 ->getElementType())->getReturnType(),
110 Instruction::Invoke, Name, InsertBefore) {
111 Operands.reserve(3+params.size());
112 Operands.push_back(Use(Func, this));
113 Operands.push_back(Use((Value*)IfNormal, this));
114 Operands.push_back(Use((Value*)IfException, this));
115 const FunctionType *MTy =
116 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
118 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
119 assert((params.size() == PL.size()) ||
120 (MTy->isVarArg() && params.size() > PL.size()) &&
121 "Calling a function with bad signature");
123 for (unsigned i = 0; i < params.size(); i++)
124 Operands.push_back(Use(params[i], this));
127 InvokeInst::InvokeInst(const InvokeInst &CI)
128 : TerminatorInst(CI.getType(), Instruction::Invoke) {
129 Operands.reserve(CI.Operands.size());
130 for (unsigned i = 0; i < CI.Operands.size(); ++i)
131 Operands.push_back(Use(CI.Operands[i], this));
134 const Function *InvokeInst::getCalledFunction() const {
135 if (const Function *F = dyn_cast<Function>(Operands[0]))
137 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
138 return cast<Function>(CPR->getValue());
141 Function *InvokeInst::getCalledFunction() {
142 if (Function *F = dyn_cast<Function>(Operands[0]))
144 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
145 return cast<Function>(CPR->getValue());
149 } // End llvm namespace
151 #include "llvm/Support/CallSite.h"
155 Function *CallSite::getCalledFunction() const {
156 Value *Callee = getCalledValue();
157 if (Function *F = dyn_cast<Function>(Callee))
159 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
160 return cast<Function>(CPR->getValue());
164 } // End llvm namespace