llvm.org GIT mirror llvm / a55b77d
GlobalISel: IRTranslate llvm.fabs.* intrinsic Summary: Fabs is a common floating-point operation, especially for some expansions. This patch adds a new generic opcode for llvm.fabs.* intrinsic in order to avoid building/matching this intrinsic. Reviewers: qcolombet, aditya_nandakumar, dsanders, rovka Reviewed By: aditya_nandakumar Subscribers: kristof.beyls, javed.absar, llvm-commits Differential Revision: https://reviews.llvm.org/D43864 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326749 91177308-0d34-0410-b5e6-96231b3b80d8 Volkan Keles 2 years ago
6 changed file(s) with 59 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
286286 }
287287
288288 template
289 inline UnaryOp_match m_GFabs(const SrcTy &Src) {
290 return UnaryOp_match(Src);
291 }
292
293 template
289294 inline UnaryOp_match m_Copy(SrcTy &&Src) {
290295 return UnaryOp_match(std::forward(Src));
291296 }
426426 /// Generic unsigned-int to float conversion
427427 HANDLE_TARGET_OPCODE(G_UITOFP)
428428
429 /// Generic FP absolute value.
430 HANDLE_TARGET_OPCODE(G_FABS)
431
429432 /// Generic pointer offset
430433 HANDLE_TARGET_OPCODE(G_GEP)
431434
377377 let hasSideEffects = 0;
378378 }
379379
380 def G_FABS : GenericInstruction {
381 let OutOperandList = (outs type0:$dst);
382 let InOperandList = (ins type0:$src);
383 let hasSideEffects = 0;
384 }
385
380386 //------------------------------------------------------------------------------
381387 // Floating Point Binary ops.
382388 //------------------------------------------------------------------------------
742742 return true;
743743 case Intrinsic::log2:
744744 MIRBuilder.buildInstr(TargetOpcode::G_FLOG2)
745 .addDef(getOrCreateVReg(CI))
746 .addUse(getOrCreateVReg(*CI.getArgOperand(0)));
747 return true;
748 case Intrinsic::fabs:
749 MIRBuilder.buildInstr(TargetOpcode::G_FABS)
745750 .addDef(getOrCreateVReg(CI))
746751 .addUse(getOrCreateVReg(*CI.getArgOperand(0)));
747752 return true;
13171317 %res = call float @llvm.log2.f32(float %a)
13181318 ret float %res
13191319 }
1320
1321 declare float @llvm.fabs.f32(float)
1322 define float @test_fabs_intrin(float %a) {
1323 ; CHECK-LABEL: name: test_fabs_intrin
1324 ; CHECK: [[A:%[0-9]+]]:_(s32) = COPY $s0
1325 ; CHECK: [[RES:%[0-9]+]]:_(s32) = G_FABS [[A]]
1326 ; CHECK: $s0 = COPY [[RES]]
1327 %res = call float @llvm.fabs.f32(float %a)
1328 ret float %res
1329 }
1330
13201331 declare void @llvm.lifetime.start.p0i8(i64, i8*)
13211332 declare void @llvm.lifetime.end.p0i8(i64, i8*)
13221333 define void @test_lifetime_intrin() {
229229 ASSERT_EQ(Src1, Copies[1]);
230230 }
231231
232 TEST(PatternMatchInstr, MatchFPUnaryOp) {
233 LLVMContext Context;
234 std::unique_ptr TM = createTargetMachine();
235 if (!TM)
236 return;
237 auto ModuleMMIPair = createDummyModule(Context, *TM, "");
238 MachineFunction *MF =
239 getMFFromMMI(ModuleMMIPair.first.get(), ModuleMMIPair.second.get());
240 SmallVector Copies;
241 collectCopies(Copies, MF);
242 MachineBasicBlock *EntryMBB = &*MF->begin();
243 MachineIRBuilder B(*MF);
244 MachineRegisterInfo &MRI = MF->getRegInfo();
245 B.setInsertPt(*EntryMBB, EntryMBB->end());
246
247 // Truncate s64 to s32.
248 LLT s32 = LLT::scalar(32);
249 auto Copy0s32 = B.buildFPTrunc(s32, Copies[0]);
250
251 // Match G_FABS.
252 auto MIBFabs = B.buildInstr(TargetOpcode::G_FABS, s32, Copy0s32);
253 bool match = mi_match(MIBFabs->getOperand(0).getReg(), MRI, m_GFabs(m_Reg()));
254 ASSERT_TRUE(match);
255 unsigned Src;
256 match = mi_match(MIBFabs->getOperand(0).getReg(), MRI, m_GFabs(m_Reg(Src)));
257 ASSERT_TRUE(match);
258 ASSERT_EQ(Src, Copy0s32->getOperand(0).getReg());
259 }
260
232261 TEST(PatternMatchInstr, MatchExtendsTrunc) {
233262 LLVMContext Context;
234263 std::unique_ptr TM = createTargetMachine();