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"
10 #include "llvm/BasicBlock.h"
12 //===----------------------------------------------------------------------===//
13 // BinaryOperator Class
14 //===----------------------------------------------------------------------===//
16 BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
17 const Type *Ty, const std::string &Name,
18 Instruction *InsertBefore)
19 : Instruction(Ty, iType, Name, InsertBefore) {
21 Operands.push_back(Use(S1, this));
22 Operands.push_back(Use(S2, this));
23 assert(Operands[0] && Operands[1] &&
24 Operands[0]->getType() == Operands[1]->getType());
30 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
31 const std::string &Name,
32 Instruction *InsertBefore) {
33 assert(S1->getType() == S2->getType() &&
34 "Cannot create binary operator with two operands of differing type!");
36 // Binary comparison operators...
37 case SetLT: case SetGT: case SetLE:
38 case SetGE: case SetEQ: case SetNE:
39 return new SetCondInst(Op, S1, S2, Name, InsertBefore);
42 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
46 BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
47 Instruction *InsertBefore) {
48 return new BinaryOperator(Instruction::Sub,
49 Constant::getNullValue(Op->getType()), Op,
50 Op->getType(), Name, InsertBefore);
53 BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
54 Instruction *InsertBefore) {
55 return new BinaryOperator(Instruction::Xor, Op,
56 ConstantIntegral::getAllOnesValue(Op->getType()),
57 Op->getType(), Name, InsertBefore);
61 // isConstantAllOnes - Helper function for several functions below
62 static inline bool isConstantAllOnes(const Value *V) {
63 return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue();
66 bool BinaryOperator::isNeg(const Value *V) {
67 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
68 return Bop->getOpcode() == Instruction::Sub &&
69 isa<Constant>(Bop->getOperand(0)) && cast<Constant>(V)->isNullValue();
73 bool BinaryOperator::isNot(const Value *V) {
74 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
75 return (Bop->getOpcode() == Instruction::Xor &&
76 (isConstantAllOnes(Bop->getOperand(1)) ||
77 isConstantAllOnes(Bop->getOperand(0))));
81 Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
82 assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
83 return Bop->getOperand(1);
86 const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
87 return getNegArgument((BinaryOperator*)Bop);
90 Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
91 assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
92 Value *Op0 = Bop->getOperand(0);
93 Value *Op1 = Bop->getOperand(1);
94 if (isConstantAllOnes(Op0)) return Op1;
96 assert(isConstantAllOnes(Op1));
100 const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
101 return getNotArgument((BinaryOperator*)Bop);
105 // swapOperands - Exchange the two operands to this instruction. This
106 // instruction is safe to use on any binary instruction and does not
107 // modify the semantics of the instruction. If the instruction is
108 // order dependant (SetLT f.e.) the opcode is changed.
110 bool BinaryOperator::swapOperands() {
111 if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) {
112 iType = SCI->getSwappedCondition();
113 std::swap(Operands[0], Operands[1]);
117 switch (getOpcode()) {
118 // Instructions that don't need opcode modification
122 // Error on the side of caution
126 std::swap(Operands[0], Operands[1]);
131 //===----------------------------------------------------------------------===//
133 //===----------------------------------------------------------------------===//
135 SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
136 const std::string &Name, Instruction *InsertBefore)
137 : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
139 // Make sure it's a valid type... getInverseCondition will assert out if not.
140 assert(getInverseCondition(Opcode));
143 // getInverseCondition - Return the inverse of the current condition opcode.
144 // For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
146 Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) {
149 assert(0 && "Unknown setcc opcode!");
150 case SetEQ: return SetNE;
151 case SetNE: return SetEQ;
152 case SetGT: return SetLE;
153 case SetLT: return SetGE;
154 case SetGE: return SetLT;
155 case SetLE: return SetGT;
159 // getSwappedCondition - Return the condition opcode that would be the result
160 // of exchanging the two operands of the setcc instruction without changing
161 // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
163 Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) {
165 default: assert(0 && "Unknown setcc instruction!");
166 case SetEQ: case SetNE: return Opcode;
167 case SetGT: return SetLT;
168 case SetLT: return SetGT;
169 case SetGE: return SetLE;
170 case SetLE: return SetGE;