blob: efeb2c3766f19f27f60942bd5efd45097644448c [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Support/CallSite.h"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000037//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000038// TerminatorInst Class
39//===----------------------------------------------------------------------===//
40
41TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000042 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000043 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
44}
45
46TerminatorInst::TerminatorInst(Instruction::TermOps iType,
47 Use *Ops, unsigned NumOps, BasicBlock *IAE)
48 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
49}
50
51
52
53//===----------------------------------------------------------------------===//
54// PHINode Class
55//===----------------------------------------------------------------------===//
56
57PHINode::PHINode(const PHINode &PN)
58 : Instruction(PN.getType(), Instruction::PHI,
59 new Use[PN.getNumOperands()], PN.getNumOperands()),
60 ReservedSpace(PN.getNumOperands()) {
61 Use *OL = OperandList;
62 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
63 OL[i].init(PN.getOperand(i), this);
64 OL[i+1].init(PN.getOperand(i+1), this);
65 }
66}
67
68PHINode::~PHINode() {
69 delete [] OperandList;
70}
71
72// removeIncomingValue - Remove an incoming value. This is useful if a
73// predecessor basic block is deleted.
74Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
75 unsigned NumOps = getNumOperands();
76 Use *OL = OperandList;
77 assert(Idx*2 < NumOps && "BB not in PHI node!");
78 Value *Removed = OL[Idx*2];
79
80 // Move everything after this operand down.
81 //
82 // FIXME: we could just swap with the end of the list, then erase. However,
83 // client might not expect this to happen. The code as it is thrashes the
84 // use/def lists, which is kinda lame.
85 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
86 OL[i-2] = OL[i];
87 OL[i-2+1] = OL[i+1];
88 }
89
90 // Nuke the last value.
91 OL[NumOps-2].set(0);
92 OL[NumOps-2+1].set(0);
93 NumOperands = NumOps-2;
94
95 // If the PHI node is dead, because it has zero entries, nuke it now.
96 if (NumOps == 2 && DeletePHIIfEmpty) {
97 // If anyone is using this PHI, make them use a dummy value instead...
98 replaceAllUsesWith(UndefValue::get(getType()));
99 eraseFromParent();
100 }
101 return Removed;
102}
103
104/// resizeOperands - resize operands - This adjusts the length of the operands
105/// list according to the following behavior:
106/// 1. If NumOps == 0, grow the operand list in response to a push_back style
107/// of operation. This grows the number of ops by 1.5 times.
108/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
109/// 3. If NumOps == NumOperands, trim the reserved space.
110///
111void PHINode::resizeOperands(unsigned NumOps) {
112 if (NumOps == 0) {
113 NumOps = (getNumOperands())*3/2;
114 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
115 } else if (NumOps*2 > NumOperands) {
116 // No resize needed.
117 if (ReservedSpace >= NumOps) return;
118 } else if (NumOps == NumOperands) {
119 if (ReservedSpace == NumOps) return;
120 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000121 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 }
123
124 ReservedSpace = NumOps;
125 Use *NewOps = new Use[NumOps];
126 Use *OldOps = OperandList;
127 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
128 NewOps[i].init(OldOps[i], this);
129 OldOps[i].set(0);
130 }
131 delete [] OldOps;
132 OperandList = NewOps;
133}
134
Nate Begemanb3923212005-08-04 23:24:19 +0000135/// hasConstantValue - If the specified PHI node always merges together the same
136/// value, return the value, otherwise return null.
137///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000138Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000139 // If the PHI node only has one incoming value, eliminate the PHI node...
140 if (getNumIncomingValues() == 1)
141 return getIncomingValue(0);
142
143 // Otherwise if all of the incoming values are the same for the PHI, replace
144 // the PHI node with the incoming value.
145 //
146 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000147 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000148 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000149 if (isa<UndefValue>(getIncomingValue(i)))
150 HasUndefInput = true;
151 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000152 if (InVal && getIncomingValue(i) != InVal)
153 return 0; // Not the same, bail out.
154 else
155 InVal = getIncomingValue(i);
156
157 // The only case that could cause InVal to be null is if we have a PHI node
158 // that only has entries for itself. In this case, there is no entry into the
159 // loop, so kill the PHI.
160 //
161 if (InVal == 0) InVal = UndefValue::get(getType());
162
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000163 // If we have a PHI node like phi(X, undef, X), where X is defined by some
164 // instruction, we cannot always return X as the result of the PHI node. Only
165 // do this if X is not an instruction (thus it must dominate the PHI block),
166 // or if the client is prepared to deal with this possibility.
167 if (HasUndefInput && !AllowNonDominatingInstruction)
168 if (Instruction *IV = dyn_cast<Instruction>(InVal))
169 // If it's in the entry block, it dominates everything.
170 if (IV->getParent() != &IV->getParent()->getParent()->front())
171 return 0; // Cannot guarantee that InVal dominates this PHINode.
172
Nate Begemanb3923212005-08-04 23:24:19 +0000173 // All of the incoming values are the same, return the value now.
174 return InVal;
175}
176
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177
178//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000179// CallInst Implementation
180//===----------------------------------------------------------------------===//
181
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000182CallInst::~CallInst() {
183 delete [] OperandList;
184}
185
186void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
187 NumOperands = Params.size()+1;
188 Use *OL = OperandList = new Use[Params.size()+1];
189 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000190
Misha Brukmanb1c93172005-04-21 23:48:37 +0000191 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
193
Misha Brukmanb1c93172005-04-21 23:48:37 +0000194 assert((Params.size() == FTy->getNumParams() ||
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000195 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
196 "Calling a function with bad signature");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000197 for (unsigned i = 0, e = Params.size(); i != e; ++i)
198 OL[i+1].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199}
200
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000201void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
202 NumOperands = 3;
203 Use *OL = OperandList = new Use[3];
204 OL[0].init(Func, this);
205 OL[1].init(Actual1, this);
206 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000207
208 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000209 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
210
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211 assert((FTy->getNumParams() == 2 ||
212 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000213 "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214}
215
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000216void CallInst::init(Value *Func, Value *Actual) {
217 NumOperands = 2;
218 Use *OL = OperandList = new Use[2];
219 OL[0].init(Func, this);
220 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000221
222 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000223 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
224
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000225 assert((FTy->getNumParams() == 1 ||
226 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228}
229
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230void CallInst::init(Value *Func) {
231 NumOperands = 1;
232 Use *OL = OperandList = new Use[1];
233 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000234
235 const FunctionType *MTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
237
238 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
239}
240
Misha Brukmanb1c93172005-04-21 23:48:37 +0000241CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
242 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000243 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
244 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000245 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 init(Func, Params);
247}
248
Misha Brukmanb1c93172005-04-21 23:48:37 +0000249CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
250 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
252 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254 init(Func, Params);
255}
256
257CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
258 const std::string &Name, Instruction *InsertBefore)
259 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
260 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000261 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 init(Func, Actual1, Actual2);
263}
264
265CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
266 const std::string &Name, BasicBlock *InsertAtEnd)
267 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
268 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000269 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000270 init(Func, Actual1, Actual2);
271}
272
273CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
274 Instruction *InsertBefore)
275 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
276 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000277 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 init(Func, Actual);
279}
280
281CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
282 BasicBlock *InsertAtEnd)
283 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
284 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000285 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286 init(Func, Actual);
287}
288
289CallInst::CallInst(Value *Func, const std::string &Name,
290 Instruction *InsertBefore)
291 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
292 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000293 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 init(Func);
295}
296
297CallInst::CallInst(Value *Func, const std::string &Name,
298 BasicBlock *InsertAtEnd)
299 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
300 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 init(Func);
303}
304
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
307 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000308 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000309 Use *OL = OperandList;
310 Use *InOL = CI.OperandList;
311 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
312 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315
316//===----------------------------------------------------------------------===//
317// InvokeInst Implementation
318//===----------------------------------------------------------------------===//
319
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000320InvokeInst::~InvokeInst() {
321 delete [] OperandList;
322}
323
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000325 const std::vector<Value*> &Params) {
326 NumOperands = 3+Params.size();
327 Use *OL = OperandList = new Use[3+Params.size()];
328 OL[0].init(Fn, this);
329 OL[1].init(IfNormal, this);
330 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000331 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000333
334 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000335 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338 for (unsigned i = 0, e = Params.size(); i != e; i++)
339 OL[i+3].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
342InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
343 BasicBlock *IfException,
344 const std::vector<Value*> &Params,
345 const std::string &Name, Instruction *InsertBefore)
346 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
347 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000348 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000349 init(Fn, IfNormal, IfException, Params);
350}
351
352InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
353 BasicBlock *IfException,
354 const std::vector<Value*> &Params,
355 const std::string &Name, BasicBlock *InsertAtEnd)
356 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
357 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000358 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000359 init(Fn, IfNormal, IfException, Params);
360}
361
Misha Brukmanb1c93172005-04-21 23:48:37 +0000362InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000363 : TerminatorInst(II.getType(), Instruction::Invoke,
364 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000365 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000366 Use *OL = OperandList, *InOL = II.OperandList;
367 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
368 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000369}
370
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000371BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
372 return getSuccessor(idx);
373}
374unsigned InvokeInst::getNumSuccessorsV() const {
375 return getNumSuccessors();
376}
377void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
378 return setSuccessor(idx, B);
379}
380
381
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000382//===----------------------------------------------------------------------===//
383// ReturnInst Implementation
384//===----------------------------------------------------------------------===//
385
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000386void ReturnInst::init(Value *retVal) {
387 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000388 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000389 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000390 NumOperands = 1;
391 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000392 }
393}
394
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000395unsigned ReturnInst::getNumSuccessorsV() const {
396 return getNumSuccessors();
397}
398
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
400// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000401void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000402 assert(0 && "ReturnInst has no successors!");
403}
404
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000405BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
406 assert(0 && "ReturnInst has no successors!");
407 abort();
408 return 0;
409}
410
411
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000412//===----------------------------------------------------------------------===//
413// UnwindInst Implementation
414//===----------------------------------------------------------------------===//
415
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000416unsigned UnwindInst::getNumSuccessorsV() const {
417 return getNumSuccessors();
418}
419
420void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000421 assert(0 && "UnwindInst has no successors!");
422}
423
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000424BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
425 assert(0 && "UnwindInst has no successors!");
426 abort();
427 return 0;
428}
429
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000430//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000431// UnreachableInst Implementation
432//===----------------------------------------------------------------------===//
433
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000434unsigned UnreachableInst::getNumSuccessorsV() const {
435 return getNumSuccessors();
436}
437
438void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
439 assert(0 && "UnwindInst has no successors!");
440}
441
442BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
443 assert(0 && "UnwindInst has no successors!");
444 abort();
445 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000446}
447
448//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000449// BranchInst Implementation
450//===----------------------------------------------------------------------===//
451
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000452void BranchInst::AssertOK() {
453 if (isConditional())
Misha Brukmanb1c93172005-04-21 23:48:37 +0000454 assert(getCondition()->getType() == Type::BoolTy &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456}
457
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458BranchInst::BranchInst(const BranchInst &BI) :
459 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
460 OperandList[0].init(BI.getOperand(0), this);
461 if (BI.getNumOperands() != 1) {
462 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
463 OperandList[1].init(BI.getOperand(1), this);
464 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000465 }
466}
467
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000468BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
469 return getSuccessor(idx);
470}
471unsigned BranchInst::getNumSuccessorsV() const {
472 return getNumSuccessors();
473}
474void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
475 setSuccessor(idx, B);
476}
477
478
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000479//===----------------------------------------------------------------------===//
480// AllocationInst Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000483static Value *getAISize(Value *Amt) {
484 if (!Amt)
485 Amt = ConstantUInt::get(Type::UIntTy, 1);
486 else
487 assert(Amt->getType() == Type::UIntTy &&
488 "Malloc/Allocation array size != UIntTy!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000489 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000490}
491
Misha Brukmanb1c93172005-04-21 23:48:37 +0000492AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000493 const std::string &Name,
494 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000495 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
496 Name, InsertBefore) {
497 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498}
499
Misha Brukmanb1c93172005-04-21 23:48:37 +0000500AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000501 const std::string &Name,
502 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000503 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
504 Name, InsertAtEnd) {
505 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000506}
507
508bool AllocationInst::isArrayAllocation() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000509 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
510 return CUI->getValue() != 1;
511 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000512}
513
514const Type *AllocationInst::getAllocatedType() const {
515 return getType()->getElementType();
516}
517
518AllocaInst::AllocaInst(const AllocaInst &AI)
519 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
520 Instruction::Alloca) {
521}
522
523MallocInst::MallocInst(const MallocInst &MI)
524 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
525 Instruction::Malloc) {
526}
527
528//===----------------------------------------------------------------------===//
529// FreeInst Implementation
530//===----------------------------------------------------------------------===//
531
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000532void FreeInst::AssertOK() {
533 assert(isa<PointerType>(getOperand(0)->getType()) &&
534 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000535}
536
537FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000538 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
539 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540}
541
542FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
544 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000545}
546
547
548//===----------------------------------------------------------------------===//
549// LoadInst Implementation
550//===----------------------------------------------------------------------===//
551
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000552void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000553 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000554 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000555}
556
557LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000558 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000559 Load, Ptr, Name, InsertBef) {
560 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000561 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000562}
563
564LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000565 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000566 Load, Ptr, Name, InsertAE) {
567 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000568 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569}
570
571LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
572 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000573 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000574 Load, Ptr, Name, InsertBef) {
575 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000577}
578
579LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
580 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000582 Load, Ptr, Name, InsertAE) {
583 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000585}
586
587
588//===----------------------------------------------------------------------===//
589// StoreInst Implementation
590//===----------------------------------------------------------------------===//
591
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000592void StoreInst::AssertOK() {
593 assert(isa<PointerType>(getOperand(1)->getType()) &&
594 "Ptr must have pointer type!");
595 assert(getOperand(0)->getType() ==
596 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000597 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000598}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600
601StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000602 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000603 Ops[0].init(val, this);
604 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000605 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000606 AssertOK();
607}
608
609StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000610 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000611 Ops[0].init(val, this);
612 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000613 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614 AssertOK();
615}
616
Misha Brukmanb1c93172005-04-21 23:48:37 +0000617StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000619 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000620 Ops[0].init(val, this);
621 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000622 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000623 AssertOK();
624}
625
Misha Brukmanb1c93172005-04-21 23:48:37 +0000626StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000628 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000629 Ops[0].init(val, this);
630 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000631 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000632 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000633}
634
635//===----------------------------------------------------------------------===//
636// GetElementPtrInst Implementation
637//===----------------------------------------------------------------------===//
638
639// checkType - Simple wrapper function to give a better assertion failure
640// message on bad indexes for a gep instruction.
641//
642static inline const Type *checkType(const Type *Ty) {
643 assert(Ty && "Invalid indices for type!");
644 return Ty;
645}
646
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
648 NumOperands = 1+Idx.size();
649 Use *OL = OperandList = new Use[NumOperands];
650 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000651
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
653 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000654}
655
656void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 NumOperands = 3;
658 Use *OL = OperandList = new Use[3];
659 OL[0].init(Ptr, this);
660 OL[1].init(Idx0, this);
661 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000662}
663
Chris Lattner82981202005-05-03 05:43:30 +0000664void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
665 NumOperands = 2;
666 Use *OL = OperandList = new Use[2];
667 OL[0].init(Ptr, this);
668 OL[1].init(Idx, this);
669}
670
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000671GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000672 const std::string &Name, Instruction *InBe)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000673 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
674 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000675 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000676 init(Ptr, Idx);
677}
678
679GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000680 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
682 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000683 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684 init(Ptr, Idx);
685}
686
Chris Lattner82981202005-05-03 05:43:30 +0000687GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
688 const std::string &Name, Instruction *InBe)
689 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
690 GetElementPtr, 0, 0, Name, InBe) {
691 init(Ptr, Idx);
692}
693
694GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
695 const std::string &Name, BasicBlock *IAE)
696 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
697 GetElementPtr, 0, 0, Name, IAE) {
698 init(Ptr, Idx);
699}
700
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000701GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
702 const std::string &Name, Instruction *InBe)
703 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
704 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000706 init(Ptr, Idx0, Idx1);
707}
708
709GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000710 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000711 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
712 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000714 init(Ptr, Idx0, Idx1);
715}
716
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717GetElementPtrInst::~GetElementPtrInst() {
718 delete[] OperandList;
719}
720
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721// getIndexedType - Returns the type of the element that would be loaded with
722// a load instruction with the specified parameters.
723//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000724// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725// pointer type.
726//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000727const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728 const std::vector<Value*> &Idx,
729 bool AllowCompositeLeaf) {
730 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
731
732 // Handle the special case of the empty set index set...
733 if (Idx.empty())
734 if (AllowCompositeLeaf ||
735 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
736 return cast<PointerType>(Ptr)->getElementType();
737 else
738 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000739
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740 unsigned CurIdx = 0;
741 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
742 if (Idx.size() == CurIdx) {
743 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
744 return 0; // Can't load a whole structure or array!?!?
745 }
746
747 Value *Index = Idx[CurIdx++];
748 if (isa<PointerType>(CT) && CurIdx != 1)
749 return 0; // Can only index into pointer types at the first index!
750 if (!CT->indexValid(Index)) return 0;
751 Ptr = CT->getTypeAtIndex(Index);
752
753 // If the new type forwards to another type, then it is in the middle
754 // of being refined to another type (and hence, may have dropped all
755 // references to what it was using before). So, use the new forwarded
756 // type.
757 if (const Type * Ty = Ptr->getForwardedType()) {
758 Ptr = Ty;
759 }
760 }
761 return CurIdx == Idx.size() ? Ptr : 0;
762}
763
Misha Brukmanb1c93172005-04-21 23:48:37 +0000764const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765 Value *Idx0, Value *Idx1,
766 bool AllowCompositeLeaf) {
767 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
768 if (!PTy) return 0; // Type isn't a pointer type!
769
770 // Check the pointer index.
771 if (!PTy->indexValid(Idx0)) return 0;
772
773 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
774 if (!CT || !CT->indexValid(Idx1)) return 0;
775
776 const Type *ElTy = CT->getTypeAtIndex(Idx1);
777 if (AllowCompositeLeaf || ElTy->isFirstClassType())
778 return ElTy;
779 return 0;
780}
781
Chris Lattner82981202005-05-03 05:43:30 +0000782const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
783 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
784 if (!PTy) return 0; // Type isn't a pointer type!
785
786 // Check the pointer index.
787 if (!PTy->indexValid(Idx)) return 0;
788
Chris Lattnerc2233332005-05-03 16:44:45 +0000789 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000790}
791
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000792//===----------------------------------------------------------------------===//
793// BinaryOperator Class
794//===----------------------------------------------------------------------===//
795
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000796void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000797{
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000798 Value *LHS = getOperand(0), *RHS = getOperand(1);
799 assert(LHS->getType() == RHS->getType() &&
800 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801#ifndef NDEBUG
802 switch (iType) {
803 case Add: case Sub:
804 case Mul: case Div:
805 case Rem:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000806 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000807 "Arithmetic operation should return same type as operands!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000808 assert((getType()->isInteger() ||
809 getType()->isFloatingPoint() ||
810 isa<PackedType>(getType()) ) &&
Brian Gaeke02209042004-08-20 06:00:58 +0000811 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812 break;
813 case And: case Or:
814 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816 "Logical operation should return same type as operands!");
817 assert(getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +0000818 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819 break;
820 case SetLT: case SetGT: case SetLE:
821 case SetGE: case SetEQ: case SetNE:
822 assert(getType() == Type::BoolTy && "Setcc must return bool!");
823 default:
824 break;
825 }
826#endif
827}
828
829BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000830 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000831 Instruction *InsertBefore) {
832 assert(S1->getType() == S2->getType() &&
833 "Cannot create binary operator with two operands of differing type!");
834 switch (Op) {
835 // Binary comparison operators...
836 case SetLT: case SetGT: case SetLE:
837 case SetGE: case SetEQ: case SetNE:
838 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
839
840 default:
841 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
842 }
843}
844
845BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000846 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000847 BasicBlock *InsertAtEnd) {
848 BinaryOperator *Res = create(Op, S1, S2, Name);
849 InsertAtEnd->getInstList().push_back(Res);
850 return Res;
851}
852
853BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
854 Instruction *InsertBefore) {
855 if (!Op->getType()->isFloatingPoint())
856 return new BinaryOperator(Instruction::Sub,
857 Constant::getNullValue(Op->getType()), Op,
858 Op->getType(), Name, InsertBefore);
859 else
860 return new BinaryOperator(Instruction::Sub,
861 ConstantFP::get(Op->getType(), -0.0), Op,
862 Op->getType(), Name, InsertBefore);
863}
864
865BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
866 BasicBlock *InsertAtEnd) {
867 if (!Op->getType()->isFloatingPoint())
868 return new BinaryOperator(Instruction::Sub,
869 Constant::getNullValue(Op->getType()), Op,
870 Op->getType(), Name, InsertAtEnd);
871 else
872 return new BinaryOperator(Instruction::Sub,
873 ConstantFP::get(Op->getType(), -0.0), Op,
874 Op->getType(), Name, InsertAtEnd);
875}
876
877BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
878 Instruction *InsertBefore) {
879 return new BinaryOperator(Instruction::Xor, Op,
880 ConstantIntegral::getAllOnesValue(Op->getType()),
881 Op->getType(), Name, InsertBefore);
882}
883
884BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
885 BasicBlock *InsertAtEnd) {
886 return new BinaryOperator(Instruction::Xor, Op,
887 ConstantIntegral::getAllOnesValue(Op->getType()),
888 Op->getType(), Name, InsertAtEnd);
889}
890
891
892// isConstantAllOnes - Helper function for several functions below
893static inline bool isConstantAllOnes(const Value *V) {
894 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
895}
896
897bool BinaryOperator::isNeg(const Value *V) {
898 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
899 if (Bop->getOpcode() == Instruction::Sub)
900 if (!V->getType()->isFloatingPoint())
901 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
902 else
903 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
904 return false;
905}
906
907bool BinaryOperator::isNot(const Value *V) {
908 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
909 return (Bop->getOpcode() == Instruction::Xor &&
910 (isConstantAllOnes(Bop->getOperand(1)) ||
911 isConstantAllOnes(Bop->getOperand(0))));
912 return false;
913}
914
Chris Lattner2c7d1772005-04-24 07:28:37 +0000915Value *BinaryOperator::getNegArgument(Value *BinOp) {
916 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
917 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000918}
919
Chris Lattner2c7d1772005-04-24 07:28:37 +0000920const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
921 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922}
923
Chris Lattner2c7d1772005-04-24 07:28:37 +0000924Value *BinaryOperator::getNotArgument(Value *BinOp) {
925 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
926 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
927 Value *Op0 = BO->getOperand(0);
928 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929 if (isConstantAllOnes(Op0)) return Op1;
930
931 assert(isConstantAllOnes(Op1));
932 return Op0;
933}
934
Chris Lattner2c7d1772005-04-24 07:28:37 +0000935const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
936 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000937}
938
939
940// swapOperands - Exchange the two operands to this instruction. This
941// instruction is safe to use on any binary instruction and does not
942// modify the semantics of the instruction. If the instruction is
943// order dependent (SetLT f.e.) the opcode is changed.
944//
945bool BinaryOperator::swapOperands() {
946 if (isCommutative())
947 ; // If the instruction is commutative, it is safe to swap the operands
948 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
949 /// FIXME: SetCC instructions shouldn't all have different opcodes.
950 setOpcode(SCI->getSwappedCondition());
951 else
952 return true; // Can't commute operands
953
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000954 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000955 return false;
956}
957
958
959//===----------------------------------------------------------------------===//
960// SetCondInst Class
961//===----------------------------------------------------------------------===//
962
Misha Brukmanb1c93172005-04-21 23:48:37 +0000963SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000964 const std::string &Name, Instruction *InsertBefore)
965 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
966
967 // Make sure it's a valid type... getInverseCondition will assert out if not.
968 assert(getInverseCondition(Opcode));
969}
970
Misha Brukmanb1c93172005-04-21 23:48:37 +0000971SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000972 const std::string &Name, BasicBlock *InsertAtEnd)
973 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
974
975 // Make sure it's a valid type... getInverseCondition will assert out if not.
976 assert(getInverseCondition(Opcode));
977}
978
979// getInverseCondition - Return the inverse of the current condition opcode.
980// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
981//
982Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
983 switch (Opcode) {
984 default:
985 assert(0 && "Unknown setcc opcode!");
986 case SetEQ: return SetNE;
987 case SetNE: return SetEQ;
988 case SetGT: return SetLE;
989 case SetLT: return SetGE;
990 case SetGE: return SetLT;
991 case SetLE: return SetGT;
992 }
993}
994
995// getSwappedCondition - Return the condition opcode that would be the result
996// of exchanging the two operands of the setcc instruction without changing
997// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
998//
999Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
1000 switch (Opcode) {
1001 default: assert(0 && "Unknown setcc instruction!");
1002 case SetEQ: case SetNE: return Opcode;
1003 case SetGT: return SetLT;
1004 case SetLT: return SetGT;
1005 case SetGE: return SetLE;
1006 case SetLE: return SetGE;
1007 }
1008}
1009
1010//===----------------------------------------------------------------------===//
1011// SwitchInst Implementation
1012//===----------------------------------------------------------------------===//
1013
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001014void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001015 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001016 ReservedSpace = 2+NumCases*2;
1017 NumOperands = 2;
1018 OperandList = new Use[ReservedSpace];
1019
1020 OperandList[0].init(Value, this);
1021 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001022}
1023
Misha Brukmanb1c93172005-04-21 23:48:37 +00001024SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001025 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
1026 SI.getNumOperands()) {
1027 Use *OL = OperandList, *InOL = SI.OperandList;
1028 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
1029 OL[i].init(InOL[i], this);
1030 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001031 }
1032}
1033
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001034SwitchInst::~SwitchInst() {
1035 delete [] OperandList;
1036}
1037
1038
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001039/// addCase - Add an entry to the switch instruction...
1040///
Chris Lattner47ac1872005-02-24 05:32:09 +00001041void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042 unsigned OpNo = NumOperands;
1043 if (OpNo+2 > ReservedSpace)
1044 resizeOperands(0); // Get more space!
1045 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001046 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001047 NumOperands = OpNo+2;
1048 OperandList[OpNo].init(OnVal, this);
1049 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050}
1051
1052/// removeCase - This method removes the specified successor from the switch
1053/// instruction. Note that this cannot be used to remove the default
1054/// destination (successor #0).
1055///
1056void SwitchInst::removeCase(unsigned idx) {
1057 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001058 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
1059
1060 unsigned NumOps = getNumOperands();
1061 Use *OL = OperandList;
1062
1063 // Move everything after this operand down.
1064 //
1065 // FIXME: we could just swap with the end of the list, then erase. However,
1066 // client might not expect this to happen. The code as it is thrashes the
1067 // use/def lists, which is kinda lame.
1068 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
1069 OL[i-2] = OL[i];
1070 OL[i-2+1] = OL[i+1];
1071 }
1072
1073 // Nuke the last value.
1074 OL[NumOps-2].set(0);
1075 OL[NumOps-2+1].set(0);
1076 NumOperands = NumOps-2;
1077}
1078
1079/// resizeOperands - resize operands - This adjusts the length of the operands
1080/// list according to the following behavior:
1081/// 1. If NumOps == 0, grow the operand list in response to a push_back style
1082/// of operation. This grows the number of ops by 1.5 times.
1083/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
1084/// 3. If NumOps == NumOperands, trim the reserved space.
1085///
1086void SwitchInst::resizeOperands(unsigned NumOps) {
1087 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001088 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001089 } else if (NumOps*2 > NumOperands) {
1090 // No resize needed.
1091 if (ReservedSpace >= NumOps) return;
1092 } else if (NumOps == NumOperands) {
1093 if (ReservedSpace == NumOps) return;
1094 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001095 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001096 }
1097
1098 ReservedSpace = NumOps;
1099 Use *NewOps = new Use[NumOps];
1100 Use *OldOps = OperandList;
1101 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1102 NewOps[i].init(OldOps[i], this);
1103 OldOps[i].set(0);
1104 }
1105 delete [] OldOps;
1106 OperandList = NewOps;
1107}
1108
1109
1110BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
1111 return getSuccessor(idx);
1112}
1113unsigned SwitchInst::getNumSuccessorsV() const {
1114 return getNumSuccessors();
1115}
1116void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1117 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118}
Chris Lattnerf22be932004-10-15 23:52:53 +00001119
1120
1121// Define these methods here so vtables don't get emitted into every translation
1122// unit that uses these classes.
1123
1124GetElementPtrInst *GetElementPtrInst::clone() const {
1125 return new GetElementPtrInst(*this);
1126}
1127
1128BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001129 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00001130}
1131
1132MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
1133AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001134FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
Chris Lattnerf22be932004-10-15 23:52:53 +00001135LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
1136StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
1137CastInst *CastInst::clone() const { return new CastInst(*this); }
1138CallInst *CallInst::clone() const { return new CallInst(*this); }
1139ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
1140SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
Chris Lattnerf22be932004-10-15 23:52:53 +00001141VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
1142PHINode *PHINode::clone() const { return new PHINode(*this); }
1143ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
1144BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
1145SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
1146InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
1147UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001148UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}