blob: 029ee6a0f6b5c83de203207fbb45ea42861d7c9e [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
236// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
237// emit the vtable for the class in this translation unit.
238void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
239 assert(0 && "ReturnInst has no successors!");
240}
241
242//===----------------------------------------------------------------------===//
243// UnwindInst Implementation
244//===----------------------------------------------------------------------===//
245
246// Likewise for UnwindInst
247void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
248 assert(0 && "UnwindInst has no successors!");
249}
250
251//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000252// UnreachableInst Implementation
253//===----------------------------------------------------------------------===//
254
255void UnreachableInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
256 assert(0 && "UnreachableInst has no successors!");
257}
258
259//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260// BranchInst Implementation
261//===----------------------------------------------------------------------===//
262
263void BranchInst::init(BasicBlock *IfTrue)
264{
265 assert(IfTrue != 0 && "Branch destination may not be null!");
266 Operands.reserve(1);
267 Operands.push_back(Use(IfTrue, this));
268}
269
270void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond)
271{
272 assert(IfTrue && IfFalse && Cond &&
273 "Branch destinations and condition may not be null!");
274 assert(Cond && Cond->getType() == Type::BoolTy &&
275 "May only branch on boolean predicates!");
276 Operands.reserve(3);
277 Operands.push_back(Use(IfTrue, this));
278 Operands.push_back(Use(IfFalse, this));
279 Operands.push_back(Use(Cond, this));
280}
281
282BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
283 Operands.reserve(BI.Operands.size());
284 Operands.push_back(Use(BI.Operands[0], this));
285 if (BI.Operands.size() != 1) {
286 assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
287 Operands.push_back(Use(BI.Operands[1], this));
288 Operands.push_back(Use(BI.Operands[2], this));
289 }
290}
291
292//===----------------------------------------------------------------------===//
293// AllocationInst Implementation
294//===----------------------------------------------------------------------===//
295
296void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) {
297 assert(Ty != Type::VoidTy && "Cannot allocate void elements!");
298 // ArraySize defaults to 1.
299 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
300
301 Operands.reserve(1);
302 assert(ArraySize->getType() == Type::UIntTy &&
303 "Malloc/Allocation array size != UIntTy!");
304
305 Operands.push_back(Use(ArraySize, this));
306}
307
308AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
309 const std::string &Name,
310 Instruction *InsertBefore)
311 : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) {
312 init(Ty, ArraySize, iTy);
313}
314
315AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
316 const std::string &Name,
317 BasicBlock *InsertAtEnd)
318 : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) {
319 init(Ty, ArraySize, iTy);
320}
321
322bool AllocationInst::isArrayAllocation() const {
323 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
324}
325
326const Type *AllocationInst::getAllocatedType() const {
327 return getType()->getElementType();
328}
329
330AllocaInst::AllocaInst(const AllocaInst &AI)
331 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
332 Instruction::Alloca) {
333}
334
335MallocInst::MallocInst(const MallocInst &MI)
336 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
337 Instruction::Malloc) {
338}
339
340//===----------------------------------------------------------------------===//
341// FreeInst Implementation
342//===----------------------------------------------------------------------===//
343
344void FreeInst::init(Value *Ptr)
345{
346 assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
347 Operands.reserve(1);
348 Operands.push_back(Use(Ptr, this));
349}
350
351FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
352 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
353 init(Ptr);
354}
355
356FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
357 : Instruction(Type::VoidTy, Free, "", InsertAtEnd) {
358 init(Ptr);
359}
360
361
362//===----------------------------------------------------------------------===//
363// LoadInst Implementation
364//===----------------------------------------------------------------------===//
365
366void LoadInst::init(Value *Ptr) {
367 assert(Ptr && isa<PointerType>(Ptr->getType()) &&
368 "Ptr must have pointer type.");
369 Operands.reserve(1);
370 Operands.push_back(Use(Ptr, this));
371}
372
373LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
374 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
375 Load, Name, InsertBef), Volatile(false) {
376 init(Ptr);
377}
378
379LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
380 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
381 Load, Name, InsertAE), Volatile(false) {
382 init(Ptr);
383}
384
385LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
386 Instruction *InsertBef)
387 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
388 Load, Name, InsertBef), Volatile(isVolatile) {
389 init(Ptr);
390}
391
392LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
393 BasicBlock *InsertAE)
394 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
395 Load, Name, InsertAE), Volatile(isVolatile) {
396 init(Ptr);
397}
398
399
400//===----------------------------------------------------------------------===//
401// StoreInst Implementation
402//===----------------------------------------------------------------------===//
403
404StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
405 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
406 init(Val, Ptr);
407}
408
409StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd)
410 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) {
411 init(Val, Ptr);
412}
413
414StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
415 Instruction *InsertBefore)
416 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
417 init(Val, Ptr);
418}
419
420StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
421 BasicBlock *InsertAtEnd)
422 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) {
423 init(Val, Ptr);
424}
425
426void StoreInst::init(Value *Val, Value *Ptr) {
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000427 assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!");
428 assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType()
429 && "Ptr must be a pointer to Val type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000430
431 Operands.reserve(2);
432 Operands.push_back(Use(Val, this));
433 Operands.push_back(Use(Ptr, this));
434}
435
436//===----------------------------------------------------------------------===//
437// GetElementPtrInst Implementation
438//===----------------------------------------------------------------------===//
439
440// checkType - Simple wrapper function to give a better assertion failure
441// message on bad indexes for a gep instruction.
442//
443static inline const Type *checkType(const Type *Ty) {
444 assert(Ty && "Invalid indices for type!");
445 return Ty;
446}
447
448void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx)
449{
450 Operands.reserve(1+Idx.size());
451 Operands.push_back(Use(Ptr, this));
452
453 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
454 Operands.push_back(Use(Idx[i], this));
455}
456
457void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
458 Operands.reserve(3);
459 Operands.push_back(Use(Ptr, this));
460 Operands.push_back(Use(Idx0, this));
461 Operands.push_back(Use(Idx1, this));
462}
463
464GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
465 const std::string &Name, Instruction *InBe)
466 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
467 Idx, true))),
468 GetElementPtr, Name, InBe) {
469 init(Ptr, Idx);
470}
471
472GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
473 const std::string &Name, BasicBlock *IAE)
474 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
475 Idx, true))),
476 GetElementPtr, Name, IAE) {
477 init(Ptr, Idx);
478}
479
480GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
481 const std::string &Name, Instruction *InBe)
482 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
483 Idx0, Idx1, true))),
484 GetElementPtr, Name, InBe) {
485 init(Ptr, Idx0, Idx1);
486}
487
488GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
489 const std::string &Name, BasicBlock *IAE)
490 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
491 Idx0, Idx1, true))),
492 GetElementPtr, Name, IAE) {
493 init(Ptr, Idx0, Idx1);
494}
495
496// getIndexedType - Returns the type of the element that would be loaded with
497// a load instruction with the specified parameters.
498//
499// A null type is returned if the indices are invalid for the specified
500// pointer type.
501//
502const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
503 const std::vector<Value*> &Idx,
504 bool AllowCompositeLeaf) {
505 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
506
507 // Handle the special case of the empty set index set...
508 if (Idx.empty())
509 if (AllowCompositeLeaf ||
510 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
511 return cast<PointerType>(Ptr)->getElementType();
512 else
513 return 0;
514
515 unsigned CurIdx = 0;
516 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
517 if (Idx.size() == CurIdx) {
518 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
519 return 0; // Can't load a whole structure or array!?!?
520 }
521
522 Value *Index = Idx[CurIdx++];
523 if (isa<PointerType>(CT) && CurIdx != 1)
524 return 0; // Can only index into pointer types at the first index!
525 if (!CT->indexValid(Index)) return 0;
526 Ptr = CT->getTypeAtIndex(Index);
527
528 // If the new type forwards to another type, then it is in the middle
529 // of being refined to another type (and hence, may have dropped all
530 // references to what it was using before). So, use the new forwarded
531 // type.
532 if (const Type * Ty = Ptr->getForwardedType()) {
533 Ptr = Ty;
534 }
535 }
536 return CurIdx == Idx.size() ? Ptr : 0;
537}
538
539const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
540 Value *Idx0, Value *Idx1,
541 bool AllowCompositeLeaf) {
542 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
543 if (!PTy) return 0; // Type isn't a pointer type!
544
545 // Check the pointer index.
546 if (!PTy->indexValid(Idx0)) return 0;
547
548 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
549 if (!CT || !CT->indexValid(Idx1)) return 0;
550
551 const Type *ElTy = CT->getTypeAtIndex(Idx1);
552 if (AllowCompositeLeaf || ElTy->isFirstClassType())
553 return ElTy;
554 return 0;
555}
556
557//===----------------------------------------------------------------------===//
558// BinaryOperator Class
559//===----------------------------------------------------------------------===//
560
561void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2)
562{
563 Operands.reserve(2);
564 Operands.push_back(Use(S1, this));
565 Operands.push_back(Use(S2, this));
566 assert(S1 && S2 && S1->getType() == S2->getType());
567
568#ifndef NDEBUG
569 switch (iType) {
570 case Add: case Sub:
571 case Mul: case Div:
572 case Rem:
573 assert(getType() == S1->getType() &&
574 "Arithmetic operation should return same type as operands!");
Brian Gaeke02209042004-08-20 06:00:58 +0000575 assert((getType()->isInteger() ||
576 getType()->isFloatingPoint() ||
577 isa<PackedType>(getType()) ) &&
578 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579 break;
580 case And: case Or:
581 case Xor:
582 assert(getType() == S1->getType() &&
583 "Logical operation should return same type as operands!");
584 assert(getType()->isIntegral() &&
585 "Tried to create an logical operation on a non-integral type!");
586 break;
587 case SetLT: case SetGT: case SetLE:
588 case SetGE: case SetEQ: case SetNE:
589 assert(getType() == Type::BoolTy && "Setcc must return bool!");
590 default:
591 break;
592 }
593#endif
594}
595
596BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
597 const std::string &Name,
598 Instruction *InsertBefore) {
599 assert(S1->getType() == S2->getType() &&
600 "Cannot create binary operator with two operands of differing type!");
601 switch (Op) {
602 // Binary comparison operators...
603 case SetLT: case SetGT: case SetLE:
604 case SetGE: case SetEQ: case SetNE:
605 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
606
607 default:
608 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
609 }
610}
611
612BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
613 const std::string &Name,
614 BasicBlock *InsertAtEnd) {
615 BinaryOperator *Res = create(Op, S1, S2, Name);
616 InsertAtEnd->getInstList().push_back(Res);
617 return Res;
618}
619
620BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
621 Instruction *InsertBefore) {
622 if (!Op->getType()->isFloatingPoint())
623 return new BinaryOperator(Instruction::Sub,
624 Constant::getNullValue(Op->getType()), Op,
625 Op->getType(), Name, InsertBefore);
626 else
627 return new BinaryOperator(Instruction::Sub,
628 ConstantFP::get(Op->getType(), -0.0), Op,
629 Op->getType(), Name, InsertBefore);
630}
631
632BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
633 BasicBlock *InsertAtEnd) {
634 if (!Op->getType()->isFloatingPoint())
635 return new BinaryOperator(Instruction::Sub,
636 Constant::getNullValue(Op->getType()), Op,
637 Op->getType(), Name, InsertAtEnd);
638 else
639 return new BinaryOperator(Instruction::Sub,
640 ConstantFP::get(Op->getType(), -0.0), Op,
641 Op->getType(), Name, InsertAtEnd);
642}
643
644BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
645 Instruction *InsertBefore) {
646 return new BinaryOperator(Instruction::Xor, Op,
647 ConstantIntegral::getAllOnesValue(Op->getType()),
648 Op->getType(), Name, InsertBefore);
649}
650
651BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
652 BasicBlock *InsertAtEnd) {
653 return new BinaryOperator(Instruction::Xor, Op,
654 ConstantIntegral::getAllOnesValue(Op->getType()),
655 Op->getType(), Name, InsertAtEnd);
656}
657
658
659// isConstantAllOnes - Helper function for several functions below
660static inline bool isConstantAllOnes(const Value *V) {
661 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
662}
663
664bool BinaryOperator::isNeg(const Value *V) {
665 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
666 if (Bop->getOpcode() == Instruction::Sub)
667 if (!V->getType()->isFloatingPoint())
668 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
669 else
670 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
671 return false;
672}
673
674bool BinaryOperator::isNot(const Value *V) {
675 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
676 return (Bop->getOpcode() == Instruction::Xor &&
677 (isConstantAllOnes(Bop->getOperand(1)) ||
678 isConstantAllOnes(Bop->getOperand(0))));
679 return false;
680}
681
682Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
683 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
684 return Bop->getOperand(1);
685}
686
687const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
688 return getNegArgument((BinaryOperator*)Bop);
689}
690
691Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
692 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
693 Value *Op0 = Bop->getOperand(0);
694 Value *Op1 = Bop->getOperand(1);
695 if (isConstantAllOnes(Op0)) return Op1;
696
697 assert(isConstantAllOnes(Op1));
698 return Op0;
699}
700
701const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
702 return getNotArgument((BinaryOperator*)Bop);
703}
704
705
706// swapOperands - Exchange the two operands to this instruction. This
707// instruction is safe to use on any binary instruction and does not
708// modify the semantics of the instruction. If the instruction is
709// order dependent (SetLT f.e.) the opcode is changed.
710//
711bool BinaryOperator::swapOperands() {
712 if (isCommutative())
713 ; // If the instruction is commutative, it is safe to swap the operands
714 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
715 /// FIXME: SetCC instructions shouldn't all have different opcodes.
716 setOpcode(SCI->getSwappedCondition());
717 else
718 return true; // Can't commute operands
719
720 std::swap(Operands[0], Operands[1]);
721 return false;
722}
723
724
725//===----------------------------------------------------------------------===//
726// SetCondInst Class
727//===----------------------------------------------------------------------===//
728
729SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
730 const std::string &Name, Instruction *InsertBefore)
731 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
732
733 // Make sure it's a valid type... getInverseCondition will assert out if not.
734 assert(getInverseCondition(Opcode));
735}
736
737SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
738 const std::string &Name, BasicBlock *InsertAtEnd)
739 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
740
741 // Make sure it's a valid type... getInverseCondition will assert out if not.
742 assert(getInverseCondition(Opcode));
743}
744
745// getInverseCondition - Return the inverse of the current condition opcode.
746// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
747//
748Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
749 switch (Opcode) {
750 default:
751 assert(0 && "Unknown setcc opcode!");
752 case SetEQ: return SetNE;
753 case SetNE: return SetEQ;
754 case SetGT: return SetLE;
755 case SetLT: return SetGE;
756 case SetGE: return SetLT;
757 case SetLE: return SetGT;
758 }
759}
760
761// getSwappedCondition - Return the condition opcode that would be the result
762// of exchanging the two operands of the setcc instruction without changing
763// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
764//
765Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
766 switch (Opcode) {
767 default: assert(0 && "Unknown setcc instruction!");
768 case SetEQ: case SetNE: return Opcode;
769 case SetGT: return SetLT;
770 case SetLT: return SetGT;
771 case SetGE: return SetLE;
772 case SetLE: return SetGE;
773 }
774}
775
776//===----------------------------------------------------------------------===//
777// SwitchInst Implementation
778//===----------------------------------------------------------------------===//
779
780void SwitchInst::init(Value *Value, BasicBlock *Default)
781{
782 assert(Value && Default);
783 Operands.push_back(Use(Value, this));
784 Operands.push_back(Use(Default, this));
785}
786
787SwitchInst::SwitchInst(const SwitchInst &SI)
788 : TerminatorInst(Instruction::Switch) {
789 Operands.reserve(SI.Operands.size());
790
791 for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
792 Operands.push_back(Use(SI.Operands[i], this));
793 Operands.push_back(Use(SI.Operands[i+1], this));
794 }
795}
796
797/// addCase - Add an entry to the switch instruction...
798///
799void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
800 Operands.push_back(Use((Value*)OnVal, this));
801 Operands.push_back(Use((Value*)Dest, this));
802}
803
804/// removeCase - This method removes the specified successor from the switch
805/// instruction. Note that this cannot be used to remove the default
806/// destination (successor #0).
807///
808void SwitchInst::removeCase(unsigned idx) {
809 assert(idx != 0 && "Cannot remove the default case!");
810 assert(idx*2 < Operands.size() && "Successor index out of range!!!");
811 Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);
812}
Chris Lattnerf22be932004-10-15 23:52:53 +0000813
814
815// Define these methods here so vtables don't get emitted into every translation
816// unit that uses these classes.
817
818GetElementPtrInst *GetElementPtrInst::clone() const {
819 return new GetElementPtrInst(*this);
820}
821
822BinaryOperator *BinaryOperator::clone() const {
823 return create(getOpcode(), Operands[0], Operands[1]);
824}
825
826MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
827AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
828FreeInst *FreeInst::clone() const { return new FreeInst(Operands[0]); }
829LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
830StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
831CastInst *CastInst::clone() const { return new CastInst(*this); }
832CallInst *CallInst::clone() const { return new CallInst(*this); }
833ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
834SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
835VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
836VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
837PHINode *PHINode::clone() const { return new PHINode(*this); }
838ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
839BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
840SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
841InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
842UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000843UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}