llvm.org GIT mirror llvm / d492be2
[IRBuilder] Add CreateUnOp(...) to the IRBuilder to support unary FNeg Also update UnaryOperator to support isa, cast, and dyn_cast. Differential Revision: https://reviews.llvm.org/D62417 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361816 91177308-0d34-0410-b5e6-96231b3b80d8 Cameron McInally 1 year, 6 months ago
6 changed file(s) with 45 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
123123 return Fold(ConstantExpr::getNot(C));
124124 }
125125
126 Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
127 return Fold(ConstantExpr::get(Opc, C));
128 }
129
126130 //===--------------------------------------------------------------------===//
127131 // Memory Instructions
128132 //===--------------------------------------------------------------------===//
131131
132132 Constant *CreateNot(Constant *C) const {
133133 return ConstantExpr::getNot(C);
134 }
135
136 Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
137 return ConstantExpr::get(Opc, C);
134138 }
135139
136140 //===--------------------------------------------------------------------===//
13711371 return Insert(BinaryOperator::CreateNot(V), Name);
13721372 }
13731373
1374 Value *CreateUnOp(Instruction::UnaryOps Opc,
1375 Value *V, const Twine &Name = "",
1376 MDNode *FPMathTag = nullptr) {
1377 if (auto *VC = dyn_cast(V))
1378 return Insert(Folder.CreateUnOp(Opc, VC), Name);
1379 Instruction *UnOp = UnaryOperator::Create(Opc, V);
1380 if (isa(UnOp))
1381 UnOp = setFPAttrs(UnOp, FPMathTag, FMF);
1382 return Insert(UnOp, Name);
1383 }
1384
13741385 //===--------------------------------------------------------------------===//
13751386 // Instruction creation methods: Memory Instructions
13761387 //===--------------------------------------------------------------------===//
7676
7777 // Methods for support type inquiry through isa, cast, and dyn_cast:
7878 static bool classof(const Instruction *I) {
79 return I->getOpcode() == Instruction::Alloca ||
79 return I->isUnaryOp() ||
80 I->getOpcode() == Instruction::Alloca ||
8081 I->getOpcode() == Instruction::Load ||
8182 I->getOpcode() == Instruction::VAArg ||
8283 I->getOpcode() == Instruction::ExtractValue ||
154155
155156 UnaryOps getOpcode() const {
156157 return static_cast(Instruction::getOpcode());
158 }
159
160 // Methods for support type inquiry through isa, cast, and dyn_cast:
161 static bool classof(const Instruction *I) {
162 return I->isUnaryOp();
163 }
164 static bool classof(const Value *V) {
165 return isa(V) && classof(cast(V));
157166 }
158167 };
159168
202202 return BinaryOperator::CreateNot(C);
203203 }
204204
205 Instruction *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
206 return UnaryOperator::Create(Opc, C);
207 }
208
205209 //===--------------------------------------------------------------------===//
206210 // Memory Instructions
207211 //===--------------------------------------------------------------------===//
199199 unsigned IntPtrBitSize = DL->getPointerSizeInBits(0);
200200 EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
201201 delete DL;
202 }
203
204 TEST_F(IRBuilderTest, UnaryOperators) {
205 IRBuilder Builder(BB);
206 Value *V = Builder.CreateLoad(GV->getValueType(), GV);
207
208 // Test CreateUnOp
209 Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
210 ASSERT_TRUE(isa(U));
211 ASSERT_TRUE(isa(U));
212 ASSERT_TRUE(isa(U));
213 ASSERT_FALSE(isa(U));
202214 }
203215
204216 TEST_F(IRBuilderTest, FastMathFlags) {