Add new SetCondInst::getInverseCondition() method.
[oota-llvm.git] / lib / VMCore / iOperators.cpp
1 //===-- iOperators.cpp - Implement binary Operators ------------*- C++ -*--===//
2 //
3 // This file implements the nontrivial binary operator instructions.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iOperators.h"
8 #include "llvm/Type.h"
9 #include "llvm/Constants.h"
10
11 //===----------------------------------------------------------------------===//
12 //                             BinaryOperator Class
13 //===----------------------------------------------------------------------===//
14
15 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
16                                        const std::string &Name) {
17   switch (Op) {
18   // Binary comparison operators...
19   case SetLT: case SetGT: case SetLE:
20   case SetGE: case SetEQ: case SetNE:
21     return new SetCondInst(Op, S1, S2, Name);
22
23   default:
24     return new GenericBinaryInst(Op, S1, S2, Name);
25   }
26 }
27
28 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name) {
29   return new GenericBinaryInst(Instruction::Sub,
30                                Constant::getNullValue(Op->getType()), Op, Name);
31 }
32
33 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name) {
34   return new GenericBinaryInst(Instruction::Xor, Op,
35                                ConstantIntegral::getAllOnesValue(Op->getType()),
36                                Name);
37 }
38
39
40 // isConstantAllOnes - Helper function for several functions below
41 static inline bool isConstantAllOnes(const Value *V) {
42   return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
43 }
44
45 bool BinaryOperator::isNeg(const Value *V) {
46   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
47     return Bop->getOpcode() == Instruction::Sub &&
48       isa<Constant>(Bop->getOperand(0)) && cast<Constant>(V)->isNullValue();
49   return false;
50 }
51
52 bool BinaryOperator::isNot(const Value *V) {
53   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
54     return (Bop->getOpcode() == Instruction::Xor &&
55             (isConstantAllOnes(Bop->getOperand(1)) ||
56              isConstantAllOnes(Bop->getOperand(0))));
57   return false;
58 }
59
60 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
61   assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
62   return Bop->getOperand(1);
63 }
64
65 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
66   return getNegArgument((BinaryOperator*)Bop);
67 }
68
69 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
70   assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
71   Value *Op0 = Bop->getOperand(0);
72   Value *Op1 = Bop->getOperand(1);
73   if (isConstantAllOnes(Op0)) return Op1;
74
75   assert(isConstantAllOnes(Op1));
76   return Op0;
77 }
78
79 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
80   return getNotArgument((BinaryOperator*)Bop);
81 }
82
83
84 // swapOperands - Exchange the two operands to this instruction.  This
85 // instruction is safe to use on any binary instruction and does not
86 // modify the semantics of the instruction.  If the instruction is
87 // order dependant (SetLT f.e.) the opcode is changed.
88 //
89 bool BinaryOperator::swapOperands() {
90   switch (getOpcode()) {
91     // Instructions that don't need opcode modification
92   case Add: case Mul:
93   case And: case Xor:
94   case Or:
95   case SetEQ: case SetNE:
96     break;
97     // Instructions that need opcode modification
98   case SetGT: iType = SetLT; break;
99   case SetLT: iType = SetGT; break;
100   case SetGE: iType = SetLE; break;
101   case SetLE: iType = SetGE; break;
102     // Error on the side of caution
103   default:
104     return true;
105   }
106   std::swap(Operands[0], Operands[1]);
107   return false;
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 // getInverseCondition - Return the inverse of the current condition opcode.
127 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
128 //
129 Instruction::BinaryOps SetCondInst::getInverseCondition() const {
130   switch (getOpcode()) {
131   default:
132     assert(0 && "Unknown setcc opcode!");
133   case SetEQ: return SetNE;
134   case SetNE: return SetEQ;
135   case SetGT: return SetLE;
136   case SetLT: return SetGE;
137   case SetGE: return SetLT;
138   case SetLE: return SetGT;
139   }
140 }