1 //===-- iOperators.cpp - Implement binary Operators ------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the nontrivial binary operator instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/iOperators.h"
15 #include "llvm/Type.h"
16 #include "llvm/Constants.h"
17 #include "llvm/BasicBlock.h"
20 //===----------------------------------------------------------------------===//
21 // BinaryOperator Class
22 //===----------------------------------------------------------------------===//
24 void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2)
27 Operands.push_back(Use(S1, this));
28 Operands.push_back(Use(S2, this));
29 assert(S1 && S2 && S1->getType() == S2->getType());
36 assert(getType() == S1->getType() &&
37 "Arithmetic operation should return same type as operands!");
38 assert((getType()->isInteger() || getType()->isFloatingPoint()) &&
39 "Tried to create an arithmetic operation on a non-arithmetic type!");
43 assert(getType() == S1->getType() &&
44 "Logical operation should return same type as operands!");
45 assert(getType()->isIntegral() &&
46 "Tried to create an logical operation on a non-integral type!");
48 case SetLT: case SetGT: case SetLE:
49 case SetGE: case SetEQ: case SetNE:
50 assert(getType() == Type::BoolTy && "Setcc must return bool!");
57 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
58 const std::string &Name,
59 Instruction *InsertBefore) {
60 assert(S1->getType() == S2->getType() &&
61 "Cannot create binary operator with two operands of differing type!");
63 // Binary comparison operators...
64 case SetLT: case SetGT: case SetLE:
65 case SetGE: case SetEQ: case SetNE:
66 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
69 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
73 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
74 const std::string &Name,
75 BasicBlock *InsertAtEnd) {
76 BinaryOperator *Res = create(Op, S1, S2, Name);
77 InsertAtEnd->getInstList().push_back(Res);
81 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
82 Instruction *InsertBefore) {
83 if (!Op->getType()->isFloatingPoint())
84 return new BinaryOperator(Instruction::Sub,
85 Constant::getNullValue(Op->getType()), Op,
86 Op->getType(), Name, InsertBefore);
88 return new BinaryOperator(Instruction::Sub,
89 ConstantFP::get(Op->getType(), -0.0), Op,
90 Op->getType(), Name, InsertBefore);
93 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
94 BasicBlock *InsertAtEnd) {
95 if (!Op->getType()->isFloatingPoint())
96 return new BinaryOperator(Instruction::Sub,
97 Constant::getNullValue(Op->getType()), Op,
98 Op->getType(), Name, InsertAtEnd);
100 return new BinaryOperator(Instruction::Sub,
101 ConstantFP::get(Op->getType(), -0.0), Op,
102 Op->getType(), Name, InsertAtEnd);
105 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
106 Instruction *InsertBefore) {
107 return new BinaryOperator(Instruction::Xor, Op,
108 ConstantIntegral::getAllOnesValue(Op->getType()),
109 Op->getType(), Name, InsertBefore);
112 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
113 BasicBlock *InsertAtEnd) {
114 return new BinaryOperator(Instruction::Xor, Op,
115 ConstantIntegral::getAllOnesValue(Op->getType()),
116 Op->getType(), Name, InsertAtEnd);
120 // isConstantAllOnes - Helper function for several functions below
121 static inline bool isConstantAllOnes(const Value *V) {
122 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
125 bool BinaryOperator::isNeg(const Value *V) {
126 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
127 if (Bop->getOpcode() == Instruction::Sub)
128 if (!V->getType()->isFloatingPoint())
129 return Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
131 return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0);
135 bool BinaryOperator::isNot(const Value *V) {
136 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
137 return (Bop->getOpcode() == Instruction::Xor &&
138 (isConstantAllOnes(Bop->getOperand(1)) ||
139 isConstantAllOnes(Bop->getOperand(0))));
143 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
144 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
145 return Bop->getOperand(1);
148 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
149 return getNegArgument((BinaryOperator*)Bop);
152 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
153 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
154 Value *Op0 = Bop->getOperand(0);
155 Value *Op1 = Bop->getOperand(1);
156 if (isConstantAllOnes(Op0)) return Op1;
158 assert(isConstantAllOnes(Op1));
162 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
163 return getNotArgument((BinaryOperator*)Bop);
167 // swapOperands - Exchange the two operands to this instruction. This
168 // instruction is safe to use on any binary instruction and does not
169 // modify the semantics of the instruction. If the instruction is
170 // order dependent (SetLT f.e.) the opcode is changed.
172 bool BinaryOperator::swapOperands() {
174 ; // If the instruction is commutative, it is safe to swap the operands
175 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
176 iType = SCI->getSwappedCondition();
178 return true; // Can't commute operands
180 std::swap(Operands[0], Operands[1]);
185 //===----------------------------------------------------------------------===//
187 //===----------------------------------------------------------------------===//
189 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
190 const std::string &Name, Instruction *InsertBefore)
191 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
193 // Make sure it's a valid type... getInverseCondition will assert out if not.
194 assert(getInverseCondition(Opcode));
197 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
198 const std::string &Name, BasicBlock *InsertAtEnd)
199 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
201 // Make sure it's a valid type... getInverseCondition will assert out if not.
202 assert(getInverseCondition(Opcode));
205 // getInverseCondition - Return the inverse of the current condition opcode.
206 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
208 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
211 assert(0 && "Unknown setcc opcode!");
212 case SetEQ: return SetNE;
213 case SetNE: return SetEQ;
214 case SetGT: return SetLE;
215 case SetLT: return SetGE;
216 case SetGE: return SetLT;
217 case SetLE: return SetGT;
221 // getSwappedCondition - Return the condition opcode that would be the result
222 // of exchanging the two operands of the setcc instruction without changing
223 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
225 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
227 default: assert(0 && "Unknown setcc instruction!");
228 case SetEQ: case SetNE: return Opcode;
229 case SetGT: return SetLT;
230 case SetLT: return SetGT;
231 case SetGE: return SetLE;
232 case SetLE: return SetGE;