blob: 44167cbae714ce8e2e79dc48e9a181ab8fb51da5 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVM instructions...
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/BasicBlock.h"
15#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/Support/CallSite.h"
20using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// CallInst Implementation
24//===----------------------------------------------------------------------===//
25
26void CallInst::init(Value *Func, const std::vector<Value*> &Params)
27{
28 Operands.reserve(1+Params.size());
29 Operands.push_back(Use(Func, this));
30
31 const FunctionType *FTy =
32 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
33
34 assert((Params.size() == FTy->getNumParams() ||
35 (FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
36 "Calling a function with bad signature");
37 for (unsigned i = 0; i != Params.size(); i++)
38 Operands.push_back(Use(Params[i], this));
39}
40
41void CallInst::init(Value *Func, Value *Actual1, Value *Actual2)
42{
43 Operands.reserve(3);
44 Operands.push_back(Use(Func, this));
45
46 const FunctionType *MTy =
47 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
48
49 assert((MTy->getNumParams() == 2 ||
50 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
51 "Calling a function with bad signature");
52 Operands.push_back(Use(Actual1, this));
53 Operands.push_back(Use(Actual2, this));
54}
55
56void CallInst::init(Value *Func, Value *Actual)
57{
58 Operands.reserve(2);
59 Operands.push_back(Use(Func, this));
60
61 const FunctionType *MTy =
62 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
63
64 assert((MTy->getNumParams() == 1 ||
65 (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
66 "Calling a function with bad signature");
67 Operands.push_back(Use(Actual, this));
68}
69
70void CallInst::init(Value *Func)
71{
72 Operands.reserve(1);
73 Operands.push_back(Use(Func, this));
74
75 const FunctionType *MTy =
76 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
77
78 assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
79}
80
81CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
82 const std::string &Name, Instruction *InsertBefore)
83 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
84 ->getElementType())->getReturnType(),
85 Instruction::Call, Name, InsertBefore) {
86 init(Func, Params);
87}
88
89CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
90 const std::string &Name, BasicBlock *InsertAtEnd)
91 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
92 ->getElementType())->getReturnType(),
93 Instruction::Call, Name, InsertAtEnd) {
94 init(Func, Params);
95}
96
97CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
98 const std::string &Name, Instruction *InsertBefore)
99 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
100 ->getElementType())->getReturnType(),
101 Instruction::Call, Name, InsertBefore) {
102 init(Func, Actual1, Actual2);
103}
104
105CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
106 const std::string &Name, BasicBlock *InsertAtEnd)
107 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
108 ->getElementType())->getReturnType(),
109 Instruction::Call, Name, InsertAtEnd) {
110 init(Func, Actual1, Actual2);
111}
112
113CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
114 Instruction *InsertBefore)
115 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
116 ->getElementType())->getReturnType(),
117 Instruction::Call, Name, InsertBefore) {
118 init(Func, Actual);
119}
120
121CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
122 BasicBlock *InsertAtEnd)
123 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
124 ->getElementType())->getReturnType(),
125 Instruction::Call, Name, InsertAtEnd) {
126 init(Func, Actual);
127}
128
129CallInst::CallInst(Value *Func, const std::string &Name,
130 Instruction *InsertBefore)
131 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
132 ->getElementType())->getReturnType(),
133 Instruction::Call, Name, InsertBefore) {
134 init(Func);
135}
136
137CallInst::CallInst(Value *Func, const std::string &Name,
138 BasicBlock *InsertAtEnd)
139 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
140 ->getElementType())->getReturnType(),
141 Instruction::Call, Name, InsertAtEnd) {
142 init(Func);
143}
144
145CallInst::CallInst(const CallInst &CI)
146 : Instruction(CI.getType(), Instruction::Call) {
147 Operands.reserve(CI.Operands.size());
148 for (unsigned i = 0; i < CI.Operands.size(); ++i)
149 Operands.push_back(Use(CI.Operands[i], this));
150}
151
152const Function *CallInst::getCalledFunction() const {
153 if (const Function *F = dyn_cast<Function>(Operands[0]))
154 return F;
155 return 0;
156}
157Function *CallInst::getCalledFunction() {
158 if (Function *F = dyn_cast<Function>(Operands[0]))
159 return F;
160 return 0;
161}
162
163
164//===----------------------------------------------------------------------===//
165// InvokeInst Implementation
166//===----------------------------------------------------------------------===//
167
168void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
169 const std::vector<Value*> &Params)
170{
171 Operands.reserve(3+Params.size());
172 Operands.push_back(Use(Fn, this));
173 Operands.push_back(Use((Value*)IfNormal, this));
174 Operands.push_back(Use((Value*)IfException, this));
175 const FunctionType *MTy =
176 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
177
178 assert((Params.size() == MTy->getNumParams()) ||
179 (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
180 "Calling a function with bad signature");
181
182 for (unsigned i = 0; i < Params.size(); i++)
183 Operands.push_back(Use(Params[i], this));
184}
185
186InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
187 BasicBlock *IfException,
188 const std::vector<Value*> &Params,
189 const std::string &Name, Instruction *InsertBefore)
190 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
191 ->getElementType())->getReturnType(),
192 Instruction::Invoke, Name, InsertBefore) {
193 init(Fn, IfNormal, IfException, Params);
194}
195
196InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
197 BasicBlock *IfException,
198 const std::vector<Value*> &Params,
199 const std::string &Name, BasicBlock *InsertAtEnd)
200 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
201 ->getElementType())->getReturnType(),
202 Instruction::Invoke, Name, InsertAtEnd) {
203 init(Fn, IfNormal, IfException, Params);
204}
205
206InvokeInst::InvokeInst(const InvokeInst &CI)
207 : TerminatorInst(CI.getType(), Instruction::Invoke) {
208 Operands.reserve(CI.Operands.size());
209 for (unsigned i = 0; i < CI.Operands.size(); ++i)
210 Operands.push_back(Use(CI.Operands[i], this));
211}
212
213const Function *InvokeInst::getCalledFunction() const {
214 if (const Function *F = dyn_cast<Function>(Operands[0]))
215 return F;
216 return 0;
217}
218Function *InvokeInst::getCalledFunction() {
219 if (Function *F = dyn_cast<Function>(Operands[0]))
220 return F;
221 return 0;
222}
223
224// FIXME: Is this supposed to be here?
225Function *CallSite::getCalledFunction() const {
226 Value *Callee = getCalledValue();
227 if (Function *F = dyn_cast<Function>(Callee))
228 return F;
229 return 0;
230}
231
232//===----------------------------------------------------------------------===//
233// ReturnInst Implementation
234//===----------------------------------------------------------------------===//
235
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000236void ReturnInst::init(Value* RetVal) {
237 if (RetVal && RetVal->getType() != Type::VoidTy) {
238 assert(!isa<BasicBlock>(RetVal) &&
239 "Cannot return basic block. Probably using the incorrect ctor");
240 Operands.reserve(1);
241 Operands.push_back(Use(RetVal, this));
242 }
243}
244
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000245// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
246// emit the vtable for the class in this translation unit.
247void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
248 assert(0 && "ReturnInst has no successors!");
249}
250
251//===----------------------------------------------------------------------===//
252// UnwindInst Implementation
253//===----------------------------------------------------------------------===//
254
255// Likewise for UnwindInst
256void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
257 assert(0 && "UnwindInst has no successors!");
258}
259
260//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000261// UnreachableInst Implementation
262//===----------------------------------------------------------------------===//
263
264void UnreachableInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
265 assert(0 && "UnreachableInst has no successors!");
266}
267
268//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269// BranchInst Implementation
270//===----------------------------------------------------------------------===//
271
272void BranchInst::init(BasicBlock *IfTrue)
273{
274 assert(IfTrue != 0 && "Branch destination may not be null!");
275 Operands.reserve(1);
276 Operands.push_back(Use(IfTrue, this));
277}
278
279void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond)
280{
281 assert(IfTrue && IfFalse && Cond &&
282 "Branch destinations and condition may not be null!");
283 assert(Cond && Cond->getType() == Type::BoolTy &&
284 "May only branch on boolean predicates!");
285 Operands.reserve(3);
286 Operands.push_back(Use(IfTrue, this));
287 Operands.push_back(Use(IfFalse, this));
288 Operands.push_back(Use(Cond, this));
289}
290
291BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
292 Operands.reserve(BI.Operands.size());
293 Operands.push_back(Use(BI.Operands[0], this));
294 if (BI.Operands.size() != 1) {
295 assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
296 Operands.push_back(Use(BI.Operands[1], this));
297 Operands.push_back(Use(BI.Operands[2], this));
298 }
299}
300
301//===----------------------------------------------------------------------===//
302// AllocationInst Implementation
303//===----------------------------------------------------------------------===//
304
305void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) {
306 assert(Ty != Type::VoidTy && "Cannot allocate void elements!");
307 // ArraySize defaults to 1.
308 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
309
310 Operands.reserve(1);
311 assert(ArraySize->getType() == Type::UIntTy &&
312 "Malloc/Allocation array size != UIntTy!");
313
314 Operands.push_back(Use(ArraySize, this));
315}
316
317AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
318 const std::string &Name,
319 Instruction *InsertBefore)
320 : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) {
321 init(Ty, ArraySize, iTy);
322}
323
324AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
325 const std::string &Name,
326 BasicBlock *InsertAtEnd)
327 : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) {
328 init(Ty, ArraySize, iTy);
329}
330
331bool AllocationInst::isArrayAllocation() const {
332 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
333}
334
335const Type *AllocationInst::getAllocatedType() const {
336 return getType()->getElementType();
337}
338
339AllocaInst::AllocaInst(const AllocaInst &AI)
340 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
341 Instruction::Alloca) {
342}
343
344MallocInst::MallocInst(const MallocInst &MI)
345 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
346 Instruction::Malloc) {
347}
348
349//===----------------------------------------------------------------------===//
350// FreeInst Implementation
351//===----------------------------------------------------------------------===//
352
353void FreeInst::init(Value *Ptr)
354{
355 assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
356 Operands.reserve(1);
357 Operands.push_back(Use(Ptr, this));
358}
359
360FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
361 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
362 init(Ptr);
363}
364
365FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
366 : Instruction(Type::VoidTy, Free, "", InsertAtEnd) {
367 init(Ptr);
368}
369
370
371//===----------------------------------------------------------------------===//
372// LoadInst Implementation
373//===----------------------------------------------------------------------===//
374
375void LoadInst::init(Value *Ptr) {
376 assert(Ptr && isa<PointerType>(Ptr->getType()) &&
377 "Ptr must have pointer type.");
378 Operands.reserve(1);
379 Operands.push_back(Use(Ptr, this));
380}
381
382LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
383 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
384 Load, Name, InsertBef), Volatile(false) {
385 init(Ptr);
386}
387
388LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
389 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
390 Load, Name, InsertAE), Volatile(false) {
391 init(Ptr);
392}
393
394LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
395 Instruction *InsertBef)
396 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
397 Load, Name, InsertBef), Volatile(isVolatile) {
398 init(Ptr);
399}
400
401LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
402 BasicBlock *InsertAE)
403 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
404 Load, Name, InsertAE), Volatile(isVolatile) {
405 init(Ptr);
406}
407
408
409//===----------------------------------------------------------------------===//
410// StoreInst Implementation
411//===----------------------------------------------------------------------===//
412
413StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
414 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
415 init(Val, Ptr);
416}
417
418StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd)
419 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) {
420 init(Val, Ptr);
421}
422
423StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
424 Instruction *InsertBefore)
425 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
426 init(Val, Ptr);
427}
428
429StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
430 BasicBlock *InsertAtEnd)
431 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) {
432 init(Val, Ptr);
433}
434
435void StoreInst::init(Value *Val, Value *Ptr) {
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000436 assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!");
437 assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType()
438 && "Ptr must be a pointer to Val type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000439
440 Operands.reserve(2);
441 Operands.push_back(Use(Val, this));
442 Operands.push_back(Use(Ptr, this));
443}
444
445//===----------------------------------------------------------------------===//
446// GetElementPtrInst Implementation
447//===----------------------------------------------------------------------===//
448
449// checkType - Simple wrapper function to give a better assertion failure
450// message on bad indexes for a gep instruction.
451//
452static inline const Type *checkType(const Type *Ty) {
453 assert(Ty && "Invalid indices for type!");
454 return Ty;
455}
456
457void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx)
458{
459 Operands.reserve(1+Idx.size());
460 Operands.push_back(Use(Ptr, this));
461
462 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
463 Operands.push_back(Use(Idx[i], this));
464}
465
466void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
467 Operands.reserve(3);
468 Operands.push_back(Use(Ptr, this));
469 Operands.push_back(Use(Idx0, this));
470 Operands.push_back(Use(Idx1, this));
471}
472
473GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
474 const std::string &Name, Instruction *InBe)
475 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
476 Idx, true))),
477 GetElementPtr, Name, InBe) {
478 init(Ptr, Idx);
479}
480
481GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
482 const std::string &Name, BasicBlock *IAE)
483 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
484 Idx, true))),
485 GetElementPtr, Name, IAE) {
486 init(Ptr, Idx);
487}
488
489GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
490 const std::string &Name, Instruction *InBe)
491 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
492 Idx0, Idx1, true))),
493 GetElementPtr, Name, InBe) {
494 init(Ptr, Idx0, Idx1);
495}
496
497GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
498 const std::string &Name, BasicBlock *IAE)
499 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
500 Idx0, Idx1, true))),
501 GetElementPtr, Name, IAE) {
502 init(Ptr, Idx0, Idx1);
503}
504
505// getIndexedType - Returns the type of the element that would be loaded with
506// a load instruction with the specified parameters.
507//
508// A null type is returned if the indices are invalid for the specified
509// pointer type.
510//
511const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
512 const std::vector<Value*> &Idx,
513 bool AllowCompositeLeaf) {
514 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
515
516 // Handle the special case of the empty set index set...
517 if (Idx.empty())
518 if (AllowCompositeLeaf ||
519 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
520 return cast<PointerType>(Ptr)->getElementType();
521 else
522 return 0;
523
524 unsigned CurIdx = 0;
525 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
526 if (Idx.size() == CurIdx) {
527 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
528 return 0; // Can't load a whole structure or array!?!?
529 }
530
531 Value *Index = Idx[CurIdx++];
532 if (isa<PointerType>(CT) && CurIdx != 1)
533 return 0; // Can only index into pointer types at the first index!
534 if (!CT->indexValid(Index)) return 0;
535 Ptr = CT->getTypeAtIndex(Index);
536
537 // If the new type forwards to another type, then it is in the middle
538 // of being refined to another type (and hence, may have dropped all
539 // references to what it was using before). So, use the new forwarded
540 // type.
541 if (const Type * Ty = Ptr->getForwardedType()) {
542 Ptr = Ty;
543 }
544 }
545 return CurIdx == Idx.size() ? Ptr : 0;
546}
547
548const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
549 Value *Idx0, Value *Idx1,
550 bool AllowCompositeLeaf) {
551 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
552 if (!PTy) return 0; // Type isn't a pointer type!
553
554 // Check the pointer index.
555 if (!PTy->indexValid(Idx0)) return 0;
556
557 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
558 if (!CT || !CT->indexValid(Idx1)) return 0;
559
560 const Type *ElTy = CT->getTypeAtIndex(Idx1);
561 if (AllowCompositeLeaf || ElTy->isFirstClassType())
562 return ElTy;
563 return 0;
564}
565
566//===----------------------------------------------------------------------===//
567// BinaryOperator Class
568//===----------------------------------------------------------------------===//
569
570void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2)
571{
572 Operands.reserve(2);
573 Operands.push_back(Use(S1, this));
574 Operands.push_back(Use(S2, this));
575 assert(S1 && S2 && S1->getType() == S2->getType());
576
577#ifndef NDEBUG
578 switch (iType) {
579 case Add: case Sub:
580 case Mul: case Div:
581 case Rem:
582 assert(getType() == S1->getType() &&
583 "Arithmetic operation should return same type as operands!");
Brian Gaeke02209042004-08-20 06:00:58 +0000584 assert((getType()->isInteger() ||
585 getType()->isFloatingPoint() ||
586 isa<PackedType>(getType()) ) &&
587 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000588 break;
589 case And: case Or:
590 case Xor:
591 assert(getType() == S1->getType() &&
592 "Logical operation should return same type as operands!");
593 assert(getType()->isIntegral() &&
594 "Tried to create an logical operation on a non-integral type!");
595 break;
596 case SetLT: case SetGT: case SetLE:
597 case SetGE: case SetEQ: case SetNE:
598 assert(getType() == Type::BoolTy && "Setcc must return bool!");
599 default:
600 break;
601 }
602#endif
603}
604
605BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
606 const std::string &Name,
607 Instruction *InsertBefore) {
608 assert(S1->getType() == S2->getType() &&
609 "Cannot create binary operator with two operands of differing type!");
610 switch (Op) {
611 // Binary comparison operators...
612 case SetLT: case SetGT: case SetLE:
613 case SetGE: case SetEQ: case SetNE:
614 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
615
616 default:
617 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
618 }
619}
620
621BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
622 const std::string &Name,
623 BasicBlock *InsertAtEnd) {
624 BinaryOperator *Res = create(Op, S1, S2, Name);
625 InsertAtEnd->getInstList().push_back(Res);
626 return Res;
627}
628
629BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
630 Instruction *InsertBefore) {
631 if (!Op->getType()->isFloatingPoint())
632 return new BinaryOperator(Instruction::Sub,
633 Constant::getNullValue(Op->getType()), Op,
634 Op->getType(), Name, InsertBefore);
635 else
636 return new BinaryOperator(Instruction::Sub,
637 ConstantFP::get(Op->getType(), -0.0), Op,
638 Op->getType(), Name, InsertBefore);
639}
640
641BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
642 BasicBlock *InsertAtEnd) {
643 if (!Op->getType()->isFloatingPoint())
644 return new BinaryOperator(Instruction::Sub,
645 Constant::getNullValue(Op->getType()), Op,
646 Op->getType(), Name, InsertAtEnd);
647 else
648 return new BinaryOperator(Instruction::Sub,
649 ConstantFP::get(Op->getType(), -0.0), Op,
650 Op->getType(), Name, InsertAtEnd);
651}
652
653BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
654 Instruction *InsertBefore) {
655 return new BinaryOperator(Instruction::Xor, Op,
656 ConstantIntegral::getAllOnesValue(Op->getType()),
657 Op->getType(), Name, InsertBefore);
658}
659
660BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
661 BasicBlock *InsertAtEnd) {
662 return new BinaryOperator(Instruction::Xor, Op,
663 ConstantIntegral::getAllOnesValue(Op->getType()),
664 Op->getType(), Name, InsertAtEnd);
665}
666
667
668// isConstantAllOnes - Helper function for several functions below
669static inline bool isConstantAllOnes(const Value *V) {
670 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
671}
672
673bool BinaryOperator::isNeg(const Value *V) {
674 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
675 if (Bop->getOpcode() == Instruction::Sub)
676 if (!V->getType()->isFloatingPoint())
677 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
678 else
679 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
680 return false;
681}
682
683bool BinaryOperator::isNot(const Value *V) {
684 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
685 return (Bop->getOpcode() == Instruction::Xor &&
686 (isConstantAllOnes(Bop->getOperand(1)) ||
687 isConstantAllOnes(Bop->getOperand(0))));
688 return false;
689}
690
691Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
692 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
693 return Bop->getOperand(1);
694}
695
696const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
697 return getNegArgument((BinaryOperator*)Bop);
698}
699
700Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
701 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
702 Value *Op0 = Bop->getOperand(0);
703 Value *Op1 = Bop->getOperand(1);
704 if (isConstantAllOnes(Op0)) return Op1;
705
706 assert(isConstantAllOnes(Op1));
707 return Op0;
708}
709
710const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
711 return getNotArgument((BinaryOperator*)Bop);
712}
713
714
715// swapOperands - Exchange the two operands to this instruction. This
716// instruction is safe to use on any binary instruction and does not
717// modify the semantics of the instruction. If the instruction is
718// order dependent (SetLT f.e.) the opcode is changed.
719//
720bool BinaryOperator::swapOperands() {
721 if (isCommutative())
722 ; // If the instruction is commutative, it is safe to swap the operands
723 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
724 /// FIXME: SetCC instructions shouldn't all have different opcodes.
725 setOpcode(SCI->getSwappedCondition());
726 else
727 return true; // Can't commute operands
728
729 std::swap(Operands[0], Operands[1]);
730 return false;
731}
732
733
734//===----------------------------------------------------------------------===//
735// SetCondInst Class
736//===----------------------------------------------------------------------===//
737
738SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
739 const std::string &Name, Instruction *InsertBefore)
740 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
741
742 // Make sure it's a valid type... getInverseCondition will assert out if not.
743 assert(getInverseCondition(Opcode));
744}
745
746SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
747 const std::string &Name, BasicBlock *InsertAtEnd)
748 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
749
750 // Make sure it's a valid type... getInverseCondition will assert out if not.
751 assert(getInverseCondition(Opcode));
752}
753
754// getInverseCondition - Return the inverse of the current condition opcode.
755// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
756//
757Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
758 switch (Opcode) {
759 default:
760 assert(0 && "Unknown setcc opcode!");
761 case SetEQ: return SetNE;
762 case SetNE: return SetEQ;
763 case SetGT: return SetLE;
764 case SetLT: return SetGE;
765 case SetGE: return SetLT;
766 case SetLE: return SetGT;
767 }
768}
769
770// getSwappedCondition - Return the condition opcode that would be the result
771// of exchanging the two operands of the setcc instruction without changing
772// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
773//
774Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
775 switch (Opcode) {
776 default: assert(0 && "Unknown setcc instruction!");
777 case SetEQ: case SetNE: return Opcode;
778 case SetGT: return SetLT;
779 case SetLT: return SetGT;
780 case SetGE: return SetLE;
781 case SetLE: return SetGE;
782 }
783}
784
785//===----------------------------------------------------------------------===//
786// SwitchInst Implementation
787//===----------------------------------------------------------------------===//
788
789void SwitchInst::init(Value *Value, BasicBlock *Default)
790{
791 assert(Value && Default);
792 Operands.push_back(Use(Value, this));
793 Operands.push_back(Use(Default, this));
794}
795
796SwitchInst::SwitchInst(const SwitchInst &SI)
797 : TerminatorInst(Instruction::Switch) {
798 Operands.reserve(SI.Operands.size());
799
800 for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
801 Operands.push_back(Use(SI.Operands[i], this));
802 Operands.push_back(Use(SI.Operands[i+1], this));
803 }
804}
805
806/// addCase - Add an entry to the switch instruction...
807///
808void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
809 Operands.push_back(Use((Value*)OnVal, this));
810 Operands.push_back(Use((Value*)Dest, this));
811}
812
813/// removeCase - This method removes the specified successor from the switch
814/// instruction. Note that this cannot be used to remove the default
815/// destination (successor #0).
816///
817void SwitchInst::removeCase(unsigned idx) {
818 assert(idx != 0 && "Cannot remove the default case!");
819 assert(idx*2 < Operands.size() && "Successor index out of range!!!");
820 Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);
821}
Chris Lattnerf22be932004-10-15 23:52:53 +0000822
823
824// Define these methods here so vtables don't get emitted into every translation
825// unit that uses these classes.
826
827GetElementPtrInst *GetElementPtrInst::clone() const {
828 return new GetElementPtrInst(*this);
829}
830
831BinaryOperator *BinaryOperator::clone() const {
832 return create(getOpcode(), Operands[0], Operands[1]);
833}
834
835MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
836AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
837FreeInst *FreeInst::clone() const { return new FreeInst(Operands[0]); }
838LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
839StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
840CastInst *CastInst::clone() const { return new CastInst(*this); }
841CallInst *CallInst::clone() const { return new CallInst(*this); }
842ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
843SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
844VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
845VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
846PHINode *PHINode::clone() const { return new PHINode(*this); }
847ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
848BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
849SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
850InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
851UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000852UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}