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 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
103 const std::vector<Value*> &Params)
105 Operands.reserve(3+Params.size());
106 Operands.push_back(Use(Fn, this));
107 Operands.push_back(Use((Value*)IfNormal, this));
108 Operands.push_back(Use((Value*)IfException, this));
109 const FunctionType *MTy =
110 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
112 assert((Params.size() == MTy->getNumParams()) ||
113 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
114 "Calling a function with bad signature");
116 for (unsigned i = 0; i < Params.size(); i++)
117 Operands.push_back(Use(Params[i], this));
120 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
121 BasicBlock *IfException,
122 const std::vector<Value*> &Params,
123 const std::string &Name, Instruction *InsertBefore)
124 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
125 ->getElementType())->getReturnType(),
126 Instruction::Invoke, Name, InsertBefore) {
127 init(Fn, IfNormal, IfException, Params);
130 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
131 BasicBlock *IfException,
132 const std::vector<Value*> &Params,
133 const std::string &Name, BasicBlock *InsertAtEnd)
134 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
135 ->getElementType())->getReturnType(),
136 Instruction::Invoke, Name, InsertAtEnd) {
137 init(Fn, IfNormal, IfException, Params);
140 InvokeInst::InvokeInst(const InvokeInst &CI)
141 : TerminatorInst(CI.getType(), Instruction::Invoke) {
142 Operands.reserve(CI.Operands.size());
143 for (unsigned i = 0; i < CI.Operands.size(); ++i)
144 Operands.push_back(Use(CI.Operands[i], this));
147 const Function *InvokeInst::getCalledFunction() const {
148 if (const Function *F = dyn_cast<Function>(Operands[0]))
150 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
151 return cast<Function>(CPR->getValue());
154 Function *InvokeInst::getCalledFunction() {
155 if (Function *F = dyn_cast<Function>(Operands[0]))
157 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
158 return cast<Function>(CPR->getValue());
162 Function *CallSite::getCalledFunction() const {
163 Value *Callee = getCalledValue();
164 if (Function *F = dyn_cast<Function>(Callee))
166 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
167 return cast<Function>(CPR->getValue());