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