Add some missing functions. Make sure to handle calls together in case the
[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 CallInst::CallInst(Value *Func, const std::vector<Value*> &params, 
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));
33
34   const FunctionType *FTy = 
35     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
36
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));
42 }
43
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) {
49   Operands.reserve(1);
50   Operands.push_back(Use(Func, this));
51   
52   const FunctionType *MTy = 
53     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
54
55   assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
56 }
57
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) {
63   Operands.reserve(2);
64   Operands.push_back(Use(Func, this));
65   
66   const FunctionType *MTy = 
67     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
68
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));
73 }
74
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));
80 }
81
82 const Function *CallInst::getCalledFunction() const {
83   if (const Function *F = dyn_cast<Function>(Operands[0]))
84     return F;
85   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
86     return cast<Function>(CPR->getValue());
87   return 0;
88 }
89 Function *CallInst::getCalledFunction() {
90   if (Function *F = dyn_cast<Function>(Operands[0]))
91     return F;
92   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
93     return cast<Function>(CPR->getValue());
94   return 0;
95 }
96
97
98 //===----------------------------------------------------------------------===//
99 //                        InvokeInst Implementation
100 //===----------------------------------------------------------------------===//
101
102 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
103                        BasicBlock *IfException,
104                        const std::vector<Value*> &params,
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());
115   
116   assert((params.size() == MTy->getNumParams()) || 
117          (MTy->isVarArg() && params.size() > MTy->getNumParams()) &&
118          "Calling a function with bad signature");
119   
120   for (unsigned i = 0; i < params.size(); i++)
121     Operands.push_back(Use(params[i], this));
122 }
123
124 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
125                        BasicBlock *IfException,
126                        const std::vector<Value*> &params,
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());
137   
138   assert((params.size() == MTy->getNumParams()) || 
139          (MTy->isVarArg() && params.size() > MTy->getNumParams()) &&
140          "Calling a function with bad signature");
141   
142   for (unsigned i = 0; i < params.size(); i++)
143     Operands.push_back(Use(params[i], this));
144
145   if (InsertAtEnd)
146     InsertAtEnd->getInstList().push_back(this);
147 }
148
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));
154 }
155
156 const Function *InvokeInst::getCalledFunction() const {
157   if (const Function *F = dyn_cast<Function>(Operands[0]))
158     return F;
159   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
160     return cast<Function>(CPR->getValue());
161   return 0;
162 }
163 Function *InvokeInst::getCalledFunction() {
164   if (Function *F = dyn_cast<Function>(Operands[0]))
165     return F;
166   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
167     return cast<Function>(CPR->getValue());
168   return 0;
169 }
170
171 Function *CallSite::getCalledFunction() const {
172   Value *Callee = getCalledValue();
173   if (Function *F = dyn_cast<Function>(Callee))
174     return F;
175   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
176     return cast<Function>(CPR->getValue());
177   return 0;
178 }