I misled Alkis: LLVM should have isnan, not isunordered.
[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 *Actual)
42 {
43   Operands.reserve(2);
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() == 1 ||
50           (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
51          "Calling a function with bad signature");
52   Operands.push_back(Use(Actual, this));
53 }
54
55 void CallInst::init(Value *Func)
56 {
57   Operands.reserve(1);
58   Operands.push_back(Use(Func, this));
59   
60   const FunctionType *MTy = 
61     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
62
63   assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
64 }
65
66 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, 
67                    const std::string &Name, Instruction *InsertBefore) 
68   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
69                                  ->getElementType())->getReturnType(),
70                 Instruction::Call, Name, InsertBefore) {
71   init(Func, Params);
72 }
73
74 CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, 
75                    const std::string &Name, BasicBlock *InsertAtEnd) 
76   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
77                                  ->getElementType())->getReturnType(),
78                 Instruction::Call, Name, InsertAtEnd) {
79   init(Func, Params);
80 }
81
82 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
83                    Instruction  *InsertBefore)
84   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
85                                    ->getElementType())->getReturnType(),
86                 Instruction::Call, Name, InsertBefore) {
87   init(Func, Actual);
88 }
89
90 CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
91                    BasicBlock  *InsertAtEnd)
92   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
93                                    ->getElementType())->getReturnType(),
94                 Instruction::Call, Name, InsertAtEnd) {
95   init(Func, Actual);
96 }
97
98 CallInst::CallInst(Value *Func, const std::string &Name,
99                    Instruction *InsertBefore)
100   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
101                                    ->getElementType())->getReturnType(),
102                 Instruction::Call, Name, InsertBefore) {
103   init(Func);
104 }
105
106 CallInst::CallInst(Value *Func, const std::string &Name,
107                    BasicBlock *InsertAtEnd)
108   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
109                                    ->getElementType())->getReturnType(),
110                 Instruction::Call, Name, InsertAtEnd) {
111   init(Func);
112 }
113
114 CallInst::CallInst(const CallInst &CI) 
115   : Instruction(CI.getType(), Instruction::Call) {
116   Operands.reserve(CI.Operands.size());
117   for (unsigned i = 0; i < CI.Operands.size(); ++i)
118     Operands.push_back(Use(CI.Operands[i], this));
119 }
120
121 const Function *CallInst::getCalledFunction() const {
122   if (const Function *F = dyn_cast<Function>(Operands[0]))
123     return F;
124   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
125     return cast<Function>(CPR->getValue());
126   return 0;
127 }
128 Function *CallInst::getCalledFunction() {
129   if (Function *F = dyn_cast<Function>(Operands[0]))
130     return F;
131   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
132     return cast<Function>(CPR->getValue());
133   return 0;
134 }
135
136
137 //===----------------------------------------------------------------------===//
138 //                        InvokeInst Implementation
139 //===----------------------------------------------------------------------===//
140
141 void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
142                       const std::vector<Value*> &Params)
143 {
144   Operands.reserve(3+Params.size());
145   Operands.push_back(Use(Fn, this));
146   Operands.push_back(Use((Value*)IfNormal, this));
147   Operands.push_back(Use((Value*)IfException, this));
148   const FunctionType *MTy = 
149     cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
150   
151   assert((Params.size() == MTy->getNumParams()) || 
152          (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
153          "Calling a function with bad signature");
154   
155   for (unsigned i = 0; i < Params.size(); i++)
156     Operands.push_back(Use(Params[i], this));
157 }
158
159 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
160                        BasicBlock *IfException,
161                        const std::vector<Value*> &Params,
162                        const std::string &Name, Instruction *InsertBefore)
163   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
164                                     ->getElementType())->getReturnType(),
165                    Instruction::Invoke, Name, InsertBefore) {
166   init(Fn, IfNormal, IfException, Params);
167 }
168
169 InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
170                        BasicBlock *IfException,
171                        const std::vector<Value*> &Params,
172                        const std::string &Name, BasicBlock *InsertAtEnd)
173   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
174                                     ->getElementType())->getReturnType(),
175                    Instruction::Invoke, Name, InsertAtEnd) {
176   init(Fn, IfNormal, IfException, Params);
177 }
178
179 InvokeInst::InvokeInst(const InvokeInst &CI) 
180   : TerminatorInst(CI.getType(), Instruction::Invoke) {
181   Operands.reserve(CI.Operands.size());
182   for (unsigned i = 0; i < CI.Operands.size(); ++i)
183     Operands.push_back(Use(CI.Operands[i], this));
184 }
185
186 const Function *InvokeInst::getCalledFunction() const {
187   if (const Function *F = dyn_cast<Function>(Operands[0]))
188     return F;
189   if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
190     return cast<Function>(CPR->getValue());
191   return 0;
192 }
193 Function *InvokeInst::getCalledFunction() {
194   if (Function *F = dyn_cast<Function>(Operands[0]))
195     return F;
196   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Operands[0]))
197     return cast<Function>(CPR->getValue());
198   return 0;
199 }
200
201 Function *CallSite::getCalledFunction() const {
202   Value *Callee = getCalledValue();
203   if (Function *F = dyn_cast<Function>(Callee))
204     return F;
205   if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Callee))
206     return cast<Function>(CPR->getValue());
207   return 0;
208 }