1 //===-- iOperators.cpp - Implement the Binary & Unary Operators --*- C++ -*--=//
3 // This file implements the nontrivial binary & unary operator instructions.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/iOperators.h"
12 //===----------------------------------------------------------------------===//
13 // UnaryOperator Class
14 //===----------------------------------------------------------------------===//
16 UnaryOperator *UnaryOperator::create(UnaryOps Op, Value *Source) {
18 case Not: return new GenericUnaryInst(Op, Source);
20 cerr << "Don't know how to Create UnaryOperator " << Op << "\n";
26 //===----------------------------------------------------------------------===//
27 // GenericUnaryOperator Class
28 //===----------------------------------------------------------------------===//
30 const char *GenericUnaryInst::getOpcodeName() const {
31 switch (getOpcode()) {
32 case Not: return "not";
33 case Cast: return "cast";
35 cerr << "Invalid unary operator type!" << getOpcode() << "\n";
42 //===----------------------------------------------------------------------===//
43 // BinaryOperator Class
44 //===----------------------------------------------------------------------===//
46 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
47 const std::string &Name) {
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);
55 return new GenericBinaryInst(Op, S1, S2, Name);
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.
64 bool BinaryOperator::swapOperands() {
65 switch (getOpcode()) {
66 // Instructions that don't need opcode modification
70 case SetEQ: case SetNE:
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
81 std::swap(Operands[0], Operands[1]);
86 //===----------------------------------------------------------------------===//
87 // GenericBinaryInst Class
88 //===----------------------------------------------------------------------===//
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";
99 // Logical operators...
100 case And: return "and";
101 case Or : return "or";
102 case Xor: return "xor";
104 cerr << "Invalid binary operator type!" << getOpcode() << "\n";
111 //===----------------------------------------------------------------------===//
113 //===----------------------------------------------------------------------===//
115 SetCondInst::SetCondInst(BinaryOps opType, Value *S1, Value *S2,
116 const std::string &Name)
117 : BinaryOperator(opType, S1, S2, Name) {
120 setType(Type::BoolTy); // setcc instructions always return bool type.
122 // Make sure it's a valid type...
123 assert(getOpcodeName() != 0);
126 const char *SetCondInst::getOpcodeName() const {
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";
135 assert(0 && "Invalid opcode type to SetCondInst class!");
136 return "invalid opcode type to SetCondInst";