blob: d5adaa79d952f1ffc1a76fb57be409272127c37c [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()) {
Chris Lattner06038452005-05-06 05:51:46 +0000252 setTailCall(CI.isTailCall());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253 Use *OL = OperandList;
254 Use *InOL = CI.OperandList;
255 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
256 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257}
258
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259
260//===----------------------------------------------------------------------===//
261// InvokeInst Implementation
262//===----------------------------------------------------------------------===//
263
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000264InvokeInst::~InvokeInst() {
265 delete [] OperandList;
266}
267
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000269 const std::vector<Value*> &Params) {
270 NumOperands = 3+Params.size();
271 Use *OL = OperandList = new Use[3+Params.size()];
272 OL[0].init(Fn, this);
273 OL[1].init(IfNormal, this);
274 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000277
278 assert((Params.size() == FTy->getNumParams()) ||
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279 (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000281
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000282 for (unsigned i = 0, e = Params.size(); i != e; i++)
283 OL[i+3].init(Params[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
286InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
287 BasicBlock *IfException,
288 const std::vector<Value*> &Params,
289 const std::string &Name, Instruction *InsertBefore)
290 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
291 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000292 Instruction::Invoke, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000293 init(Fn, IfNormal, IfException, Params);
294}
295
296InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
297 BasicBlock *IfException,
298 const std::vector<Value*> &Params,
299 const std::string &Name, BasicBlock *InsertAtEnd)
300 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
301 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 init(Fn, IfNormal, IfException, Params);
304}
305
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000307 : TerminatorInst(II.getType(), Instruction::Invoke,
308 new Use[II.getNumOperands()], II.getNumOperands()) {
309 Use *OL = OperandList, *InOL = II.OperandList;
310 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
311 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312}
313
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000314BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
315 return getSuccessor(idx);
316}
317unsigned InvokeInst::getNumSuccessorsV() const {
318 return getNumSuccessors();
319}
320void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
321 return setSuccessor(idx, B);
322}
323
324
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000325//===----------------------------------------------------------------------===//
326// ReturnInst Implementation
327//===----------------------------------------------------------------------===//
328
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000329void ReturnInst::init(Value *retVal) {
330 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000331 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000332 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000333 NumOperands = 1;
334 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000335 }
336}
337
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338unsigned ReturnInst::getNumSuccessorsV() const {
339 return getNumSuccessors();
340}
341
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
343// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000344void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345 assert(0 && "ReturnInst has no successors!");
346}
347
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000348BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
349 assert(0 && "ReturnInst has no successors!");
350 abort();
351 return 0;
352}
353
354
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355//===----------------------------------------------------------------------===//
356// UnwindInst Implementation
357//===----------------------------------------------------------------------===//
358
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000359unsigned UnwindInst::getNumSuccessorsV() const {
360 return getNumSuccessors();
361}
362
363void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364 assert(0 && "UnwindInst has no successors!");
365}
366
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000367BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
368 assert(0 && "UnwindInst has no successors!");
369 abort();
370 return 0;
371}
372
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000374// UnreachableInst Implementation
375//===----------------------------------------------------------------------===//
376
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000377unsigned UnreachableInst::getNumSuccessorsV() const {
378 return getNumSuccessors();
379}
380
381void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
382 assert(0 && "UnwindInst has no successors!");
383}
384
385BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
386 assert(0 && "UnwindInst has no successors!");
387 abort();
388 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000389}
390
391//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000392// BranchInst Implementation
393//===----------------------------------------------------------------------===//
394
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000395void BranchInst::AssertOK() {
396 if (isConditional())
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397 assert(getCondition()->getType() == Type::BoolTy &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000398 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399}
400
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000401BranchInst::BranchInst(const BranchInst &BI) :
402 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
403 OperandList[0].init(BI.getOperand(0), this);
404 if (BI.getNumOperands() != 1) {
405 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
406 OperandList[1].init(BI.getOperand(1), this);
407 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000408 }
409}
410
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000411BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
412 return getSuccessor(idx);
413}
414unsigned BranchInst::getNumSuccessorsV() const {
415 return getNumSuccessors();
416}
417void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
418 setSuccessor(idx, B);
419}
420
421
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000422//===----------------------------------------------------------------------===//
423// AllocationInst Implementation
424//===----------------------------------------------------------------------===//
425
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000426static Value *getAISize(Value *Amt) {
427 if (!Amt)
428 Amt = ConstantUInt::get(Type::UIntTy, 1);
429 else
430 assert(Amt->getType() == Type::UIntTy &&
431 "Malloc/Allocation array size != UIntTy!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000432 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000433}
434
Misha Brukmanb1c93172005-04-21 23:48:37 +0000435AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000436 const std::string &Name,
437 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000438 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
439 Name, InsertBefore) {
440 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000441}
442
Misha Brukmanb1c93172005-04-21 23:48:37 +0000443AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000444 const std::string &Name,
445 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000446 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
447 Name, InsertAtEnd) {
448 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000449}
450
451bool AllocationInst::isArrayAllocation() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000452 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
453 return CUI->getValue() != 1;
454 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000455}
456
457const Type *AllocationInst::getAllocatedType() const {
458 return getType()->getElementType();
459}
460
461AllocaInst::AllocaInst(const AllocaInst &AI)
462 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
463 Instruction::Alloca) {
464}
465
466MallocInst::MallocInst(const MallocInst &MI)
467 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
468 Instruction::Malloc) {
469}
470
471//===----------------------------------------------------------------------===//
472// FreeInst Implementation
473//===----------------------------------------------------------------------===//
474
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000475void FreeInst::AssertOK() {
476 assert(isa<PointerType>(getOperand(0)->getType()) &&
477 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000478}
479
480FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000481 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
482 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000483}
484
485FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000486 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
487 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000488}
489
490
491//===----------------------------------------------------------------------===//
492// LoadInst Implementation
493//===----------------------------------------------------------------------===//
494
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000495void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000496 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000497 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000498}
499
500LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000501 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000502 Load, Ptr, Name, InsertBef) {
503 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000504 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000505}
506
507LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000508 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000509 Load, Ptr, Name, InsertAE) {
510 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000511 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000512}
513
514LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
515 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000516 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000517 Load, Ptr, Name, InsertBef) {
518 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000519 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000520}
521
522LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
523 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000524 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000525 Load, Ptr, Name, InsertAE) {
526 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000527 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528}
529
530
531//===----------------------------------------------------------------------===//
532// StoreInst Implementation
533//===----------------------------------------------------------------------===//
534
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000535void StoreInst::AssertOK() {
536 assert(isa<PointerType>(getOperand(1)->getType()) &&
537 "Ptr must have pointer type!");
538 assert(getOperand(0)->getType() ==
539 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000540 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000541}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000542
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000543
544StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000545 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000546 Ops[0].init(val, this);
547 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000548 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000549 AssertOK();
550}
551
552StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000553 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000554 Ops[0].init(val, this);
555 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000556 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000557 AssertOK();
558}
559
Misha Brukmanb1c93172005-04-21 23:48:37 +0000560StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000561 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000562 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000563 Ops[0].init(val, this);
564 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000565 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000566 AssertOK();
567}
568
Misha Brukmanb1c93172005-04-21 23:48:37 +0000569StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000571 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572 Ops[0].init(val, this);
573 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000574 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000576}
577
578//===----------------------------------------------------------------------===//
579// GetElementPtrInst Implementation
580//===----------------------------------------------------------------------===//
581
582// checkType - Simple wrapper function to give a better assertion failure
583// message on bad indexes for a gep instruction.
584//
585static inline const Type *checkType(const Type *Ty) {
586 assert(Ty && "Invalid indices for type!");
587 return Ty;
588}
589
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000590void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
591 NumOperands = 1+Idx.size();
592 Use *OL = OperandList = new Use[NumOperands];
593 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000594
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000595 for (unsigned i = 0, e = Idx.size(); i != e; ++i)
596 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000597}
598
599void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000600 NumOperands = 3;
601 Use *OL = OperandList = new Use[3];
602 OL[0].init(Ptr, this);
603 OL[1].init(Idx0, this);
604 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000605}
606
Chris Lattner82981202005-05-03 05:43:30 +0000607void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
608 NumOperands = 2;
609 Use *OL = OperandList = new Use[2];
610 OL[0].init(Ptr, this);
611 OL[1].init(Idx, this);
612}
613
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000614GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000615 const std::string &Name, Instruction *InBe)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000616 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
617 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000618 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619 init(Ptr, Idx);
620}
621
622GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
Misha Brukman96eb8782005-03-16 05:42:00 +0000623 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000624 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
625 Idx, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000626 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000627 init(Ptr, Idx);
628}
629
Chris Lattner82981202005-05-03 05:43:30 +0000630GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
631 const std::string &Name, Instruction *InBe)
632 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
633 GetElementPtr, 0, 0, Name, InBe) {
634 init(Ptr, Idx);
635}
636
637GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
638 const std::string &Name, BasicBlock *IAE)
639 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
640 GetElementPtr, 0, 0, Name, IAE) {
641 init(Ptr, Idx);
642}
643
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000644GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
645 const std::string &Name, Instruction *InBe)
646 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
647 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000649 init(Ptr, Idx0, Idx1);
650}
651
652GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000653 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000654 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
655 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000657 init(Ptr, Idx0, Idx1);
658}
659
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660GetElementPtrInst::~GetElementPtrInst() {
661 delete[] OperandList;
662}
663
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000664// getIndexedType - Returns the type of the element that would be loaded with
665// a load instruction with the specified parameters.
666//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000667// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668// pointer type.
669//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000670const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000671 const std::vector<Value*> &Idx,
672 bool AllowCompositeLeaf) {
673 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
674
675 // Handle the special case of the empty set index set...
676 if (Idx.empty())
677 if (AllowCompositeLeaf ||
678 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
679 return cast<PointerType>(Ptr)->getElementType();
680 else
681 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000682
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000683 unsigned CurIdx = 0;
684 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
685 if (Idx.size() == CurIdx) {
686 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
687 return 0; // Can't load a whole structure or array!?!?
688 }
689
690 Value *Index = Idx[CurIdx++];
691 if (isa<PointerType>(CT) && CurIdx != 1)
692 return 0; // Can only index into pointer types at the first index!
693 if (!CT->indexValid(Index)) return 0;
694 Ptr = CT->getTypeAtIndex(Index);
695
696 // If the new type forwards to another type, then it is in the middle
697 // of being refined to another type (and hence, may have dropped all
698 // references to what it was using before). So, use the new forwarded
699 // type.
700 if (const Type * Ty = Ptr->getForwardedType()) {
701 Ptr = Ty;
702 }
703 }
704 return CurIdx == Idx.size() ? Ptr : 0;
705}
706
Misha Brukmanb1c93172005-04-21 23:48:37 +0000707const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000708 Value *Idx0, Value *Idx1,
709 bool AllowCompositeLeaf) {
710 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
711 if (!PTy) return 0; // Type isn't a pointer type!
712
713 // Check the pointer index.
714 if (!PTy->indexValid(Idx0)) return 0;
715
716 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
717 if (!CT || !CT->indexValid(Idx1)) return 0;
718
719 const Type *ElTy = CT->getTypeAtIndex(Idx1);
720 if (AllowCompositeLeaf || ElTy->isFirstClassType())
721 return ElTy;
722 return 0;
723}
724
Chris Lattner82981202005-05-03 05:43:30 +0000725const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
726 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
727 if (!PTy) return 0; // Type isn't a pointer type!
728
729 // Check the pointer index.
730 if (!PTy->indexValid(Idx)) return 0;
731
Chris Lattnerc2233332005-05-03 16:44:45 +0000732 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000733}
734
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000735//===----------------------------------------------------------------------===//
736// BinaryOperator Class
737//===----------------------------------------------------------------------===//
738
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000739void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740{
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741 Value *LHS = getOperand(0), *RHS = getOperand(1);
742 assert(LHS->getType() == RHS->getType() &&
743 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000744#ifndef NDEBUG
745 switch (iType) {
746 case Add: case Sub:
747 case Mul: case Div:
748 case Rem:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000749 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750 "Arithmetic operation should return same type as operands!");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000751 assert((getType()->isInteger() ||
752 getType()->isFloatingPoint() ||
753 isa<PackedType>(getType()) ) &&
Brian Gaeke02209042004-08-20 06:00:58 +0000754 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755 break;
756 case And: case Or:
757 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000758 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759 "Logical operation should return same type as operands!");
760 assert(getType()->isIntegral() &&
Misha Brukman3852f652005-01-27 06:46:38 +0000761 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000762 break;
763 case SetLT: case SetGT: case SetLE:
764 case SetGE: case SetEQ: case SetNE:
765 assert(getType() == Type::BoolTy && "Setcc must return bool!");
766 default:
767 break;
768 }
769#endif
770}
771
772BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000773 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774 Instruction *InsertBefore) {
775 assert(S1->getType() == S2->getType() &&
776 "Cannot create binary operator with two operands of differing type!");
777 switch (Op) {
778 // Binary comparison operators...
779 case SetLT: case SetGT: case SetLE:
780 case SetGE: case SetEQ: case SetNE:
781 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
782
783 default:
784 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
785 }
786}
787
788BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +0000789 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790 BasicBlock *InsertAtEnd) {
791 BinaryOperator *Res = create(Op, S1, S2, Name);
792 InsertAtEnd->getInstList().push_back(Res);
793 return Res;
794}
795
796BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
797 Instruction *InsertBefore) {
798 if (!Op->getType()->isFloatingPoint())
799 return new BinaryOperator(Instruction::Sub,
800 Constant::getNullValue(Op->getType()), Op,
801 Op->getType(), Name, InsertBefore);
802 else
803 return new BinaryOperator(Instruction::Sub,
804 ConstantFP::get(Op->getType(), -0.0), Op,
805 Op->getType(), Name, InsertBefore);
806}
807
808BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
809 BasicBlock *InsertAtEnd) {
810 if (!Op->getType()->isFloatingPoint())
811 return new BinaryOperator(Instruction::Sub,
812 Constant::getNullValue(Op->getType()), Op,
813 Op->getType(), Name, InsertAtEnd);
814 else
815 return new BinaryOperator(Instruction::Sub,
816 ConstantFP::get(Op->getType(), -0.0), Op,
817 Op->getType(), Name, InsertAtEnd);
818}
819
820BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
821 Instruction *InsertBefore) {
822 return new BinaryOperator(Instruction::Xor, Op,
823 ConstantIntegral::getAllOnesValue(Op->getType()),
824 Op->getType(), Name, InsertBefore);
825}
826
827BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
828 BasicBlock *InsertAtEnd) {
829 return new BinaryOperator(Instruction::Xor, Op,
830 ConstantIntegral::getAllOnesValue(Op->getType()),
831 Op->getType(), Name, InsertAtEnd);
832}
833
834
835// isConstantAllOnes - Helper function for several functions below
836static inline bool isConstantAllOnes(const Value *V) {
837 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
838}
839
840bool BinaryOperator::isNeg(const Value *V) {
841 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
842 if (Bop->getOpcode() == Instruction::Sub)
843 if (!V->getType()->isFloatingPoint())
844 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
845 else
846 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
847 return false;
848}
849
850bool BinaryOperator::isNot(const Value *V) {
851 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
852 return (Bop->getOpcode() == Instruction::Xor &&
853 (isConstantAllOnes(Bop->getOperand(1)) ||
854 isConstantAllOnes(Bop->getOperand(0))));
855 return false;
856}
857
Chris Lattner2c7d1772005-04-24 07:28:37 +0000858Value *BinaryOperator::getNegArgument(Value *BinOp) {
859 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
860 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000861}
862
Chris Lattner2c7d1772005-04-24 07:28:37 +0000863const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
864 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000865}
866
Chris Lattner2c7d1772005-04-24 07:28:37 +0000867Value *BinaryOperator::getNotArgument(Value *BinOp) {
868 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
869 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
870 Value *Op0 = BO->getOperand(0);
871 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000872 if (isConstantAllOnes(Op0)) return Op1;
873
874 assert(isConstantAllOnes(Op1));
875 return Op0;
876}
877
Chris Lattner2c7d1772005-04-24 07:28:37 +0000878const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
879 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
882
883// swapOperands - Exchange the two operands to this instruction. This
884// instruction is safe to use on any binary instruction and does not
885// modify the semantics of the instruction. If the instruction is
886// order dependent (SetLT f.e.) the opcode is changed.
887//
888bool BinaryOperator::swapOperands() {
889 if (isCommutative())
890 ; // If the instruction is commutative, it is safe to swap the operands
891 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
892 /// FIXME: SetCC instructions shouldn't all have different opcodes.
893 setOpcode(SCI->getSwappedCondition());
894 else
895 return true; // Can't commute operands
896
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000897 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000898 return false;
899}
900
901
902//===----------------------------------------------------------------------===//
903// SetCondInst Class
904//===----------------------------------------------------------------------===//
905
Misha Brukmanb1c93172005-04-21 23:48:37 +0000906SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907 const std::string &Name, Instruction *InsertBefore)
908 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
909
910 // Make sure it's a valid type... getInverseCondition will assert out if not.
911 assert(getInverseCondition(Opcode));
912}
913
Misha Brukmanb1c93172005-04-21 23:48:37 +0000914SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000915 const std::string &Name, BasicBlock *InsertAtEnd)
916 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
917
918 // Make sure it's a valid type... getInverseCondition will assert out if not.
919 assert(getInverseCondition(Opcode));
920}
921
922// getInverseCondition - Return the inverse of the current condition opcode.
923// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
924//
925Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
926 switch (Opcode) {
927 default:
928 assert(0 && "Unknown setcc opcode!");
929 case SetEQ: return SetNE;
930 case SetNE: return SetEQ;
931 case SetGT: return SetLE;
932 case SetLT: return SetGE;
933 case SetGE: return SetLT;
934 case SetLE: return SetGT;
935 }
936}
937
938// getSwappedCondition - Return the condition opcode that would be the result
939// of exchanging the two operands of the setcc instruction without changing
940// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
941//
942Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
943 switch (Opcode) {
944 default: assert(0 && "Unknown setcc instruction!");
945 case SetEQ: case SetNE: return Opcode;
946 case SetGT: return SetLT;
947 case SetLT: return SetGT;
948 case SetGE: return SetLE;
949 case SetLE: return SetGE;
950 }
951}
952
953//===----------------------------------------------------------------------===//
954// SwitchInst Implementation
955//===----------------------------------------------------------------------===//
956
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000957void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000958 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000959 ReservedSpace = 2+NumCases*2;
960 NumOperands = 2;
961 OperandList = new Use[ReservedSpace];
962
963 OperandList[0].init(Value, this);
964 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000965}
966
Misha Brukmanb1c93172005-04-21 23:48:37 +0000967SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000968 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
969 SI.getNumOperands()) {
970 Use *OL = OperandList, *InOL = SI.OperandList;
971 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
972 OL[i].init(InOL[i], this);
973 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000974 }
975}
976
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000977SwitchInst::~SwitchInst() {
978 delete [] OperandList;
979}
980
981
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000982/// addCase - Add an entry to the switch instruction...
983///
Chris Lattner47ac1872005-02-24 05:32:09 +0000984void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000985 unsigned OpNo = NumOperands;
986 if (OpNo+2 > ReservedSpace)
987 resizeOperands(0); // Get more space!
988 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +0000989 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000990 NumOperands = OpNo+2;
991 OperandList[OpNo].init(OnVal, this);
992 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000993}
994
995/// removeCase - This method removes the specified successor from the switch
996/// instruction. Note that this cannot be used to remove the default
997/// destination (successor #0).
998///
999void SwitchInst::removeCase(unsigned idx) {
1000 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001001 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
1002
1003 unsigned NumOps = getNumOperands();
1004 Use *OL = OperandList;
1005
1006 // Move everything after this operand down.
1007 //
1008 // FIXME: we could just swap with the end of the list, then erase. However,
1009 // client might not expect this to happen. The code as it is thrashes the
1010 // use/def lists, which is kinda lame.
1011 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
1012 OL[i-2] = OL[i];
1013 OL[i-2+1] = OL[i+1];
1014 }
1015
1016 // Nuke the last value.
1017 OL[NumOps-2].set(0);
1018 OL[NumOps-2+1].set(0);
1019 NumOperands = NumOps-2;
1020}
1021
1022/// resizeOperands - resize operands - This adjusts the length of the operands
1023/// list according to the following behavior:
1024/// 1. If NumOps == 0, grow the operand list in response to a push_back style
1025/// of operation. This grows the number of ops by 1.5 times.
1026/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
1027/// 3. If NumOps == NumOperands, trim the reserved space.
1028///
1029void SwitchInst::resizeOperands(unsigned NumOps) {
1030 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001031 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001032 } else if (NumOps*2 > NumOperands) {
1033 // No resize needed.
1034 if (ReservedSpace >= NumOps) return;
1035 } else if (NumOps == NumOperands) {
1036 if (ReservedSpace == NumOps) return;
1037 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00001038 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001039 }
1040
1041 ReservedSpace = NumOps;
1042 Use *NewOps = new Use[NumOps];
1043 Use *OldOps = OperandList;
1044 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1045 NewOps[i].init(OldOps[i], this);
1046 OldOps[i].set(0);
1047 }
1048 delete [] OldOps;
1049 OperandList = NewOps;
1050}
1051
1052
1053BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
1054 return getSuccessor(idx);
1055}
1056unsigned SwitchInst::getNumSuccessorsV() const {
1057 return getNumSuccessors();
1058}
1059void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1060 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001061}
Chris Lattnerf22be932004-10-15 23:52:53 +00001062
1063
1064// Define these methods here so vtables don't get emitted into every translation
1065// unit that uses these classes.
1066
1067GetElementPtrInst *GetElementPtrInst::clone() const {
1068 return new GetElementPtrInst(*this);
1069}
1070
1071BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001072 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00001073}
1074
1075MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
1076AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001077FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
Chris Lattnerf22be932004-10-15 23:52:53 +00001078LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
1079StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
1080CastInst *CastInst::clone() const { return new CastInst(*this); }
1081CallInst *CallInst::clone() const { return new CallInst(*this); }
1082ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
1083SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
1084VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
1085VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
1086PHINode *PHINode::clone() const { return new PHINode(*this); }
1087ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
1088BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
1089SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
1090InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
1091UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001092UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}