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