2d41dcd274349a54c283b08016ce34ad08afd02f
[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 *MTy = 
35     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
36
37   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
38   assert(params.size() == PL.size() || 
39          (MTy->isVarArg() && params.size() > PL.size()) &&
40          "Calling a function with bad signature");
41   for (unsigned i = 0; i < params.size(); i++)
42     Operands.push_back(Use(params[i], this));
43 }
44
45 CallInst::CallInst(Value *Func, const std::string &Name,
46                    Instruction *InsertBefore)
47   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
48                                    ->getElementType())->getReturnType(),
49                 Instruction::Call, Name, InsertBefore) {
50   Operands.reserve(1);
51   Operands.push_back(Use(Func, this));
52   
53   const FunctionType *MTy = 
54     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
55
56   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
57   assert(PL.empty() && "Calling a function with bad signature");
58 }
59
60 CallInst::CallInst(Value *Func, Value* A, const std::string &Name,
61                    Instruction  *InsertBefore)
62   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
63                                    ->getElementType())->getReturnType(),
64                 Instruction::Call, Name, InsertBefore) {
65   Operands.reserve(2);
66   Operands.push_back(Use(Func, this));
67   
68   const FunctionType *MTy = 
69     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
70
71   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
72   assert(PL.size() == 1 || (MTy->isVarArg() && PL.empty()) &&
73          "Calling a function with bad signature");
74   Operands.push_back(Use(A, this));
75 }
76
77 CallInst::CallInst(const CallInst &CI) 
78   : Instruction(CI.getType(), Instruction::Call) {
79   Operands.reserve(CI.Operands.size());
80   for (unsigned i = 0; i < CI.Operands.size(); ++i)
81     Operands.push_back(Use(CI.Operands[i], this));
82 }
83
84 const Function *CallInst::getCalledFunction() const {
85   if (const Function *F = dyn_cast<Function>(Operands[0]))
86     return F;
87   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
88     return cast<Function>(CPR->getValue());
89   return 0;
90 }
91 Function *CallInst::getCalledFunction() {
92   if (Function *F = dyn_cast<Function>(Operands[0]))
93     return F;
94   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
95     return cast<Function>(CPR->getValue());
96   return 0;
97 }
98
99
100 //===----------------------------------------------------------------------===//
101 //                        InvokeInst Implementation
102 //===----------------------------------------------------------------------===//
103
104 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
105                        BasicBlock *IfException,
106                        const std::vector<Value*> &params,
107                        const std::string &Name, Instruction *InsertBefore)
108   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
109                                     ->getElementType())->getReturnType(),
110                    Instruction::Invoke, Name, InsertBefore) {
111   Operands.reserve(3+params.size());
112   Operands.push_back(Use(Func, this));
113   Operands.push_back(Use((Value*)IfNormal, this));
114   Operands.push_back(Use((Value*)IfException, this));
115   const FunctionType *MTy = 
116     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
117   
118   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
119   assert((params.size() == PL.size()) || 
120          (MTy->isVarArg() && params.size() > PL.size()) &&
121          "Calling a function with bad signature");
122   
123   for (unsigned i = 0; i < params.size(); i++)
124     Operands.push_back(Use(params[i], this));
125 }
126
127 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
128                        BasicBlock *IfException,
129                        const std::vector<Value*> &params,
130                        const std::string &Name, BasicBlock *InsertAtEnd)
131   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
132                                     ->getElementType())->getReturnType(),
133                    Instruction::Invoke, Name) {
134   Operands.reserve(3+params.size());
135   Operands.push_back(Use(Func, this));
136   Operands.push_back(Use((Value*)IfNormal, this));
137   Operands.push_back(Use((Value*)IfException, this));
138   const FunctionType *MTy = 
139     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
140   
141   const FunctionType::ParamTypes &PL = MTy->getParamTypes();
142   assert((params.size() == PL.size()) || 
143          (MTy->isVarArg() && params.size() > PL.size()) &&
144          "Calling a function with bad signature");
145   
146   for (unsigned i = 0; i < params.size(); i++)
147     Operands.push_back(Use(params[i], this));
148
149   if (InsertAtEnd)
150     InsertAtEnd->getInstList().push_back(this);
151 }
152
153 InvokeInst::InvokeInst(const InvokeInst &CI) 
154   : TerminatorInst(CI.getType(), Instruction::Invoke) {
155   Operands.reserve(CI.Operands.size());
156   for (unsigned i = 0; i < CI.Operands.size(); ++i)
157     Operands.push_back(Use(CI.Operands[i], this));
158 }
159
160 const Function *InvokeInst::getCalledFunction() const {
161   if (const Function *F = dyn_cast<Function>(Operands[0]))
162     return F;
163   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
164     return cast<Function>(CPR->getValue());
165   return 0;
166 }
167 Function *InvokeInst::getCalledFunction() {
168   if (Function *F = dyn_cast<Function>(Operands[0]))
169     return F;
170   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
171     return cast<Function>(CPR->getValue());
172   return 0;
173 }
174
175 Function *CallSite::getCalledFunction() const {
176   Value *Callee = getCalledValue();
177   if (Function *F = dyn_cast<Function>(Callee))
178     return F;
179   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
180     return cast<Function>(CPR->getValue());
181   return 0;
182 }