Add convinience constructor for function calls with two args.
[oota-llvm.git] / lib / VMCore / iCall.cpp
1 //===-- iCall.cpp - Implement the call & invoke instructions --------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the call and invoke instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //                        CallInst Implementation
24 //===----------------------------------------------------------------------===//
25
26 void CallInst::init(Value *Func, const std::vector<Value*> &Params)
27 {
28   Operands.reserve(1+Params.size());
29   Operands.push_back(Use(Func, this));
30
31   const FunctionType *FTy = 
32     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
33
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));
39 }
40
41 void CallInst::init(Value *Func, Value *Actual1, Value *Actual2)
42 {
43   Operands.reserve(3);
44   Operands.push_back(Use(Func, this));
45   
46   const FunctionType *MTy = 
47     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
48
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));
54 }
55
56 void CallInst::init(Value *Func, Value *Actual)
57 {
58   Operands.reserve(2);
59   Operands.push_back(Use(Func, this));
60   
61   const FunctionType *MTy = 
62     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
63
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));
68 }
69
70 void CallInst::init(Value *Func)
71 {
72   Operands.reserve(1);
73   Operands.push_back(Use(Func, this));
74   
75   const FunctionType *MTy = 
76     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
77
78   assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
79 }
80
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) {
86   init(Func, Params);
87 }
88
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) {
94   init(Func, Params);
95 }
96
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);
103 }
104
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);
111 }
112
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) {
118   init(Func, Actual);
119 }
120
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) {
126   init(Func, Actual);
127 }
128
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) {
134   init(Func);
135 }
136
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) {
142   init(Func);
143 }
144
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));
150 }
151
152 const Function *CallInst::getCalledFunction() const {
153   if (const Function *F = dyn_cast<Function>(Operands[0]))
154     return F;
155   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
156     return cast<Function>(CPR->getValue());
157   return 0;
158 }
159 Function *CallInst::getCalledFunction() {
160   if (Function *F = dyn_cast<Function>(Operands[0]))
161     return F;
162   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
163     return cast<Function>(CPR->getValue());
164   return 0;
165 }
166
167
168 //===----------------------------------------------------------------------===//
169 //                        InvokeInst Implementation
170 //===----------------------------------------------------------------------===//
171
172 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
173                       const std::vector<Value*> &Params)
174 {
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());
181   
182   assert((Params.size() == MTy->getNumParams()) || 
183          (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
184          "Calling a function with bad signature");
185   
186   for (unsigned i = 0; i < Params.size(); i++)
187     Operands.push_back(Use(Params[i], this));
188 }
189
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);
198 }
199
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);
208 }
209
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));
215 }
216
217 const Function *InvokeInst::getCalledFunction() const {
218   if (const Function *F = dyn_cast<Function>(Operands[0]))
219     return F;
220   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
221     return cast<Function>(CPR->getValue());
222   return 0;
223 }
224 Function *InvokeInst::getCalledFunction() {
225   if (Function *F = dyn_cast<Function>(Operands[0]))
226     return F;
227   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
228     return cast<Function>(CPR->getValue());
229   return 0;
230 }
231
232 Function *CallSite::getCalledFunction() const {
233   Value *Callee = getCalledValue();
234   if (Function *F = dyn_cast<Function>(Callee))
235     return F;
236   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
237     return cast<Function>(CPR->getValue());
238   return 0;
239 }