blob: 8404e0bad8eafbeb4a167681e02847f3c3455751 [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
23//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000024// TerminatorInst Class
25//===----------------------------------------------------------------------===//
26
27TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000028 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000029 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
30}
31
32TerminatorInst::TerminatorInst(Instruction::TermOps iType,
33 Use *Ops, unsigned NumOps, BasicBlock *IAE)
34 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
35}
36
37
38
39//===----------------------------------------------------------------------===//
40// PHINode Class
41//===----------------------------------------------------------------------===//
42
43PHINode::PHINode(const PHINode &PN)
44 : Instruction(PN.getType(), Instruction::PHI,
45 new Use[PN.getNumOperands()], PN.getNumOperands()),
46 ReservedSpace(PN.getNumOperands()) {
47 Use *OL = OperandList;
48 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
49 OL[i].init(PN.getOperand(i), this);
50 OL[i+1].init(PN.getOperand(i+1), this);
51 }
52}
53
54PHINode::~PHINode() {
55 delete [] OperandList;
56}
57
58// removeIncomingValue - Remove an incoming value. This is useful if a
59// predecessor basic block is deleted.
60Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
61 unsigned NumOps = getNumOperands();
62 Use *OL = OperandList;
63 assert(Idx*2 < NumOps && "BB not in PHI node!");
64 Value *Removed = OL[Idx*2];
65
66 // Move everything after this operand down.
67 //
68 // FIXME: we could just swap with the end of the list, then erase. However,
69 // client might not expect this to happen. The code as it is thrashes the
70 // use/def lists, which is kinda lame.
71 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
72 OL[i-2] = OL[i];
73 OL[i-2+1] = OL[i+1];
74 }
75
76 // Nuke the last value.
77 OL[NumOps-2].set(0);
78 OL[NumOps-2+1].set(0);
79 NumOperands = NumOps-2;
80
81 // If the PHI node is dead, because it has zero entries, nuke it now.
82 if (NumOps == 2 && DeletePHIIfEmpty) {
83 // If anyone is using this PHI, make them use a dummy value instead...
84 replaceAllUsesWith(UndefValue::get(getType()));
85 eraseFromParent();
86 }
87 return Removed;
88}
89
90/// resizeOperands - resize operands - This adjusts the length of the operands
91/// list according to the following behavior:
92/// 1. If NumOps == 0, grow the operand list in response to a push_back style
93/// of operation. This grows the number of ops by 1.5 times.
94/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
95/// 3. If NumOps == NumOperands, trim the reserved space.
96///
97void PHINode::resizeOperands(unsigned NumOps) {
98 if (NumOps == 0) {
99 NumOps = (getNumOperands())*3/2;
100 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
101 } else if (NumOps*2 > NumOperands) {
102 // No resize needed.
103 if (ReservedSpace >= NumOps) return;
104 } else if (NumOps == NumOperands) {
105 if (ReservedSpace == NumOps) return;
106 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000107 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000108 }
109
110 ReservedSpace = NumOps;
111 Use *NewOps = new Use[NumOps];
112 Use *OldOps = OperandList;
113 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
114 NewOps[i].init(OldOps[i], this);
115 OldOps[i].set(0);
116 }
117 delete [] OldOps;
118 OperandList = NewOps;
119}
120
121
122//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000123// CallInst Implementation
124//===----------------------------------------------------------------------===//
125
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000126CallInst::~CallInst() {
127 delete [] OperandList;
128}
129
130void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
131 NumOperands = Params.size()+1;
132 Use *OL = OperandList = new Use[Params.size()+1];
133 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000134
Misha Brukmanb1c93172005-04-21 23:48:37 +0000135 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000136 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
137
Misha Brukmanb1c93172005-04-21 23:48:37 +0000138 assert((Params.size() == FTy->getNumParams() ||
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000139 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
140 "Calling a function with bad signature");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000141 for (unsigned i = 0, e = Params.size(); i != e; ++i)
142 OL[i+1].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000143}
144
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
146 NumOperands = 3;
147 Use *OL = OperandList = new Use[3];
148 OL[0].init(Func, this);
149 OL[1].init(Actual1, this);
150 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000151
152 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000153 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
154
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 assert((FTy->getNumParams() == 2 ||
156 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000157 "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000158}
159
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000160void CallInst::init(Value *Func, Value *Actual) {
161 NumOperands = 2;
162 Use *OL = OperandList = new Use[2];
163 OL[0].init(Func, this);
164 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000165
166 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000167 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
168
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000169 assert((FTy->getNumParams() == 1 ||
170 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000171 "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000172}
173
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000174void CallInst::init(Value *Func) {
175 NumOperands = 1;
176 Use *OL = OperandList = new Use[1];
177 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000178
179 const FunctionType *MTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000180 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
181
182 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
183}
184
Misha Brukmanb1c93172005-04-21 23:48:37 +0000185CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
186 const std::string &Name, Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000187 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
188 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000189 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000190 init(Func, Params);
191}
192
Misha Brukmanb1c93172005-04-21 23:48:37 +0000193CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
194 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000195 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
196 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000197 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000198 init(Func, Params);
199}
200
201CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
202 const std::string &Name, Instruction *InsertBefore)
203 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
204 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000205 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000206 init(Func, Actual1, Actual2);
207}
208
209CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
210 const std::string &Name, BasicBlock *InsertAtEnd)
211 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
212 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000213 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000214 init(Func, Actual1, Actual2);
215}
216
217CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
218 Instruction *InsertBefore)
219 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
220 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000221 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000222 init(Func, Actual);
223}
224
225CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
226 BasicBlock *InsertAtEnd)
227 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
228 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000229 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000230 init(Func, Actual);
231}
232
233CallInst::CallInst(Value *Func, const std::string &Name,
234 Instruction *InsertBefore)
235 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
236 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000237 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238 init(Func);
239}
240
241CallInst::CallInst(Value *Func, const std::string &Name,
242 BasicBlock *InsertAtEnd)
243 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
244 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000245 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 init(Func);
247}
248
Misha Brukmanb1c93172005-04-21 23:48:37 +0000249CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000250 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
251 CI.getNumOperands()) {
252 Use *OL = OperandList;
253 Use *InOL = CI.OperandList;
254 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
255 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256}
257
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258
259//===----------------------------------------------------------------------===//
260// InvokeInst Implementation
261//===----------------------------------------------------------------------===//
262
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263InvokeInst::~InvokeInst() {
264 delete [] OperandList;
265}
266
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000268 const std::vector<Value*> &Params) {
269 NumOperands = 3+Params.size();
270 Use *OL = OperandList = new Use[3+Params.size()];
271 OL[0].init(Fn, this);
272 OL[1].init(IfNormal, this);
273 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000274 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000275 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000276
277 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000278 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281 for (unsigned i = 0, e = Params.size(); i != e; i++)
282 OL[i+3].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283}
284
285InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
286 BasicBlock *IfException,
287 const std::vector<Value*> &Params,
288 const std::string &Name, Instruction *InsertBefore)
289 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
290 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000292 init(Fn, IfNormal, IfException, Params);
293}
294
295InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
296 BasicBlock *IfException,
297 const std::vector<Value*> &Params,
298 const std::string &Name, BasicBlock *InsertAtEnd)
299 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
300 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000301 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 init(Fn, IfNormal, IfException, Params);
303}
304
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306 : TerminatorInst(II.getType(), Instruction::Invoke,
307 new Use[II.getNumOperands()], II.getNumOperands()) {
308 Use *OL = OperandList, *InOL = II.OperandList;
309 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
310 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000313BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
314 return getSuccessor(idx);
315}
316unsigned InvokeInst::getNumSuccessorsV() const {
317 return getNumSuccessors();
318}
319void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
320 return setSuccessor(idx, B);
321}
322
323
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324//===----------------------------------------------------------------------===//
325// ReturnInst Implementation
326//===----------------------------------------------------------------------===//
327
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000328void ReturnInst::init(Value *retVal) {
329 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000330 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000331 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000332 NumOperands = 1;
333 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000334 }
335}
336
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000337unsigned ReturnInst::getNumSuccessorsV() const {
338 return getNumSuccessors();
339}
340
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
342// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000343void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 assert(0 && "ReturnInst has no successors!");
345}
346
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000347BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
348 assert(0 && "ReturnInst has no successors!");
349 abort();
350 return 0;
351}
352
353
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354//===----------------------------------------------------------------------===//
355// UnwindInst Implementation
356//===----------------------------------------------------------------------===//
357
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000358unsigned UnwindInst::getNumSuccessorsV() const {
359 return getNumSuccessors();
360}
361
362void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000363 assert(0 && "UnwindInst has no successors!");
364}
365
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000366BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
367 assert(0 && "UnwindInst has no successors!");
368 abort();
369 return 0;
370}
371
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000373// UnreachableInst Implementation
374//===----------------------------------------------------------------------===//
375
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000376unsigned UnreachableInst::getNumSuccessorsV() const {
377 return getNumSuccessors();
378}
379
380void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
381 assert(0 && "UnwindInst has no successors!");
382}
383
384BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
385 assert(0 && "UnwindInst has no successors!");
386 abort();
387 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000388}
389
390//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000391// BranchInst Implementation
392//===----------------------------------------------------------------------===//
393
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000394void BranchInst::AssertOK() {
395 if (isConditional())
Misha Brukmanb1c93172005-04-21 23:48:37 +0000396 assert(getCondition()->getType() == Type::BoolTy &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000397 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000398}
399
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000400BranchInst::BranchInst(const BranchInst &BI) :
401 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
402 OperandList[0].init(BI.getOperand(0), this);
403 if (BI.getNumOperands() != 1) {
404 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
405 OperandList[1].init(BI.getOperand(1), this);
406 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000407 }
408}
409
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000410BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
411 return getSuccessor(idx);
412}
413unsigned BranchInst::getNumSuccessorsV() const {
414 return getNumSuccessors();
415}
416void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
417 setSuccessor(idx, B);
418}
419
420
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000421//===----------------------------------------------------------------------===//
422// AllocationInst Implementation
423//===----------------------------------------------------------------------===//
424
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000425static Value *getAISize(Value *Amt) {
426 if (!Amt)
427 Amt = ConstantUInt::get(Type::UIntTy, 1);
428 else
429 assert(Amt->getType() == Type::UIntTy &&
430 "Malloc/Allocation array size != UIntTy!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000431 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000432}
433
Misha Brukmanb1c93172005-04-21 23:48:37 +0000434AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000435 const std::string &Name,
436 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000437 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
438 Name, InsertBefore) {
439 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000440}
441
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000443 const std::string &Name,
444 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000445 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
446 Name, InsertAtEnd) {
447 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000448}
449
450bool AllocationInst::isArrayAllocation() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000451 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
452 return CUI->getValue() != 1;
453 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000454}
455
456const Type *AllocationInst::getAllocatedType() const {
457 return getType()->getElementType();
458}
459
460AllocaInst::AllocaInst(const AllocaInst &AI)
461 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
462 Instruction::Alloca) {
463}
464
465MallocInst::MallocInst(const MallocInst &MI)
466 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
467 Instruction::Malloc) {
468}
469
470//===----------------------------------------------------------------------===//
471// FreeInst Implementation
472//===----------------------------------------------------------------------===//
473
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000474void FreeInst::AssertOK() {
475 assert(isa<PointerType>(getOperand(0)->getType()) &&
476 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000477}
478
479FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000480 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
481 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000482}
483
484FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000485 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
486 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000487}
488
489
490//===----------------------------------------------------------------------===//
491// LoadInst Implementation
492//===----------------------------------------------------------------------===//
493
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000494void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000495 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000497}
498
499LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000500 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000501 Load, Ptr, Name, InsertBef) {
502 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000503 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000504}
505
506LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000507 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000508 Load, Ptr, Name, InsertAE) {
509 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000510 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000511}
512
513LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
514 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000515 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000516 Load, Ptr, Name, InsertBef) {
517 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000518 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000519}
520
521LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
522 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000523 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000524 Load, Ptr, Name, InsertAE) {
525 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000526 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000527}
528
529
530//===----------------------------------------------------------------------===//
531// StoreInst Implementation
532//===----------------------------------------------------------------------===//
533
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000534void StoreInst::AssertOK() {
535 assert(isa<PointerType>(getOperand(1)->getType()) &&
536 "Ptr must have pointer type!");
537 assert(getOperand(0)->getType() ==
538 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000539 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000540}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000541
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542
543StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000544 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000545 Ops[0].init(val, this);
546 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000547 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000548 AssertOK();
549}
550
551StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000552 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000553 Ops[0].init(val, this);
554 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000555 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556 AssertOK();
557}
558
Misha Brukmanb1c93172005-04-21 23:48:37 +0000559StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000561 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000562 Ops[0].init(val, this);
563 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000564 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000565 AssertOK();
566}
567
Misha Brukmanb1c93172005-04-21 23:48:37 +0000568StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000570 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571 Ops[0].init(val, this);
572 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000573 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000575}
576
577//===----------------------------------------------------------------------===//
578// GetElementPtrInst Implementation
579//===----------------------------------------------------------------------===//
580
581// checkType - Simple wrapper function to give a better assertion failure
582// message on bad indexes for a gep instruction.
583//
584static inline const Type *checkType(const Type *Ty) {
585 assert(Ty && "Invalid indices for type!");
586 return Ty;
587}
588
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000589void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
590 NumOperands = 1+Idx.size();
591 Use *OL = OperandList = new Use[NumOperands];
592 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000593
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000594 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
595 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000596}
597
598void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000599 NumOperands = 3;
600 Use *OL = OperandList = new Use[3];
601 OL[0].init(Ptr, this);
602 OL[1].init(Idx0, this);
603 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000604}
605
Chris Lattner82981202005-05-03 05:43:30 +0000606void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
607 NumOperands = 2;
608 Use *OL = OperandList = new Use[2];
609 OL[0].init(Ptr, this);
610 OL[1].init(Idx, this);
611}
612
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000614 const std::string &Name, Instruction *InBe)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000615 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
616 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000618 init(Ptr, Idx);
619}
620
621GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000622 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
624 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000626 init(Ptr, Idx);
627}
628
Chris Lattner82981202005-05-03 05:43:30 +0000629GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
630 const std::string &Name, Instruction *InBe)
631 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
632 GetElementPtr, 0, 0, Name, InBe) {
633 init(Ptr, Idx);
634}
635
636GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
637 const std::string &Name, BasicBlock *IAE)
638 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
639 GetElementPtr, 0, 0, Name, IAE) {
640 init(Ptr, Idx);
641}
642
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000643GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
644 const std::string &Name, Instruction *InBe)
645 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
646 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648 init(Ptr, Idx0, Idx1);
649}
650
651GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000652 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
654 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656 init(Ptr, Idx0, Idx1);
657}
658
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659GetElementPtrInst::~GetElementPtrInst() {
660 delete[] OperandList;
661}
662
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663// getIndexedType - Returns the type of the element that would be loaded with
664// a load instruction with the specified parameters.
665//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000666// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667// pointer type.
668//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000669const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000670 const std::vector<Value*> &Idx,
671 bool AllowCompositeLeaf) {
672 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
673
674 // Handle the special case of the empty set index set...
675 if (Idx.empty())
676 if (AllowCompositeLeaf ||
677 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
678 return cast<PointerType>(Ptr)->getElementType();
679 else
680 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000681
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682 unsigned CurIdx = 0;
683 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
684 if (Idx.size() == CurIdx) {
685 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
686 return 0; // Can't load a whole structure or array!?!?
687 }
688
689 Value *Index = Idx[CurIdx++];
690 if (isa<PointerType>(CT) && CurIdx != 1)
691 return 0; // Can only index into pointer types at the first index!
692 if (!CT->indexValid(Index)) return 0;
693 Ptr = CT->getTypeAtIndex(Index);
694
695 // If the new type forwards to another type, then it is in the middle
696 // of being refined to another type (and hence, may have dropped all
697 // references to what it was using before). So, use the new forwarded
698 // type.
699 if (const Type * Ty = Ptr->getForwardedType()) {
700 Ptr = Ty;
701 }
702 }
703 return CurIdx == Idx.size() ? Ptr : 0;
704}
705
Misha Brukmanb1c93172005-04-21 23:48:37 +0000706const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707 Value *Idx0, Value *Idx1,
708 bool AllowCompositeLeaf) {
709 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
710 if (!PTy) return 0; // Type isn't a pointer type!
711
712 // Check the pointer index.
713 if (!PTy->indexValid(Idx0)) return 0;
714
715 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
716 if (!CT || !CT->indexValid(Idx1)) return 0;
717
718 const Type *ElTy = CT->getTypeAtIndex(Idx1);
719 if (AllowCompositeLeaf || ElTy->isFirstClassType())
720 return ElTy;
721 return 0;
722}
723
Chris Lattner82981202005-05-03 05:43:30 +0000724const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
725 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
726 if (!PTy) return 0; // Type isn't a pointer type!
727
728 // Check the pointer index.
729 if (!PTy->indexValid(Idx)) return 0;
730
731 return PTy;
732}
733
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734//===----------------------------------------------------------------------===//
735// BinaryOperator Class
736//===----------------------------------------------------------------------===//
737
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000738void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000739{
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000740 Value *LHS = getOperand(0), *RHS = getOperand(1);
741 assert(LHS->getType() == RHS->getType() &&
742 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743#ifndef NDEBUG
744 switch (iType) {
745 case Add: case Sub:
746 case Mul: case Div:
747 case Rem:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000748 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000749 "Arithmetic operation should return same type as operands!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000750 assert((getType()->isInteger() ||
751 getType()->isFloatingPoint() ||
752 isa<PackedType>(getType()) ) &&
Brian Gaeke02209042004-08-20 06:00:58 +0000753 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000754 break;
755 case And: case Or:
756 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000758 "Logical operation should return same type as operands!");
759 assert(getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +0000760 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000761 break;
762 case SetLT: case SetGT: case SetLE:
763 case SetGE: case SetEQ: case SetNE:
764 assert(getType() == Type::BoolTy && "Setcc must return bool!");
765 default:
766 break;
767 }
768#endif
769}
770
771BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000772 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000773 Instruction *InsertBefore) {
774 assert(S1->getType() == S2->getType() &&
775 "Cannot create binary operator with two operands of differing type!");
776 switch (Op) {
777 // Binary comparison operators...
778 case SetLT: case SetGT: case SetLE:
779 case SetGE: case SetEQ: case SetNE:
780 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
781
782 default:
783 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
784 }
785}
786
787BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000788 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000789 BasicBlock *InsertAtEnd) {
790 BinaryOperator *Res = create(Op, S1, S2, Name);
791 InsertAtEnd->getInstList().push_back(Res);
792 return Res;
793}
794
795BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
796 Instruction *InsertBefore) {
797 if (!Op->getType()->isFloatingPoint())
798 return new BinaryOperator(Instruction::Sub,
799 Constant::getNullValue(Op->getType()), Op,
800 Op->getType(), Name, InsertBefore);
801 else
802 return new BinaryOperator(Instruction::Sub,
803 ConstantFP::get(Op->getType(), -0.0), Op,
804 Op->getType(), Name, InsertBefore);
805}
806
807BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
808 BasicBlock *InsertAtEnd) {
809 if (!Op->getType()->isFloatingPoint())
810 return new BinaryOperator(Instruction::Sub,
811 Constant::getNullValue(Op->getType()), Op,
812 Op->getType(), Name, InsertAtEnd);
813 else
814 return new BinaryOperator(Instruction::Sub,
815 ConstantFP::get(Op->getType(), -0.0), Op,
816 Op->getType(), Name, InsertAtEnd);
817}
818
819BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
820 Instruction *InsertBefore) {
821 return new BinaryOperator(Instruction::Xor, Op,
822 ConstantIntegral::getAllOnesValue(Op->getType()),
823 Op->getType(), Name, InsertBefore);
824}
825
826BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
827 BasicBlock *InsertAtEnd) {
828 return new BinaryOperator(Instruction::Xor, Op,
829 ConstantIntegral::getAllOnesValue(Op->getType()),
830 Op->getType(), Name, InsertAtEnd);
831}
832
833
834// isConstantAllOnes - Helper function for several functions below
835static inline bool isConstantAllOnes(const Value *V) {
836 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
837}
838
839bool BinaryOperator::isNeg(const Value *V) {
840 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
841 if (Bop->getOpcode() == Instruction::Sub)
842 if (!V->getType()->isFloatingPoint())
843 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
844 else
845 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
846 return false;
847}
848
849bool BinaryOperator::isNot(const Value *V) {
850 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
851 return (Bop->getOpcode() == Instruction::Xor &&
852 (isConstantAllOnes(Bop->getOperand(1)) ||
853 isConstantAllOnes(Bop->getOperand(0))));
854 return false;
855}
856
Chris Lattner2c7d1772005-04-24 07:28:37 +0000857Value *BinaryOperator::getNegArgument(Value *BinOp) {
858 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
859 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000860}
861
Chris Lattner2c7d1772005-04-24 07:28:37 +0000862const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
863 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000864}
865
Chris Lattner2c7d1772005-04-24 07:28:37 +0000866Value *BinaryOperator::getNotArgument(Value *BinOp) {
867 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
868 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
869 Value *Op0 = BO->getOperand(0);
870 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000871 if (isConstantAllOnes(Op0)) return Op1;
872
873 assert(isConstantAllOnes(Op1));
874 return Op0;
875}
876
Chris Lattner2c7d1772005-04-24 07:28:37 +0000877const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
878 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000879}
880
881
882// swapOperands - Exchange the two operands to this instruction. This
883// instruction is safe to use on any binary instruction and does not
884// modify the semantics of the instruction. If the instruction is
885// order dependent (SetLT f.e.) the opcode is changed.
886//
887bool BinaryOperator::swapOperands() {
888 if (isCommutative())
889 ; // If the instruction is commutative, it is safe to swap the operands
890 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
891 /// FIXME: SetCC instructions shouldn't all have different opcodes.
892 setOpcode(SCI->getSwappedCondition());
893 else
894 return true; // Can't commute operands
895
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000896 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000897 return false;
898}
899
900
901//===----------------------------------------------------------------------===//
902// SetCondInst Class
903//===----------------------------------------------------------------------===//
904
Misha Brukmanb1c93172005-04-21 23:48:37 +0000905SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906 const std::string &Name, Instruction *InsertBefore)
907 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
908
909 // Make sure it's a valid type... getInverseCondition will assert out if not.
910 assert(getInverseCondition(Opcode));
911}
912
Misha Brukmanb1c93172005-04-21 23:48:37 +0000913SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000914 const std::string &Name, BasicBlock *InsertAtEnd)
915 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
916
917 // Make sure it's a valid type... getInverseCondition will assert out if not.
918 assert(getInverseCondition(Opcode));
919}
920
921// getInverseCondition - Return the inverse of the current condition opcode.
922// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
923//
924Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
925 switch (Opcode) {
926 default:
927 assert(0 && "Unknown setcc opcode!");
928 case SetEQ: return SetNE;
929 case SetNE: return SetEQ;
930 case SetGT: return SetLE;
931 case SetLT: return SetGE;
932 case SetGE: return SetLT;
933 case SetLE: return SetGT;
934 }
935}
936
937// getSwappedCondition - Return the condition opcode that would be the result
938// of exchanging the two operands of the setcc instruction without changing
939// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
940//
941Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
942 switch (Opcode) {
943 default: assert(0 && "Unknown setcc instruction!");
944 case SetEQ: case SetNE: return Opcode;
945 case SetGT: return SetLT;
946 case SetLT: return SetGT;
947 case SetGE: return SetLE;
948 case SetLE: return SetGE;
949 }
950}
951
952//===----------------------------------------------------------------------===//
953// SwitchInst Implementation
954//===----------------------------------------------------------------------===//
955
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000956void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000957 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 ReservedSpace = 2+NumCases*2;
959 NumOperands = 2;
960 OperandList = new Use[ReservedSpace];
961
962 OperandList[0].init(Value, this);
963 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000964}
965
Misha Brukmanb1c93172005-04-21 23:48:37 +0000966SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000967 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
968 SI.getNumOperands()) {
969 Use *OL = OperandList, *InOL = SI.OperandList;
970 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
971 OL[i].init(InOL[i], this);
972 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000973 }
974}
975
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976SwitchInst::~SwitchInst() {
977 delete [] OperandList;
978}
979
980
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981/// addCase - Add an entry to the switch instruction...
982///
Chris Lattner47ac1872005-02-24 05:32:09 +0000983void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000984 unsigned OpNo = NumOperands;
985 if (OpNo+2 > ReservedSpace)
986 resizeOperands(0); // Get more space!
987 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +0000988 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000989 NumOperands = OpNo+2;
990 OperandList[OpNo].init(OnVal, this);
991 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000992}
993
994/// removeCase - This method removes the specified successor from the switch
995/// instruction. Note that this cannot be used to remove the default
996/// destination (successor #0).
997///
998void SwitchInst::removeCase(unsigned idx) {
999 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001000 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
1001
1002 unsigned NumOps = getNumOperands();
1003 Use *OL = OperandList;
1004
1005 // Move everything after this operand down.
1006 //
1007 // FIXME: we could just swap with the end of the list, then erase. However,
1008 // client might not expect this to happen. The code as it is thrashes the
1009 // use/def lists, which is kinda lame.
1010 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
1011 OL[i-2] = OL[i];
1012 OL[i-2+1] = OL[i+1];
1013 }
1014
1015 // Nuke the last value.
1016 OL[NumOps-2].set(0);
1017 OL[NumOps-2+1].set(0);
1018 NumOperands = NumOps-2;
1019}
1020
1021/// resizeOperands - resize operands - This adjusts the length of the operands
1022/// list according to the following behavior:
1023/// 1. If NumOps == 0, grow the operand list in response to a push_back style
1024/// of operation. This grows the number of ops by 1.5 times.
1025/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
1026/// 3. If NumOps == NumOperands, trim the reserved space.
1027///
1028void SwitchInst::resizeOperands(unsigned NumOps) {
1029 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001030 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001031 } else if (NumOps*2 > NumOperands) {
1032 // No resize needed.
1033 if (ReservedSpace >= NumOps) return;
1034 } else if (NumOps == NumOperands) {
1035 if (ReservedSpace == NumOps) return;
1036 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001037 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038 }
1039
1040 ReservedSpace = NumOps;
1041 Use *NewOps = new Use[NumOps];
1042 Use *OldOps = OperandList;
1043 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1044 NewOps[i].init(OldOps[i], this);
1045 OldOps[i].set(0);
1046 }
1047 delete [] OldOps;
1048 OperandList = NewOps;
1049}
1050
1051
1052BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
1053 return getSuccessor(idx);
1054}
1055unsigned SwitchInst::getNumSuccessorsV() const {
1056 return getNumSuccessors();
1057}
1058void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1059 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001060}
Chris Lattnerf22be932004-10-15 23:52:53 +00001061
1062
1063// Define these methods here so vtables don't get emitted into every translation
1064// unit that uses these classes.
1065
1066GetElementPtrInst *GetElementPtrInst::clone() const {
1067 return new GetElementPtrInst(*this);
1068}
1069
1070BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001071 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00001072}
1073
1074MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
1075AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001076FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
Chris Lattnerf22be932004-10-15 23:52:53 +00001077LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
1078StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
1079CastInst *CastInst::clone() const { return new CastInst(*this); }
1080CallInst *CallInst::clone() const { return new CallInst(*this); }
1081ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
1082SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
1083VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
1084VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
1085PHINode *PHINode::clone() const { return new PHINode(*this); }
1086ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
1087BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
1088SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
1089InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
1090UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001091UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}