Implement == and != correctly. Before they would incorrectly return !=
[oota-llvm.git] / lib / VMCore / iOperators.cpp
1 //===-- iOperators.cpp - Implement binary Operators ------------*- C++ -*--===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the nontrivial binary operator instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/iOperators.h"
15 #include "llvm/Type.h"
16 #include "llvm/Constants.h"
17 #include "llvm/BasicBlock.h"
18
19 namespace llvm {
20
21 //===----------------------------------------------------------------------===//
22 //                             BinaryOperator Class
23 //===----------------------------------------------------------------------===//
24
25 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 
26                                const Type *Ty, const std::string &Name,
27                                Instruction *InsertBefore)
28   : Instruction(Ty, iType, Name, InsertBefore) {
29
30   Operands.reserve(2);
31   Operands.push_back(Use(S1, this));
32   Operands.push_back(Use(S2, this));
33   assert(S1 && S2 && S1->getType() == S2->getType());
34
35 #ifndef NDEBUG
36   switch (iType) {
37   case Add: case Sub:
38   case Mul: case Div:
39   case Rem:
40     assert(Ty == S1->getType() &&
41            "Arithmetic operation should return same type as operands!");
42     assert((Ty->isInteger() || Ty->isFloatingPoint()) && 
43            "Tried to create an arithmetic operation on a non-arithmetic type!");
44     break;
45   case And: case Or:
46   case Xor:
47     assert(Ty == S1->getType() &&
48            "Logical operation should return same type as operands!");
49     assert(Ty->isIntegral() &&
50            "Tried to create an logical operation on a non-integral type!");
51     break;
52   case SetLT: case SetGT: case SetLE:
53   case SetGE: case SetEQ: case SetNE:
54     assert(Ty == Type::BoolTy && "Setcc must return bool!");
55   default:
56     break;
57   }
58 #endif
59 }
60
61
62
63
64 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
65                                        const std::string &Name,
66                                        Instruction *InsertBefore) {
67   assert(S1->getType() == S2->getType() &&
68          "Cannot create binary operator with two operands of differing type!");
69   switch (Op) {
70   // Binary comparison operators...
71   case SetLT: case SetGT: case SetLE:
72   case SetGE: case SetEQ: case SetNE:
73     return new SetCondInst(Op, S1, S2, Name, InsertBefore);
74
75   default:
76     return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
77   }
78 }
79
80 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
81                                           Instruction *InsertBefore) {
82   return new BinaryOperator(Instruction::Sub,
83                             Constant::getNullValue(Op->getType()), Op,
84                             Op->getType(), Name, InsertBefore);
85 }
86
87 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
88                                           Instruction *InsertBefore) {
89   return new BinaryOperator(Instruction::Xor, Op,
90                             ConstantIntegral::getAllOnesValue(Op->getType()),
91                             Op->getType(), Name, InsertBefore);
92 }
93
94
95 // isConstantAllOnes - Helper function for several functions below
96 static inline bool isConstantAllOnes(const Value *V) {
97   return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
98 }
99
100 bool BinaryOperator::isNeg(const Value *V) {
101   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
102     return Bop->getOpcode() == Instruction::Sub &&
103       Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
104   return false;
105 }
106
107 bool BinaryOperator::isNot(const Value *V) {
108   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
109     return (Bop->getOpcode() == Instruction::Xor &&
110             (isConstantAllOnes(Bop->getOperand(1)) ||
111              isConstantAllOnes(Bop->getOperand(0))));
112   return false;
113 }
114
115 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
116   assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
117   return Bop->getOperand(1);
118 }
119
120 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
121   return getNegArgument((BinaryOperator*)Bop);
122 }
123
124 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
125   assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
126   Value *Op0 = Bop->getOperand(0);
127   Value *Op1 = Bop->getOperand(1);
128   if (isConstantAllOnes(Op0)) return Op1;
129
130   assert(isConstantAllOnes(Op1));
131   return Op0;
132 }
133
134 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
135   return getNotArgument((BinaryOperator*)Bop);
136 }
137
138
139 // swapOperands - Exchange the two operands to this instruction.  This
140 // instruction is safe to use on any binary instruction and does not
141 // modify the semantics of the instruction.  If the instruction is
142 // order dependent (SetLT f.e.) the opcode is changed.
143 //
144 bool BinaryOperator::swapOperands() {
145   if (isCommutative())
146     ;  // If the instruction is commutative, it is safe to swap the operands
147   else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
148     iType = SCI->getSwappedCondition();
149   else
150     return true;   // Can't commute operands
151
152   std::swap(Operands[0], Operands[1]);
153   return false;
154 }
155
156
157 //===----------------------------------------------------------------------===//
158 //                             SetCondInst Class
159 //===----------------------------------------------------------------------===//
160
161 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, 
162                          const std::string &Name, Instruction *InsertBefore)
163   : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
164
165   // Make sure it's a valid type... getInverseCondition will assert out if not.
166   assert(getInverseCondition(Opcode));
167 }
168
169 // getInverseCondition - Return the inverse of the current condition opcode.
170 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
171 //
172 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
173   switch (Opcode) {
174   default:
175     assert(0 && "Unknown setcc opcode!");
176   case SetEQ: return SetNE;
177   case SetNE: return SetEQ;
178   case SetGT: return SetLE;
179   case SetLT: return SetGE;
180   case SetGE: return SetLT;
181   case SetLE: return SetGT;
182   }
183 }
184
185 // getSwappedCondition - Return the condition opcode that would be the result
186 // of exchanging the two operands of the setcc instruction without changing
187 // the result produced.  Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
188 //
189 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
190   switch (Opcode) {
191   default: assert(0 && "Unknown setcc instruction!");
192   case SetEQ: case SetNE: return Opcode;
193   case SetGT: return SetLT;
194   case SetLT: return SetGT;
195   case SetGE: return SetLE;
196   case SetLE: return SetGE;
197   }
198 }
199
200 } // End llvm namespace