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"
21 //===----------------------------------------------------------------------===//
22 // BinaryOperator Class
23 //===----------------------------------------------------------------------===//
25 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
26 const Type *Ty, const std::string &Name,
27 Instruction *InsertBefore)
28 : Instruction(Ty, iType, Name, InsertBefore) {
31 Operands.push_back(Use(S1, this));
32 Operands.push_back(Use(S2, this));
33 assert(S1 && S2 && S1->getType() == S2->getType());
40 assert(Ty == S1->getType() &&
41 "Arithmetic operation should return same type as operands!");
42 assert((Ty->isInteger() || Ty->isFloatingPoint()) &&
43 "Tried to create an arithmetic operation on a non-arithmetic type!");
47 assert(Ty == S1->getType() &&
48 "Logical operation should return same type as operands!");
49 assert(Ty->isIntegral() &&
50 "Tried to create an logical operation on a non-integral type!");
52 case SetLT: case SetGT: case SetLE:
53 case SetGE: case SetEQ: case SetNE:
54 assert(Ty == Type::BoolTy && "Setcc must return bool!");
64 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
65 const std::string &Name,
66 Instruction *InsertBefore) {
67 assert(S1->getType() == S2->getType() &&
68 "Cannot create binary operator with two operands of differing type!");
70 // Binary comparison operators...
71 case SetLT: case SetGT: case SetLE:
72 case SetGE: case SetEQ: case SetNE:
73 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
76 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
80 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
81 Instruction *InsertBefore) {
82 return new BinaryOperator(Instruction::Sub,
83 Constant::getNullValue(Op->getType()), Op,
84 Op->getType(), Name, InsertBefore);
87 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
88 Instruction *InsertBefore) {
89 return new BinaryOperator(Instruction::Xor, Op,
90 ConstantIntegral::getAllOnesValue(Op->getType()),
91 Op->getType(), Name, InsertBefore);
95 // isConstantAllOnes - Helper function for several functions below
96 static inline bool isConstantAllOnes(const Value *V) {
97 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
100 bool BinaryOperator::isNeg(const Value *V) {
101 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
102 return Bop->getOpcode() == Instruction::Sub &&
103 Bop->getOperand(0) == Constant::getNullValue(Bop->getType());
107 bool BinaryOperator::isNot(const Value *V) {
108 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
109 return (Bop->getOpcode() == Instruction::Xor &&
110 (isConstantAllOnes(Bop->getOperand(1)) ||
111 isConstantAllOnes(Bop->getOperand(0))));
115 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
116 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
117 return Bop->getOperand(1);
120 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
121 return getNegArgument((BinaryOperator*)Bop);
124 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
125 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
126 Value *Op0 = Bop->getOperand(0);
127 Value *Op1 = Bop->getOperand(1);
128 if (isConstantAllOnes(Op0)) return Op1;
130 assert(isConstantAllOnes(Op1));
134 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
135 return getNotArgument((BinaryOperator*)Bop);
139 // swapOperands - Exchange the two operands to this instruction. This
140 // instruction is safe to use on any binary instruction and does not
141 // modify the semantics of the instruction. If the instruction is
142 // order dependent (SetLT f.e.) the opcode is changed.
144 bool BinaryOperator::swapOperands() {
146 ; // If the instruction is commutative, it is safe to swap the operands
147 else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this))
148 iType = SCI->getSwappedCondition();
150 return true; // Can't commute operands
152 std::swap(Operands[0], Operands[1]);
157 //===----------------------------------------------------------------------===//
159 //===----------------------------------------------------------------------===//
161 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
162 const std::string &Name, Instruction *InsertBefore)
163 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
165 // Make sure it's a valid type... getInverseCondition will assert out if not.
166 assert(getInverseCondition(Opcode));
169 // getInverseCondition - Return the inverse of the current condition opcode.
170 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
172 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
175 assert(0 && "Unknown setcc opcode!");
176 case SetEQ: return SetNE;
177 case SetNE: return SetEQ;
178 case SetGT: return SetLE;
179 case SetLT: return SetGE;
180 case SetGE: return SetLT;
181 case SetLE: return SetGT;
185 // getSwappedCondition - Return the condition opcode that would be the result
186 // of exchanging the two operands of the setcc instruction without changing
187 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
189 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
191 default: assert(0 && "Unknown setcc instruction!");
192 case SetEQ: case SetNE: return Opcode;
193 case SetGT: return SetLT;
194 case SetLT: return SetGT;
195 case SetGE: return SetLE;
196 case SetLE: return SetGE;
200 } // End llvm namespace