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"
19 //===----------------------------------------------------------------------===//
20 // BinaryOperator Class
21 //===----------------------------------------------------------------------===//
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) {
29 Operands.push_back(Use(S1, this));
30 Operands.push_back(Use(S2, this));
31 assert(S1 && S2 && S1->getType() == S2->getType());
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!");
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!");
50 case SetLT: case SetGT: case SetLE:
51 case SetGE: case SetEQ: case SetNE:
52 assert(Ty == Type::BoolTy && "Setcc must return bool!");
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!");
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);
74 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
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);
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);
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();
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());
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))));
113 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
114 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
115 return Bop->getOperand(1);
118 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
119 return getNegArgument((BinaryOperator*)Bop);
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;
128 assert(isConstantAllOnes(Op1));
132 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
133 return getNotArgument((BinaryOperator*)Bop);
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.
142 bool BinaryOperator::swapOperands() {
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();
148 return true; // Can't commute operands
150 std::swap(Operands[0], Operands[1]);
155 //===----------------------------------------------------------------------===//
157 //===----------------------------------------------------------------------===//
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) {
163 // Make sure it's a valid type... getInverseCondition will assert out if not.
164 assert(getInverseCondition(Opcode));
167 // getInverseCondition - Return the inverse of the current condition opcode.
168 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
170 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
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;
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.
187 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps 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;