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"
19 #include "llvm/Support/CallSite.h"
22 //===----------------------------------------------------------------------===//
23 // CallInst Implementation
24 //===----------------------------------------------------------------------===//
26 void CallInst::init(Value *Func, const std::vector<Value*> &Params)
28 Operands.reserve(1+Params.size());
29 Operands.push_back(Use(Func, this));
31 const FunctionType *FTy =
32 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
34 assert((Params.size() == FTy->getNumParams() ||
35 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
36 "Calling a function with bad signature");
37 for (unsigned i = 0; i != Params.size(); i++)
38 Operands.push_back(Use(Params[i], this));
41 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2)
44 Operands.push_back(Use(Func, this));
46 const FunctionType *MTy =
47 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
49 assert((MTy->getNumParams() == 2 ||
50 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
51 "Calling a function with bad signature");
52 Operands.push_back(Use(Actual1, this));
53 Operands.push_back(Use(Actual2, this));
56 void CallInst::init(Value *Func, Value *Actual)
59 Operands.push_back(Use(Func, this));
61 const FunctionType *MTy =
62 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
64 assert((MTy->getNumParams() == 1 ||
65 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
66 "Calling a function with bad signature");
67 Operands.push_back(Use(Actual, this));
70 void CallInst::init(Value *Func)
73 Operands.push_back(Use(Func, this));
75 const FunctionType *MTy =
76 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
78 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
81 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
82 const std::string &Name, Instruction *InsertBefore)
83 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
84 ->getElementType())->getReturnType(),
85 Instruction::Call, Name, InsertBefore) {
89 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
90 const std::string &Name, BasicBlock *InsertAtEnd)
91 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
92 ->getElementType())->getReturnType(),
93 Instruction::Call, Name, InsertAtEnd) {
97 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
98 const std::string &Name, Instruction *InsertBefore)
99 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
100 ->getElementType())->getReturnType(),
101 Instruction::Call, Name, InsertBefore) {
102 init(Func, Actual1, Actual2);
105 CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
106 const std::string &Name, BasicBlock *InsertAtEnd)
107 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
108 ->getElementType())->getReturnType(),
109 Instruction::Call, Name, InsertAtEnd) {
110 init(Func, Actual1, Actual2);
113 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
114 Instruction *InsertBefore)
115 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
116 ->getElementType())->getReturnType(),
117 Instruction::Call, Name, InsertBefore) {
121 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
122 BasicBlock *InsertAtEnd)
123 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
124 ->getElementType())->getReturnType(),
125 Instruction::Call, Name, InsertAtEnd) {
129 CallInst::CallInst(Value *Func, const std::string &Name,
130 Instruction *InsertBefore)
131 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
132 ->getElementType())->getReturnType(),
133 Instruction::Call, Name, InsertBefore) {
137 CallInst::CallInst(Value *Func, const std::string &Name,
138 BasicBlock *InsertAtEnd)
139 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
140 ->getElementType())->getReturnType(),
141 Instruction::Call, Name, InsertAtEnd) {
145 CallInst::CallInst(const CallInst &CI)
146 : Instruction(CI.getType(), Instruction::Call) {
147 Operands.reserve(CI.Operands.size());
148 for (unsigned i = 0; i < CI.Operands.size(); ++i)
149 Operands.push_back(Use(CI.Operands[i], this));
152 const Function *CallInst::getCalledFunction() const {
153 if (const Function *F = dyn_cast<Function>(Operands[0]))
157 Function *CallInst::getCalledFunction() {
158 if (Function *F = dyn_cast<Function>(Operands[0]))
164 //===----------------------------------------------------------------------===//
165 // InvokeInst Implementation
166 //===----------------------------------------------------------------------===//
168 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
169 const std::vector<Value*> &Params)
171 Operands.reserve(3+Params.size());
172 Operands.push_back(Use(Fn, this));
173 Operands.push_back(Use((Value*)IfNormal, this));
174 Operands.push_back(Use((Value*)IfException, this));
175 const FunctionType *MTy =
176 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
178 assert((Params.size() == MTy->getNumParams()) ||
179 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
180 "Calling a function with bad signature");
182 for (unsigned i = 0; i < Params.size(); i++)
183 Operands.push_back(Use(Params[i], this));
186 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
187 BasicBlock *IfException,
188 const std::vector<Value*> &Params,
189 const std::string &Name, Instruction *InsertBefore)
190 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
191 ->getElementType())->getReturnType(),
192 Instruction::Invoke, Name, InsertBefore) {
193 init(Fn, IfNormal, IfException, Params);
196 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
197 BasicBlock *IfException,
198 const std::vector<Value*> &Params,
199 const std::string &Name, BasicBlock *InsertAtEnd)
200 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
201 ->getElementType())->getReturnType(),
202 Instruction::Invoke, Name, InsertAtEnd) {
203 init(Fn, IfNormal, IfException, Params);
206 InvokeInst::InvokeInst(const InvokeInst &CI)
207 : TerminatorInst(CI.getType(), Instruction::Invoke) {
208 Operands.reserve(CI.Operands.size());
209 for (unsigned i = 0; i < CI.Operands.size(); ++i)
210 Operands.push_back(Use(CI.Operands[i], this));
213 const Function *InvokeInst::getCalledFunction() const {
214 if (const Function *F = dyn_cast<Function>(Operands[0]))
218 Function *InvokeInst::getCalledFunction() {
219 if (Function *F = dyn_cast<Function>(Operands[0]))
224 Function *CallSite::getCalledFunction() const {
225 Value *Callee = getCalledValue();
226 if (Function *F = dyn_cast<Function>(Callee))