blob: 96a010b2f773cda6c3303d55bded2e4a48109546 [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)
Chris Lattner6e709c12005-08-05 15:37:31 +0000141 if (getIncomingValue(0) != this) // not X = phi X
142 return getIncomingValue(0);
143 else
144 return UndefValue::get(getType()); // Self cycle is dead.
145
Nate Begemanb3923212005-08-04 23:24:19 +0000146 // Otherwise if all of the incoming values are the same for the PHI, replace
147 // the PHI node with the incoming value.
148 //
149 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000150 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000151 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000152 if (isa<UndefValue>(getIncomingValue(i)))
153 HasUndefInput = true;
154 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000155 if (InVal && getIncomingValue(i) != InVal)
156 return 0; // Not the same, bail out.
157 else
158 InVal = getIncomingValue(i);
159
160 // The only case that could cause InVal to be null is if we have a PHI node
161 // that only has entries for itself. In this case, there is no entry into the
162 // loop, so kill the PHI.
163 //
164 if (InVal == 0) InVal = UndefValue::get(getType());
165
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000166 // If we have a PHI node like phi(X, undef, X), where X is defined by some
167 // instruction, we cannot always return X as the result of the PHI node. Only
168 // do this if X is not an instruction (thus it must dominate the PHI block),
169 // or if the client is prepared to deal with this possibility.
170 if (HasUndefInput && !AllowNonDominatingInstruction)
171 if (Instruction *IV = dyn_cast<Instruction>(InVal))
172 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000173 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
174 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 return 0; // Cannot guarantee that InVal dominates this PHINode.
176
Nate Begemanb3923212005-08-04 23:24:19 +0000177 // All of the incoming values are the same, return the value now.
178 return InVal;
179}
180
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000181
182//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000183// CallInst Implementation
184//===----------------------------------------------------------------------===//
185
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000186CallInst::~CallInst() {
187 delete [] OperandList;
188}
189
190void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
191 NumOperands = Params.size()+1;
192 Use *OL = OperandList = new Use[Params.size()+1];
193 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000194
Misha Brukmanb1c93172005-04-21 23:48:37 +0000195 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000196 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
197
Misha Brukmanb1c93172005-04-21 23:48:37 +0000198 assert((Params.size() == FTy->getNumParams() ||
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
200 "Calling a function with bad signature");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000201 for (unsigned i = 0, e = Params.size(); i != e; ++i)
202 OL[i+1].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203}
204
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000205void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
206 NumOperands = 3;
207 Use *OL = OperandList = new Use[3];
208 OL[0].init(Func, this);
209 OL[1].init(Actual1, this);
210 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000211
212 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000213 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
214
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 assert((FTy->getNumParams() == 2 ||
216 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217 "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000218}
219
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000220void CallInst::init(Value *Func, Value *Actual) {
221 NumOperands = 2;
222 Use *OL = OperandList = new Use[2];
223 OL[0].init(Func, this);
224 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000225
226 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
228
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000229 assert((FTy->getNumParams() == 1 ||
230 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000231 "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232}
233
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000234void CallInst::init(Value *Func) {
235 NumOperands = 1;
236 Use *OL = OperandList = new Use[1];
237 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000238
239 const FunctionType *MTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000240 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
241
242 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
243}
244
Misha Brukmanb1c93172005-04-21 23:48:37 +0000245CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
246 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000247 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
248 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000249 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250 init(Func, Params);
251}
252
Misha Brukmanb1c93172005-04-21 23:48:37 +0000253CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
254 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
256 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 init(Func, Params);
259}
260
261CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
262 const std::string &Name, Instruction *InsertBefore)
263 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
264 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000265 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266 init(Func, Actual1, Actual2);
267}
268
269CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
270 const std::string &Name, BasicBlock *InsertAtEnd)
271 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
272 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000273 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274 init(Func, Actual1, Actual2);
275}
276
277CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
278 Instruction *InsertBefore)
279 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
280 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282 init(Func, Actual);
283}
284
285CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
286 BasicBlock *InsertAtEnd)
287 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
288 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290 init(Func, Actual);
291}
292
293CallInst::CallInst(Value *Func, const std::string &Name,
294 Instruction *InsertBefore)
295 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
296 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000297 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298 init(Func);
299}
300
301CallInst::CallInst(Value *Func, const std::string &Name,
302 BasicBlock *InsertAtEnd)
303 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
304 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000305 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 init(Func);
307}
308
Misha Brukmanb1c93172005-04-21 23:48:37 +0000309CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000310 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
311 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000312 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000313 Use *OL = OperandList;
314 Use *InOL = CI.OperandList;
315 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
316 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000317}
318
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000319
320//===----------------------------------------------------------------------===//
321// InvokeInst Implementation
322//===----------------------------------------------------------------------===//
323
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000324InvokeInst::~InvokeInst() {
325 delete [] OperandList;
326}
327
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000329 const std::vector<Value*> &Params) {
330 NumOperands = 3+Params.size();
331 Use *OL = OperandList = new Use[3+Params.size()];
332 OL[0].init(Fn, this);
333 OL[1].init(IfNormal, this);
334 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000335 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000337
338 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000339 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000341
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000342 for (unsigned i = 0, e = Params.size(); i != e; i++)
343 OL[i+3].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344}
345
346InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
347 BasicBlock *IfException,
348 const std::vector<Value*> &Params,
349 const std::string &Name, Instruction *InsertBefore)
350 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
351 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000352 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353 init(Fn, IfNormal, IfException, Params);
354}
355
356InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
357 BasicBlock *IfException,
358 const std::vector<Value*> &Params,
359 const std::string &Name, BasicBlock *InsertAtEnd)
360 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
361 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000362 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363 init(Fn, IfNormal, IfException, Params);
364}
365
Misha Brukmanb1c93172005-04-21 23:48:37 +0000366InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000367 : TerminatorInst(II.getType(), Instruction::Invoke,
368 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000369 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000370 Use *OL = OperandList, *InOL = II.OperandList;
371 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
372 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
374
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000375BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
376 return getSuccessor(idx);
377}
378unsigned InvokeInst::getNumSuccessorsV() const {
379 return getNumSuccessors();
380}
381void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
382 return setSuccessor(idx, B);
383}
384
385
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000386//===----------------------------------------------------------------------===//
387// ReturnInst Implementation
388//===----------------------------------------------------------------------===//
389
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000390void ReturnInst::init(Value *retVal) {
391 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000392 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000393 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000394 NumOperands = 1;
395 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000396 }
397}
398
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399unsigned ReturnInst::getNumSuccessorsV() const {
400 return getNumSuccessors();
401}
402
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000403// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
404// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000405void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406 assert(0 && "ReturnInst has no successors!");
407}
408
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000409BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
410 assert(0 && "ReturnInst has no successors!");
411 abort();
412 return 0;
413}
414
415
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000416//===----------------------------------------------------------------------===//
417// UnwindInst Implementation
418//===----------------------------------------------------------------------===//
419
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000420unsigned UnwindInst::getNumSuccessorsV() const {
421 return getNumSuccessors();
422}
423
424void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000425 assert(0 && "UnwindInst has no successors!");
426}
427
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000428BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
429 assert(0 && "UnwindInst has no successors!");
430 abort();
431 return 0;
432}
433
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000434//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000435// UnreachableInst Implementation
436//===----------------------------------------------------------------------===//
437
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000438unsigned UnreachableInst::getNumSuccessorsV() const {
439 return getNumSuccessors();
440}
441
442void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
443 assert(0 && "UnwindInst has no successors!");
444}
445
446BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
447 assert(0 && "UnwindInst has no successors!");
448 abort();
449 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000450}
451
452//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000453// BranchInst Implementation
454//===----------------------------------------------------------------------===//
455
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000456void BranchInst::AssertOK() {
457 if (isConditional())
Misha Brukmanb1c93172005-04-21 23:48:37 +0000458 assert(getCondition()->getType() == Type::BoolTy &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000459 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000460}
461
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462BranchInst::BranchInst(const BranchInst &BI) :
463 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
464 OperandList[0].init(BI.getOperand(0), this);
465 if (BI.getNumOperands() != 1) {
466 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
467 OperandList[1].init(BI.getOperand(1), this);
468 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000469 }
470}
471
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000472BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
473 return getSuccessor(idx);
474}
475unsigned BranchInst::getNumSuccessorsV() const {
476 return getNumSuccessors();
477}
478void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
479 setSuccessor(idx, B);
480}
481
482
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000483//===----------------------------------------------------------------------===//
484// AllocationInst Implementation
485//===----------------------------------------------------------------------===//
486
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000487static Value *getAISize(Value *Amt) {
488 if (!Amt)
489 Amt = ConstantUInt::get(Type::UIntTy, 1);
490 else
491 assert(Amt->getType() == Type::UIntTy &&
492 "Malloc/Allocation array size != UIntTy!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000493 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000494}
495
Misha Brukmanb1c93172005-04-21 23:48:37 +0000496AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000497 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000499 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000500 Name, InsertBefore), Alignment(Align) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000501 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000502}
503
Misha Brukmanb1c93172005-04-21 23:48:37 +0000504AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000505 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000506 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000507 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000508 Name, InsertAtEnd), Alignment(Align) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000509 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000510}
511
512bool AllocationInst::isArrayAllocation() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000513 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
514 return CUI->getValue() != 1;
515 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000516}
517
518const Type *AllocationInst::getAllocatedType() const {
519 return getType()->getElementType();
520}
521
522AllocaInst::AllocaInst(const AllocaInst &AI)
523 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000524 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525}
526
527MallocInst::MallocInst(const MallocInst &MI)
528 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000529 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000530}
531
532//===----------------------------------------------------------------------===//
533// FreeInst Implementation
534//===----------------------------------------------------------------------===//
535
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000536void FreeInst::AssertOK() {
537 assert(isa<PointerType>(getOperand(0)->getType()) &&
538 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000539}
540
541FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
543 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000544}
545
546FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000547 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
548 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000549}
550
551
552//===----------------------------------------------------------------------===//
553// LoadInst Implementation
554//===----------------------------------------------------------------------===//
555
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000557 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000558 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000559}
560
561LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000562 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000563 Load, Ptr, Name, InsertBef) {
564 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000565 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566}
567
568LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000570 Load, Ptr, Name, InsertAE) {
571 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000573}
574
575LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
576 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000577 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000578 Load, Ptr, Name, InsertBef) {
579 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000580 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581}
582
583LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
584 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000585 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000586 Load, Ptr, Name, InsertAE) {
587 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000588 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589}
590
591
592//===----------------------------------------------------------------------===//
593// StoreInst Implementation
594//===----------------------------------------------------------------------===//
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596void StoreInst::AssertOK() {
597 assert(isa<PointerType>(getOperand(1)->getType()) &&
598 "Ptr must have pointer type!");
599 assert(getOperand(0)->getType() ==
600 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000601 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000603
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000604
605StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000606 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000607 Ops[0].init(val, this);
608 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000609 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000610 AssertOK();
611}
612
613StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000614 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000615 Ops[0].init(val, this);
616 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000617 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 AssertOK();
619}
620
Misha Brukmanb1c93172005-04-21 23:48:37 +0000621StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000622 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000623 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 Ops[0].init(val, this);
625 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000626 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627 AssertOK();
628}
629
Misha Brukmanb1c93172005-04-21 23:48:37 +0000630StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000631 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000632 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633 Ops[0].init(val, this);
634 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000635 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000637}
638
639//===----------------------------------------------------------------------===//
640// GetElementPtrInst Implementation
641//===----------------------------------------------------------------------===//
642
643// checkType - Simple wrapper function to give a better assertion failure
644// message on bad indexes for a gep instruction.
645//
646static inline const Type *checkType(const Type *Ty) {
647 assert(Ty && "Invalid indices for type!");
648 return Ty;
649}
650
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
652 NumOperands = 1+Idx.size();
653 Use *OL = OperandList = new Use[NumOperands];
654 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000655
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
657 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000658}
659
660void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000661 NumOperands = 3;
662 Use *OL = OperandList = new Use[3];
663 OL[0].init(Ptr, this);
664 OL[1].init(Idx0, this);
665 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000666}
667
Chris Lattner82981202005-05-03 05:43:30 +0000668void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
669 NumOperands = 2;
670 Use *OL = OperandList = new Use[2];
671 OL[0].init(Ptr, this);
672 OL[1].init(Idx, this);
673}
674
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000675GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000676 const std::string &Name, Instruction *InBe)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000677 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
678 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000680 init(Ptr, Idx);
681}
682
683GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000684 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000685 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
686 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688 init(Ptr, Idx);
689}
690
Chris Lattner82981202005-05-03 05:43:30 +0000691GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
692 const std::string &Name, Instruction *InBe)
693 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
694 GetElementPtr, 0, 0, Name, InBe) {
695 init(Ptr, Idx);
696}
697
698GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
699 const std::string &Name, BasicBlock *IAE)
700 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
701 GetElementPtr, 0, 0, Name, IAE) {
702 init(Ptr, Idx);
703}
704
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000705GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
706 const std::string &Name, Instruction *InBe)
707 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
708 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000709 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000710 init(Ptr, Idx0, Idx1);
711}
712
713GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000714 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000715 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
716 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718 init(Ptr, Idx0, Idx1);
719}
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721GetElementPtrInst::~GetElementPtrInst() {
722 delete[] OperandList;
723}
724
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725// getIndexedType - Returns the type of the element that would be loaded with
726// a load instruction with the specified parameters.
727//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000728// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000729// pointer type.
730//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000731const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732 const std::vector<Value*> &Idx,
733 bool AllowCompositeLeaf) {
734 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
735
736 // Handle the special case of the empty set index set...
737 if (Idx.empty())
738 if (AllowCompositeLeaf ||
739 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
740 return cast<PointerType>(Ptr)->getElementType();
741 else
742 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000743
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000744 unsigned CurIdx = 0;
745 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
746 if (Idx.size() == CurIdx) {
747 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
748 return 0; // Can't load a whole structure or array!?!?
749 }
750
751 Value *Index = Idx[CurIdx++];
752 if (isa<PointerType>(CT) && CurIdx != 1)
753 return 0; // Can only index into pointer types at the first index!
754 if (!CT->indexValid(Index)) return 0;
755 Ptr = CT->getTypeAtIndex(Index);
756
757 // If the new type forwards to another type, then it is in the middle
758 // of being refined to another type (and hence, may have dropped all
759 // references to what it was using before). So, use the new forwarded
760 // type.
761 if (const Type * Ty = Ptr->getForwardedType()) {
762 Ptr = Ty;
763 }
764 }
765 return CurIdx == Idx.size() ? Ptr : 0;
766}
767
Misha Brukmanb1c93172005-04-21 23:48:37 +0000768const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000769 Value *Idx0, Value *Idx1,
770 bool AllowCompositeLeaf) {
771 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
772 if (!PTy) return 0; // Type isn't a pointer type!
773
774 // Check the pointer index.
775 if (!PTy->indexValid(Idx0)) return 0;
776
777 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
778 if (!CT || !CT->indexValid(Idx1)) return 0;
779
780 const Type *ElTy = CT->getTypeAtIndex(Idx1);
781 if (AllowCompositeLeaf || ElTy->isFirstClassType())
782 return ElTy;
783 return 0;
784}
785
Chris Lattner82981202005-05-03 05:43:30 +0000786const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
787 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
788 if (!PTy) return 0; // Type isn't a pointer type!
789
790 // Check the pointer index.
791 if (!PTy->indexValid(Idx)) return 0;
792
Chris Lattnerc2233332005-05-03 16:44:45 +0000793 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000794}
795
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000796//===----------------------------------------------------------------------===//
797// BinaryOperator Class
798//===----------------------------------------------------------------------===//
799
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000800void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000801{
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 Value *LHS = getOperand(0), *RHS = getOperand(1);
803 assert(LHS->getType() == RHS->getType() &&
804 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000805#ifndef NDEBUG
806 switch (iType) {
807 case Add: case Sub:
808 case Mul: case Div:
809 case Rem:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000811 "Arithmetic operation should return same type as operands!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000812 assert((getType()->isInteger() ||
813 getType()->isFloatingPoint() ||
814 isa<PackedType>(getType()) ) &&
Brian Gaeke02209042004-08-20 06:00:58 +0000815 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000816 break;
817 case And: case Or:
818 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820 "Logical operation should return same type as operands!");
821 assert(getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +0000822 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000823 break;
824 case SetLT: case SetGT: case SetLE:
825 case SetGE: case SetEQ: case SetNE:
826 assert(getType() == Type::BoolTy && "Setcc must return bool!");
827 default:
828 break;
829 }
830#endif
831}
832
833BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000834 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835 Instruction *InsertBefore) {
836 assert(S1->getType() == S2->getType() &&
837 "Cannot create binary operator with two operands of differing type!");
838 switch (Op) {
839 // Binary comparison operators...
840 case SetLT: case SetGT: case SetLE:
841 case SetGE: case SetEQ: case SetNE:
842 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
843
844 default:
845 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
846 }
847}
848
849BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000850 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000851 BasicBlock *InsertAtEnd) {
852 BinaryOperator *Res = create(Op, S1, S2, Name);
853 InsertAtEnd->getInstList().push_back(Res);
854 return Res;
855}
856
857BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
858 Instruction *InsertBefore) {
859 if (!Op->getType()->isFloatingPoint())
860 return new BinaryOperator(Instruction::Sub,
861 Constant::getNullValue(Op->getType()), Op,
862 Op->getType(), Name, InsertBefore);
863 else
864 return new BinaryOperator(Instruction::Sub,
865 ConstantFP::get(Op->getType(), -0.0), Op,
866 Op->getType(), Name, InsertBefore);
867}
868
869BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
870 BasicBlock *InsertAtEnd) {
871 if (!Op->getType()->isFloatingPoint())
872 return new BinaryOperator(Instruction::Sub,
873 Constant::getNullValue(Op->getType()), Op,
874 Op->getType(), Name, InsertAtEnd);
875 else
876 return new BinaryOperator(Instruction::Sub,
877 ConstantFP::get(Op->getType(), -0.0), Op,
878 Op->getType(), Name, InsertAtEnd);
879}
880
881BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
882 Instruction *InsertBefore) {
883 return new BinaryOperator(Instruction::Xor, Op,
884 ConstantIntegral::getAllOnesValue(Op->getType()),
885 Op->getType(), Name, InsertBefore);
886}
887
888BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
889 BasicBlock *InsertAtEnd) {
890 return new BinaryOperator(Instruction::Xor, Op,
891 ConstantIntegral::getAllOnesValue(Op->getType()),
892 Op->getType(), Name, InsertAtEnd);
893}
894
895
896// isConstantAllOnes - Helper function for several functions below
897static inline bool isConstantAllOnes(const Value *V) {
898 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
899}
900
901bool BinaryOperator::isNeg(const Value *V) {
902 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
903 if (Bop->getOpcode() == Instruction::Sub)
904 if (!V->getType()->isFloatingPoint())
905 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
906 else
907 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
908 return false;
909}
910
911bool BinaryOperator::isNot(const Value *V) {
912 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
913 return (Bop->getOpcode() == Instruction::Xor &&
914 (isConstantAllOnes(Bop->getOperand(1)) ||
915 isConstantAllOnes(Bop->getOperand(0))));
916 return false;
917}
918
Chris Lattner2c7d1772005-04-24 07:28:37 +0000919Value *BinaryOperator::getNegArgument(Value *BinOp) {
920 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
921 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922}
923
Chris Lattner2c7d1772005-04-24 07:28:37 +0000924const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
925 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926}
927
Chris Lattner2c7d1772005-04-24 07:28:37 +0000928Value *BinaryOperator::getNotArgument(Value *BinOp) {
929 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
930 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
931 Value *Op0 = BO->getOperand(0);
932 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000933 if (isConstantAllOnes(Op0)) return Op1;
934
935 assert(isConstantAllOnes(Op1));
936 return Op0;
937}
938
Chris Lattner2c7d1772005-04-24 07:28:37 +0000939const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
940 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000941}
942
943
944// swapOperands - Exchange the two operands to this instruction. This
945// instruction is safe to use on any binary instruction and does not
946// modify the semantics of the instruction. If the instruction is
947// order dependent (SetLT f.e.) the opcode is changed.
948//
949bool BinaryOperator::swapOperands() {
950 if (isCommutative())
951 ; // If the instruction is commutative, it is safe to swap the operands
952 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
953 /// FIXME: SetCC instructions shouldn't all have different opcodes.
954 setOpcode(SCI->getSwappedCondition());
955 else
956 return true; // Can't commute operands
957
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000959 return false;
960}
961
962
963//===----------------------------------------------------------------------===//
964// SetCondInst Class
965//===----------------------------------------------------------------------===//
966
Misha Brukmanb1c93172005-04-21 23:48:37 +0000967SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968 const std::string &Name, Instruction *InsertBefore)
969 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
970
971 // Make sure it's a valid type... getInverseCondition will assert out if not.
972 assert(getInverseCondition(Opcode));
973}
974
Misha Brukmanb1c93172005-04-21 23:48:37 +0000975SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000976 const std::string &Name, BasicBlock *InsertAtEnd)
977 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
978
979 // Make sure it's a valid type... getInverseCondition will assert out if not.
980 assert(getInverseCondition(Opcode));
981}
982
983// getInverseCondition - Return the inverse of the current condition opcode.
984// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
985//
986Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
987 switch (Opcode) {
988 default:
989 assert(0 && "Unknown setcc opcode!");
990 case SetEQ: return SetNE;
991 case SetNE: return SetEQ;
992 case SetGT: return SetLE;
993 case SetLT: return SetGE;
994 case SetGE: return SetLT;
995 case SetLE: return SetGT;
996 }
997}
998
999// getSwappedCondition - Return the condition opcode that would be the result
1000// of exchanging the two operands of the setcc instruction without changing
1001// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
1002//
1003Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
1004 switch (Opcode) {
1005 default: assert(0 && "Unknown setcc instruction!");
1006 case SetEQ: case SetNE: return Opcode;
1007 case SetGT: return SetLT;
1008 case SetLT: return SetGT;
1009 case SetGE: return SetLE;
1010 case SetLE: return SetGE;
1011 }
1012}
1013
1014//===----------------------------------------------------------------------===//
1015// SwitchInst Implementation
1016//===----------------------------------------------------------------------===//
1017
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001018void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001019 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001020 ReservedSpace = 2+NumCases*2;
1021 NumOperands = 2;
1022 OperandList = new Use[ReservedSpace];
1023
1024 OperandList[0].init(Value, this);
1025 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026}
1027
Misha Brukmanb1c93172005-04-21 23:48:37 +00001028SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
1030 SI.getNumOperands()) {
1031 Use *OL = OperandList, *InOL = SI.OperandList;
1032 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
1033 OL[i].init(InOL[i], this);
1034 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001035 }
1036}
1037
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038SwitchInst::~SwitchInst() {
1039 delete [] OperandList;
1040}
1041
1042
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001043/// addCase - Add an entry to the switch instruction...
1044///
Chris Lattner47ac1872005-02-24 05:32:09 +00001045void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001046 unsigned OpNo = NumOperands;
1047 if (OpNo+2 > ReservedSpace)
1048 resizeOperands(0); // Get more space!
1049 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001050 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001051 NumOperands = OpNo+2;
1052 OperandList[OpNo].init(OnVal, this);
1053 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054}
1055
1056/// removeCase - This method removes the specified successor from the switch
1057/// instruction. Note that this cannot be used to remove the default
1058/// destination (successor #0).
1059///
1060void SwitchInst::removeCase(unsigned idx) {
1061 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001062 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
1063
1064 unsigned NumOps = getNumOperands();
1065 Use *OL = OperandList;
1066
1067 // Move everything after this operand down.
1068 //
1069 // FIXME: we could just swap with the end of the list, then erase. However,
1070 // client might not expect this to happen. The code as it is thrashes the
1071 // use/def lists, which is kinda lame.
1072 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
1073 OL[i-2] = OL[i];
1074 OL[i-2+1] = OL[i+1];
1075 }
1076
1077 // Nuke the last value.
1078 OL[NumOps-2].set(0);
1079 OL[NumOps-2+1].set(0);
1080 NumOperands = NumOps-2;
1081}
1082
1083/// resizeOperands - resize operands - This adjusts the length of the operands
1084/// list according to the following behavior:
1085/// 1. If NumOps == 0, grow the operand list in response to a push_back style
1086/// of operation. This grows the number of ops by 1.5 times.
1087/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
1088/// 3. If NumOps == NumOperands, trim the reserved space.
1089///
1090void SwitchInst::resizeOperands(unsigned NumOps) {
1091 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001092 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001093 } else if (NumOps*2 > NumOperands) {
1094 // No resize needed.
1095 if (ReservedSpace >= NumOps) return;
1096 } else if (NumOps == NumOperands) {
1097 if (ReservedSpace == NumOps) return;
1098 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001099 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001100 }
1101
1102 ReservedSpace = NumOps;
1103 Use *NewOps = new Use[NumOps];
1104 Use *OldOps = OperandList;
1105 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1106 NewOps[i].init(OldOps[i], this);
1107 OldOps[i].set(0);
1108 }
1109 delete [] OldOps;
1110 OperandList = NewOps;
1111}
1112
1113
1114BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
1115 return getSuccessor(idx);
1116}
1117unsigned SwitchInst::getNumSuccessorsV() const {
1118 return getNumSuccessors();
1119}
1120void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1121 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001122}
Chris Lattnerf22be932004-10-15 23:52:53 +00001123
1124
1125// Define these methods here so vtables don't get emitted into every translation
1126// unit that uses these classes.
1127
1128GetElementPtrInst *GetElementPtrInst::clone() const {
1129 return new GetElementPtrInst(*this);
1130}
1131
1132BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001133 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00001134}
1135
1136MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
1137AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001138FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
Chris Lattnerf22be932004-10-15 23:52:53 +00001139LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
1140StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
1141CastInst *CastInst::clone() const { return new CastInst(*this); }
1142CallInst *CallInst::clone() const { return new CallInst(*this); }
1143ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
1144SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
Chris Lattnerf22be932004-10-15 23:52:53 +00001145VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
1146PHINode *PHINode::clone() const { return new PHINode(*this); }
1147ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
1148BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
1149SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
1150InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
1151UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001152UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}