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