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 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 *FTy =
35 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
37 assert((params.size() == FTy->getNumParams() ||
38 (FTy->isVarArg() && params.size() > FTy->getNumParams())) &&
39 "Calling a function with bad signature");
40 for (unsigned i = 0; i != params.size(); i++)
41 Operands.push_back(Use(params[i], this));
44 CallInst::CallInst(Value *Func, const std::string &Name,
45 Instruction *InsertBefore)
46 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
47 ->getElementType())->getReturnType(),
48 Instruction::Call, Name, InsertBefore) {
50 Operands.push_back(Use(Func, this));
52 const FunctionType *MTy =
53 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
55 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
58 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
59 Instruction *InsertBefore)
60 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
61 ->getElementType())->getReturnType(),
62 Instruction::Call, Name, InsertBefore) {
64 Operands.push_back(Use(Func, this));
66 const FunctionType *MTy =
67 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
69 assert((MTy->getNumParams() == 1 ||
70 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
71 "Calling a function with bad signature");
72 Operands.push_back(Use(A, this));
75 CallInst::CallInst(const CallInst &CI)
76 : Instruction(CI.getType(), Instruction::Call) {
77 Operands.reserve(CI.Operands.size());
78 for (unsigned i = 0; i < CI.Operands.size(); ++i)
79 Operands.push_back(Use(CI.Operands[i], this));
82 const Function *CallInst::getCalledFunction() const {
83 if (const Function *F = dyn_cast<Function>(Operands[0]))
85 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
86 return cast<Function>(CPR->getValue());
89 Function *CallInst::getCalledFunction() {
90 if (Function *F = dyn_cast<Function>(Operands[0]))
92 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
93 return cast<Function>(CPR->getValue());
98 //===----------------------------------------------------------------------===//
99 // InvokeInst Implementation
100 //===----------------------------------------------------------------------===//
102 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
103 BasicBlock *IfException,
104 const std::vector<Value*> ¶ms,
105 const std::string &Name, Instruction *InsertBefore)
106 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
107 ->getElementType())->getReturnType(),
108 Instruction::Invoke, Name, InsertBefore) {
109 Operands.reserve(3+params.size());
110 Operands.push_back(Use(Func, this));
111 Operands.push_back(Use((Value*)IfNormal, this));
112 Operands.push_back(Use((Value*)IfException, this));
113 const FunctionType *MTy =
114 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
116 assert((params.size() == MTy->getNumParams()) ||
117 (MTy->isVarArg() && params.size() > MTy->getNumParams()) &&
118 "Calling a function with bad signature");
120 for (unsigned i = 0; i < params.size(); i++)
121 Operands.push_back(Use(params[i], this));
124 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
125 BasicBlock *IfException,
126 const std::vector<Value*> ¶ms,
127 const std::string &Name, BasicBlock *InsertAtEnd)
128 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
129 ->getElementType())->getReturnType(),
130 Instruction::Invoke, Name) {
131 Operands.reserve(3+params.size());
132 Operands.push_back(Use(Func, this));
133 Operands.push_back(Use((Value*)IfNormal, this));
134 Operands.push_back(Use((Value*)IfException, this));
135 const FunctionType *MTy =
136 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
138 assert((params.size() == MTy->getNumParams()) ||
139 (MTy->isVarArg() && params.size() > MTy->getNumParams()) &&
140 "Calling a function with bad signature");
142 for (unsigned i = 0; i < params.size(); i++)
143 Operands.push_back(Use(params[i], this));
146 InsertAtEnd->getInstList().push_back(this);
149 InvokeInst::InvokeInst(const InvokeInst &CI)
150 : TerminatorInst(CI.getType(), Instruction::Invoke) {
151 Operands.reserve(CI.Operands.size());
152 for (unsigned i = 0; i < CI.Operands.size(); ++i)
153 Operands.push_back(Use(CI.Operands[i], this));
156 const Function *InvokeInst::getCalledFunction() const {
157 if (const Function *F = dyn_cast<Function>(Operands[0]))
159 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
160 return cast<Function>(CPR->getValue());
163 Function *InvokeInst::getCalledFunction() {
164 if (Function *F = dyn_cast<Function>(Operands[0]))
166 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
167 return cast<Function>(CPR->getValue());
171 Function *CallSite::getCalledFunction() const {
172 Value *Callee = getCalledValue();
173 if (Function *F = dyn_cast<Function>(Callee))
175 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
176 return cast<Function>(CPR->getValue());