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 *Actual)
44 Operands.push_back(Use(Func, this));
46 const FunctionType *MTy =
47 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
49 assert((MTy->getNumParams() == 1 ||
50 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
51 "Calling a function with bad signature");
52 Operands.push_back(Use(Actual, this));
55 void CallInst::init(Value *Func)
58 Operands.push_back(Use(Func, this));
60 const FunctionType *MTy =
61 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
63 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
66 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
67 const std::string &Name, Instruction *InsertBefore)
68 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
69 ->getElementType())->getReturnType(),
70 Instruction::Call, Name, InsertBefore) {
74 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
75 const std::string &Name, BasicBlock *InsertAtEnd)
76 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
77 ->getElementType())->getReturnType(),
78 Instruction::Call, Name, InsertAtEnd) {
82 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
83 Instruction *InsertBefore)
84 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
85 ->getElementType())->getReturnType(),
86 Instruction::Call, Name, InsertBefore) {
90 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
91 BasicBlock *InsertAtEnd)
92 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
93 ->getElementType())->getReturnType(),
94 Instruction::Call, Name, InsertAtEnd) {
98 CallInst::CallInst(Value *Func, const std::string &Name,
99 Instruction *InsertBefore)
100 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
101 ->getElementType())->getReturnType(),
102 Instruction::Call, Name, InsertBefore) {
106 CallInst::CallInst(Value *Func, const std::string &Name,
107 BasicBlock *InsertAtEnd)
108 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
109 ->getElementType())->getReturnType(),
110 Instruction::Call, Name, InsertAtEnd) {
114 CallInst::CallInst(const CallInst &CI)
115 : Instruction(CI.getType(), Instruction::Call) {
116 Operands.reserve(CI.Operands.size());
117 for (unsigned i = 0; i < CI.Operands.size(); ++i)
118 Operands.push_back(Use(CI.Operands[i], this));
121 const Function *CallInst::getCalledFunction() const {
122 if (const Function *F = dyn_cast<Function>(Operands[0]))
124 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
125 return cast<Function>(CPR->getValue());
128 Function *CallInst::getCalledFunction() {
129 if (Function *F = dyn_cast<Function>(Operands[0]))
131 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
132 return cast<Function>(CPR->getValue());
137 //===----------------------------------------------------------------------===//
138 // InvokeInst Implementation
139 //===----------------------------------------------------------------------===//
141 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
142 const std::vector<Value*> &Params)
144 Operands.reserve(3+Params.size());
145 Operands.push_back(Use(Fn, this));
146 Operands.push_back(Use((Value*)IfNormal, this));
147 Operands.push_back(Use((Value*)IfException, this));
148 const FunctionType *MTy =
149 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
151 assert((Params.size() == MTy->getNumParams()) ||
152 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
153 "Calling a function with bad signature");
155 for (unsigned i = 0; i < Params.size(); i++)
156 Operands.push_back(Use(Params[i], this));
159 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
160 BasicBlock *IfException,
161 const std::vector<Value*> &Params,
162 const std::string &Name, Instruction *InsertBefore)
163 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
164 ->getElementType())->getReturnType(),
165 Instruction::Invoke, Name, InsertBefore) {
166 init(Fn, IfNormal, IfException, Params);
169 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
170 BasicBlock *IfException,
171 const std::vector<Value*> &Params,
172 const std::string &Name, BasicBlock *InsertAtEnd)
173 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
174 ->getElementType())->getReturnType(),
175 Instruction::Invoke, Name, InsertAtEnd) {
176 init(Fn, IfNormal, IfException, Params);
179 InvokeInst::InvokeInst(const InvokeInst &CI)
180 : TerminatorInst(CI.getType(), Instruction::Invoke) {
181 Operands.reserve(CI.Operands.size());
182 for (unsigned i = 0; i < CI.Operands.size(); ++i)
183 Operands.push_back(Use(CI.Operands[i], this));
186 const Function *InvokeInst::getCalledFunction() const {
187 if (const Function *F = dyn_cast<Function>(Operands[0]))
189 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
190 return cast<Function>(CPR->getValue());
193 Function *InvokeInst::getCalledFunction() {
194 if (Function *F = dyn_cast<Function>(Operands[0]))
196 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
197 return cast<Function>(CPR->getValue());
201 Function *CallSite::getCalledFunction() const {
202 Value *Callee = getCalledValue();
203 if (Function *F = dyn_cast<Function>(Callee))
205 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
206 return cast<Function>(CPR->getValue());