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]))
155 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
156 return cast<Function>(CPR->getValue());
159 Function *CallInst::getCalledFunction() {
160 if (Function *F = dyn_cast<Function>(Operands[0]))
162 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
163 return cast<Function>(CPR->getValue());
168 //===----------------------------------------------------------------------===//
169 // InvokeInst Implementation
170 //===----------------------------------------------------------------------===//
172 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
173 const std::vector<Value*> &Params)
175 Operands.reserve(3+Params.size());
176 Operands.push_back(Use(Fn, this));
177 Operands.push_back(Use((Value*)IfNormal, this));
178 Operands.push_back(Use((Value*)IfException, this));
179 const FunctionType *MTy =
180 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
182 assert((Params.size() == MTy->getNumParams()) ||
183 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
184 "Calling a function with bad signature");
186 for (unsigned i = 0; i < Params.size(); i++)
187 Operands.push_back(Use(Params[i], this));
190 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
191 BasicBlock *IfException,
192 const std::vector<Value*> &Params,
193 const std::string &Name, Instruction *InsertBefore)
194 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
195 ->getElementType())->getReturnType(),
196 Instruction::Invoke, Name, InsertBefore) {
197 init(Fn, IfNormal, IfException, Params);
200 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
201 BasicBlock *IfException,
202 const std::vector<Value*> &Params,
203 const std::string &Name, BasicBlock *InsertAtEnd)
204 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
205 ->getElementType())->getReturnType(),
206 Instruction::Invoke, Name, InsertAtEnd) {
207 init(Fn, IfNormal, IfException, Params);
210 InvokeInst::InvokeInst(const InvokeInst &CI)
211 : TerminatorInst(CI.getType(), Instruction::Invoke) {
212 Operands.reserve(CI.Operands.size());
213 for (unsigned i = 0; i < CI.Operands.size(); ++i)
214 Operands.push_back(Use(CI.Operands[i], this));
217 const Function *InvokeInst::getCalledFunction() const {
218 if (const Function *F = dyn_cast<Function>(Operands[0]))
220 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
221 return cast<Function>(CPR->getValue());
224 Function *InvokeInst::getCalledFunction() {
225 if (Function *F = dyn_cast<Function>(Operands[0]))
227 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
228 return cast<Function>(CPR->getValue());
232 Function *CallSite::getCalledFunction() const {
233 Value *Callee = getCalledValue();
234 if (Function *F = dyn_cast<Function>(Callee))
236 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
237 return cast<Function>(CPR->getValue());