* Make the ctor take a TargetData even though it's not using it yet
[oota-llvm.git] / lib / VMCore / iOperators.cpp
1 //===-- iOperators.cpp - Implement the Binary & Unary Operators --*- C++ -*--=//
2 //
3 // This file implements the nontrivial binary & unary operator instructions.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iOperators.h"
8 #include "llvm/Type.h"
9 #include <iostream>
10 using std::cerr;
11
12 //===----------------------------------------------------------------------===//
13 //                              UnaryOperator Class
14 //===----------------------------------------------------------------------===//
15
16 UnaryOperator *UnaryOperator::create(UnaryOps Op, Value *Source) {
17   switch (Op) {
18   case Not:  return new GenericUnaryInst(Op, Source);
19   default:
20     cerr << "Don't know how to Create UnaryOperator " << Op << "\n";
21     return 0;
22   }
23 }
24
25
26 //===----------------------------------------------------------------------===//
27 //                           GenericUnaryOperator Class
28 //===----------------------------------------------------------------------===//
29
30
31 //===----------------------------------------------------------------------===//
32 //                             BinaryOperator Class
33 //===----------------------------------------------------------------------===//
34
35 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
36                                        const std::string &Name) {
37   switch (Op) {
38   // Binary comparison operators...
39   case SetLT: case SetGT: case SetLE:
40   case SetGE: case SetEQ: case SetNE:
41     return new SetCondInst(Op, S1, S2, Name);
42
43   default:
44     return new GenericBinaryInst(Op, S1, S2, Name);
45   }
46 }
47
48 // swapOperands - Exchange the two operands to this instruction.  This
49 // instruction is safe to use on any binary instruction and does not
50 // modify the semantics of the instruction.  If the instruction is
51 // order dependant (SetLT f.e.) the opcode is changed.
52 //
53 bool BinaryOperator::swapOperands() {
54   switch (getOpcode()) {
55     // Instructions that don't need opcode modification
56   case Add: case Mul:
57   case And: case Xor:
58   case Or:
59   case SetEQ: case SetNE:
60     break;
61     // Instructions that need opcode modification
62   case SetGT: iType = SetLT; break;
63   case SetLT: iType = SetGT; break;
64   case SetGE: iType = SetLE; break;
65   case SetLE: iType = SetGE; break;
66     // Error on the side of caution
67   default:
68     return true;
69   }
70   std::swap(Operands[0], Operands[1]);
71   return false;
72 }
73
74
75 //===----------------------------------------------------------------------===//
76 //                            GenericBinaryInst Class
77 //===----------------------------------------------------------------------===//
78
79
80 //===----------------------------------------------------------------------===//
81 //                             SetCondInst Class
82 //===----------------------------------------------------------------------===//
83
84 SetCondInst::SetCondInst(BinaryOps opType, Value *S1, Value *S2, 
85                          const std::string &Name) 
86   : BinaryOperator(opType, S1, S2, Name) {
87
88   OpType = opType;
89   setType(Type::BoolTy);   // setcc instructions always return bool type.
90
91   // Make sure it's a valid type...
92   assert(getOpcodeName() != 0);
93 }