Minor changes to allow Modules (which are no longer Values) to work
[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 const char *GenericUnaryInst::getOpcodeName() const {
31   switch (getOpcode()) {
32   case Not: return "not";
33   case Cast: return "cast";
34   default:
35     cerr << "Invalid unary operator type!" << getOpcode() << "\n";
36     abort();
37   }
38   return 0;
39 }
40
41
42 //===----------------------------------------------------------------------===//
43 //                             BinaryOperator Class
44 //===----------------------------------------------------------------------===//
45
46 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
47                                        const std::string &Name) {
48   switch (Op) {
49   // Binary comparison operators...
50   case SetLT: case SetGT: case SetLE:
51   case SetGE: case SetEQ: case SetNE:
52     return new SetCondInst(Op, S1, S2, Name);
53
54   default:
55     return new GenericBinaryInst(Op, S1, S2, Name);
56   }
57 }
58
59 // swapOperands - Exchange the two operands to this instruction.  This
60 // instruction is safe to use on any binary instruction and does not
61 // modify the semantics of the instruction.  If the instruction is
62 // order dependant (SetLT f.e.) the opcode is changed.
63 //
64 bool BinaryOperator::swapOperands() {
65   switch (getOpcode()) {
66     // Instructions that don't need opcode modification
67   case Add: case Mul:
68   case And: case Xor:
69   case Or:
70   case SetEQ: case SetNE:
71     break;
72     // Instructions that need opcode modification
73   case SetGT: iType = SetLT; break;
74   case SetLT: iType = SetGT; break;
75   case SetGE: iType = SetLE; break;
76   case SetLE: iType = SetGE; break;
77     // Error on the side of caution
78   default:
79     return true;
80   }
81   std::swap(Operands[0], Operands[1]);
82   return false;
83 }
84
85
86 //===----------------------------------------------------------------------===//
87 //                            GenericBinaryInst Class
88 //===----------------------------------------------------------------------===//
89
90 const char *GenericBinaryInst::getOpcodeName() const {
91   switch (getOpcode()) {
92   // Standard binary operators...
93   case Add: return "add";
94   case Sub: return "sub";
95   case Mul: return "mul";
96   case Div: return "div";
97   case Rem: return "rem";
98
99   // Logical operators...
100   case And: return "and";
101   case Or : return "or";
102   case Xor: return "xor";
103   default:
104     cerr << "Invalid binary operator type!" << getOpcode() << "\n";
105     abort();
106   }
107   return 0;
108 }
109
110
111 //===----------------------------------------------------------------------===//
112 //                             SetCondInst Class
113 //===----------------------------------------------------------------------===//
114
115 SetCondInst::SetCondInst(BinaryOps opType, Value *S1, Value *S2, 
116                          const std::string &Name) 
117   : BinaryOperator(opType, S1, S2, Name) {
118
119   OpType = opType;
120   setType(Type::BoolTy);   // setcc instructions always return bool type.
121
122   // Make sure it's a valid type...
123   assert(getOpcodeName() != 0);
124 }
125
126 const char *SetCondInst::getOpcodeName() const {
127   switch (OpType) {
128   case SetLE:  return "setle";
129   case SetGE:  return "setge";
130   case SetLT:  return "setlt";
131   case SetGT:  return "setgt";
132   case SetEQ:  return "seteq";
133   case SetNE:  return "setne";
134   default:
135     assert(0 && "Invalid opcode type to SetCondInst class!");
136     return "invalid opcode type to SetCondInst";
137   }
138 }