LI can only take signed values, so values > 32767 can only be loaded with ORI
[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   return 0;
156 }
157 Function *CallInst::getCalledFunction() {
158   if (Function *F = dyn_cast<Function>(Operands[0]))
159     return F;
160   return 0;
161 }
162
163
164 //===----------------------------------------------------------------------===//
165 //                        InvokeInst Implementation
166 //===----------------------------------------------------------------------===//
167
168 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
169                       const std::vector<Value*> &Params)
170 {
171   Operands.reserve(3+Params.size());
172   Operands.push_back(Use(Fn, this));
173   Operands.push_back(Use((Value*)IfNormal, this));
174   Operands.push_back(Use((Value*)IfException, this));
175   const FunctionType *MTy = 
176     cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
177   
178   assert((Params.size() == MTy->getNumParams()) || 
179          (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
180          "Calling a function with bad signature");
181   
182   for (unsigned i = 0; i < Params.size(); i++)
183     Operands.push_back(Use(Params[i], this));
184 }
185
186 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
187                        BasicBlock *IfException,
188                        const std::vector<Value*> &Params,
189                        const std::string &Name, Instruction *InsertBefore)
190   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
191                                     ->getElementType())->getReturnType(),
192                    Instruction::Invoke, Name, InsertBefore) {
193   init(Fn, IfNormal, IfException, Params);
194 }
195
196 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
197                        BasicBlock *IfException,
198                        const std::vector<Value*> &Params,
199                        const std::string &Name, BasicBlock *InsertAtEnd)
200   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
201                                     ->getElementType())->getReturnType(),
202                    Instruction::Invoke, Name, InsertAtEnd) {
203   init(Fn, IfNormal, IfException, Params);
204 }
205
206 InvokeInst::InvokeInst(const InvokeInst &CI) 
207   : TerminatorInst(CI.getType(), Instruction::Invoke) {
208   Operands.reserve(CI.Operands.size());
209   for (unsigned i = 0; i < CI.Operands.size(); ++i)
210     Operands.push_back(Use(CI.Operands[i], this));
211 }
212
213 const Function *InvokeInst::getCalledFunction() const {
214   if (const Function *F = dyn_cast<Function>(Operands[0]))
215     return F;
216   return 0;
217 }
218 Function *InvokeInst::getCalledFunction() {
219   if (Function *F = dyn_cast<Function>(Operands[0]))
220     return F;
221   return 0;
222 }
223
224 Function *CallSite::getCalledFunction() const {
225   Value *Callee = getCalledValue();
226   if (Function *F = dyn_cast<Function>(Callee))
227     return F;
228   return 0;
229 }