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 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) {
30 Operands.push_back(Use(S1, this));
31 Operands.push_back(Use(S2, this));
32 assert(S1 && S2 && S1->getType() == S2->getType());
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!");
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!");
51 case SetLT: case SetGT: case SetLE:
52 case SetGE: case SetEQ: case SetNE:
53 assert(Ty == Type::BoolTy && "Setcc must return bool!");
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!");
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);
75 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
79 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
80 Instruction *InsertBefore) {
81 return new BinaryOperator(Instruction::Sub,
82 Constant::getNullValue(Op->getType()), Op,
83 Op->getType(), Name, InsertBefore);
86 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
87 Instruction *InsertBefore) {
88 return new BinaryOperator(Instruction::Xor, Op,
89 ConstantIntegral::getAllOnesValue(Op->getType()),
90 Op->getType(), Name, InsertBefore);
94 // isConstantAllOnes - Helper function for several functions below
95 static inline bool isConstantAllOnes(const Value *V) {
96 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
99 bool BinaryOperator::isNeg(const Value *V) {
100 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
101 return Bop->getOpcode() == Instruction::Sub &&
102 Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
106 bool BinaryOperator::isNot(const Value *V) {
107 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
108 return (Bop->getOpcode() == Instruction::Xor &&
109 (isConstantAllOnes(Bop->getOperand(1)) ||
110 isConstantAllOnes(Bop->getOperand(0))));
114 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
115 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
116 return Bop->getOperand(1);
119 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
120 return getNegArgument((BinaryOperator*)Bop);
123 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
124 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
125 Value *Op0 = Bop->getOperand(0);
126 Value *Op1 = Bop->getOperand(1);
127 if (isConstantAllOnes(Op0)) return Op1;
129 assert(isConstantAllOnes(Op1));
133 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
134 return getNotArgument((BinaryOperator*)Bop);
138 // swapOperands - Exchange the two operands to this instruction. This
139 // instruction is safe to use on any binary instruction and does not
140 // modify the semantics of the instruction. If the instruction is
141 // order dependent (SetLT f.e.) the opcode is changed.
143 bool BinaryOperator::swapOperands() {
145 ; // If the instruction is commutative, it is safe to swap the operands
146 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
147 iType = SCI->getSwappedCondition();
149 return true; // Can't commute operands
151 std::swap(Operands[0], Operands[1]);
156 //===----------------------------------------------------------------------===//
158 //===----------------------------------------------------------------------===//
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) {
164 // Make sure it's a valid type... getInverseCondition will assert out if not.
165 assert(getInverseCondition(Opcode));
168 // getInverseCondition - Return the inverse of the current condition opcode.
169 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
171 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
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;
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.
188 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps 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;