1 //===-- iOperators.cpp - Implement binary Operators ------------*- C++ -*--===//
3 // This file implements the nontrivial binary operator instructions.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/iOperators.h"
9 #include "llvm/Constants.h"
11 //===----------------------------------------------------------------------===//
12 // BinaryOperator Class
13 //===----------------------------------------------------------------------===//
15 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
16 const std::string &Name) {
18 // Binary comparison operators...
19 case SetLT: case SetGT: case SetLE:
20 case SetGE: case SetEQ: case SetNE:
21 return new SetCondInst(Op, S1, S2, Name);
24 return new GenericBinaryInst(Op, S1, S2, Name);
28 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name) {
29 return new GenericBinaryInst(Instruction::Sub,
30 Constant::getNullValue(Op->getType()), Op, Name);
33 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name) {
34 return new GenericBinaryInst(Instruction::Xor, Op,
35 ConstantIntegral::getAllOnesValue(Op->getType()),
40 // isConstantAllOnes - Helper function for several functions below
41 static inline bool isConstantAllOnes(const Value *V) {
42 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
45 bool BinaryOperator::isNeg(const Value *V) {
46 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
47 return Bop->getOpcode() == Instruction::Sub &&
48 isa<Constant>(Bop->getOperand(0)) && cast<Constant>(V)->isNullValue();
52 bool BinaryOperator::isNot(const Value *V) {
53 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
54 return (Bop->getOpcode() == Instruction::Xor &&
55 (isConstantAllOnes(Bop->getOperand(1)) ||
56 isConstantAllOnes(Bop->getOperand(0))));
60 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
61 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
62 return Bop->getOperand(1);
65 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
66 return getNegArgument((BinaryOperator*)Bop);
69 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
70 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
71 Value *Op0 = Bop->getOperand(0);
72 Value *Op1 = Bop->getOperand(1);
73 if (isConstantAllOnes(Op0)) return Op1;
75 assert(isConstantAllOnes(Op1));
79 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
80 return getNotArgument((BinaryOperator*)Bop);
84 // swapOperands - Exchange the two operands to this instruction. This
85 // instruction is safe to use on any binary instruction and does not
86 // modify the semantics of the instruction. If the instruction is
87 // order dependant (SetLT f.e.) the opcode is changed.
89 bool BinaryOperator::swapOperands() {
90 if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) {
91 iType = SCI->getSwappedCondition();
92 std::swap(Operands[0], Operands[1]);
96 switch (getOpcode()) {
97 // Instructions that don't need opcode modification
101 // Error on the side of caution
105 std::swap(Operands[0], Operands[1]);
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
114 SetCondInst::SetCondInst(BinaryOps opType, Value *S1, Value *S2,
115 const std::string &Name)
116 : BinaryOperator(opType, S1, S2, Name) {
119 setType(Type::BoolTy); // setcc instructions always return bool type.
121 // Make sure it's a valid type...
122 assert(getOpcodeName() != 0);
125 // getInverseCondition - Return the inverse of the current condition opcode.
126 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
128 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
131 assert(0 && "Unknown setcc opcode!");
132 case SetEQ: return SetNE;
133 case SetNE: return SetEQ;
134 case SetGT: return SetLE;
135 case SetLT: return SetGE;
136 case SetGE: return SetLT;
137 case SetLE: return SetGT;
141 // getSwappedCondition - Return the condition opcode that would be the result
142 // of exchanging the two operands of the setcc instruction without changing
143 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
145 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
147 default: assert(0 && "Unknown setcc instruction!");
148 case SetEQ: case SetNE: return Opcode;
149 case SetGT: return SetLT;
150 case SetLT: return SetGT;
151 case SetGE: return SetLE;
152 case SetLE: return SetGE;