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