Also update UnaryOperator to support isa, cast, and dyn_cast.
Differential Revision: https://reviews.llvm.org/D62417
llvm-svn: 361816
return Fold(ConstantExpr::getNot(C));
}
+ Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
+ return Fold(ConstantExpr::get(Opc, C));
+ }
+
//===--------------------------------------------------------------------===//
// Memory Instructions
//===--------------------------------------------------------------------===//
return ConstantExpr::getNot(C);
}
+ Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
+ return ConstantExpr::get(Opc, C);
+ }
+
//===--------------------------------------------------------------------===//
// Memory Instructions
//===--------------------------------------------------------------------===//
return Insert(BinaryOperator::CreateNot(V), Name);
}
+ Value *CreateUnOp(Instruction::UnaryOps Opc,
+ Value *V, const Twine &Name = "",
+ MDNode *FPMathTag = nullptr) {
+ if (auto *VC = dyn_cast<Constant>(V))
+ return Insert(Folder.CreateUnOp(Opc, VC), Name);
+ Instruction *UnOp = UnaryOperator::Create(Opc, V);
+ if (isa<FPMathOperator>(UnOp))
+ UnOp = setFPAttrs(UnOp, FPMathTag, FMF);
+ return Insert(UnOp, Name);
+ }
+
//===--------------------------------------------------------------------===//
// Instruction creation methods: Memory Instructions
//===--------------------------------------------------------------------===//
// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Instruction *I) {
- return I->getOpcode() == Instruction::Alloca ||
+ return I->isUnaryOp() ||
+ I->getOpcode() == Instruction::Alloca ||
I->getOpcode() == Instruction::Load ||
I->getOpcode() == Instruction::VAArg ||
I->getOpcode() == Instruction::ExtractValue ||
UnaryOps getOpcode() const {
return static_cast<UnaryOps>(Instruction::getOpcode());
}
+
+ // Methods for support type inquiry through isa, cast, and dyn_cast:
+ static bool classof(const Instruction *I) {
+ return I->isUnaryOp();
+ }
+ static bool classof(const Value *V) {
+ return isa<Instruction>(V) && classof(cast<Instruction>(V));
+ }
};
//===----------------------------------------------------------------------===//
return BinaryOperator::CreateNot(C);
}
+ Instruction *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
+ return UnaryOperator::Create(Opc, C);
+ }
+
//===--------------------------------------------------------------------===//
// Memory Instructions
//===--------------------------------------------------------------------===//
delete DL;
}
+TEST_F(IRBuilderTest, UnaryOperators) {
+ IRBuilder<NoFolder> Builder(BB);
+ Value *V = Builder.CreateLoad(GV->getValueType(), GV);
+
+ // Test CreateUnOp
+ Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
+ ASSERT_TRUE(isa<Instruction>(U));
+ ASSERT_TRUE(isa<FPMathOperator>(U));
+ ASSERT_TRUE(isa<UnaryOperator>(U));
+ ASSERT_FALSE(isa<BinaryOperator>(U));
+}
+
TEST_F(IRBuilderTest, FastMathFlags) {
IRBuilder<> Builder(BB);
Value *F, *FC;