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"
10 //===----------------------------------------------------------------------===//
11 // UnaryOperator Class
12 //===----------------------------------------------------------------------===//
14 UnaryOperator *UnaryOperator::create(UnaryOps Op, Value *Source) {
16 case Not: return new GenericUnaryInst(Op, Source);
18 cerr << "Don't know how to Create UnaryOperator " << Op << endl;
24 //===----------------------------------------------------------------------===//
25 // GenericUnaryOperator Class
26 //===----------------------------------------------------------------------===//
28 const char *GenericUnaryInst::getOpcodeName() const {
29 switch (getOpcode()) {
30 case Not: return "not";
31 case Cast: return "cast";
33 cerr << "Invalid unary operator type!" << getOpcode() << endl;
39 //===----------------------------------------------------------------------===//
40 // BinaryOperator Class
41 //===----------------------------------------------------------------------===//
43 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
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);
52 return new GenericBinaryInst(Op, S1, S2, Name);
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.
61 bool BinaryOperator::swapOperands() {
62 switch (getOpcode()) {
63 // Instructions that don't need opcode modification
67 case SetEQ: case SetNE:
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
78 swap(Operands[0], Operands[1]);
83 //===----------------------------------------------------------------------===//
84 // GenericBinaryInst Class
85 //===----------------------------------------------------------------------===//
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";
96 // Logical operators...
97 case And: return "and";
98 case Or : return "or";
99 case Xor: return "xor";
101 cerr << "Invalid binary operator type!" << getOpcode() << endl;
107 //===----------------------------------------------------------------------===//
109 //===----------------------------------------------------------------------===//
111 SetCondInst::SetCondInst(BinaryOps opType, Value *S1, Value *S2,
113 : BinaryOperator(opType, S1, S2, Name) {
116 setType(Type::BoolTy); // setcc instructions always return bool type.
118 // Make sure it's a valid type...
119 assert(getOpcodeName() != 0);
122 const char *SetCondInst::getOpcodeName() const {
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";
131 assert(0 && "Invalid opcode type to SetCondInst class!");
132 return "invalid opcode type to SetCondInst";