blob: d64f5747e729e33c2a465580f306fb57e86a786b [file] [log] [blame]
Matt Arsenault52133812019-01-22 21:31:02 +00001//===- MachineIRBuilderTest.cpp -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "GISelMITest.h"
10#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
11
12TEST_F(GISelMITest, TestBuildConstantFConstant) {
13 if (!TM)
14 return;
15
Matt Arsenault52133812019-01-22 21:31:02 +000016 B.buildConstant(LLT::scalar(32), 42);
17 B.buildFConstant(LLT::scalar(32), 1.0);
18
19 B.buildConstant(LLT::vector(2, 32), 99);
20 B.buildFConstant(LLT::vector(2, 32), 2.0);
21
22 auto CheckStr = R"(
23 CHECK: [[CONST0:%[0-9]+]]:_(s32) = G_CONSTANT i32 42
24 CHECK: [[FCONST0:%[0-9]+]]:_(s32) = G_FCONSTANT float 1.000000e+00
25 CHECK: [[CONST1:%[0-9]+]]:_(s32) = G_CONSTANT i32 99
26 CHECK: [[VEC0:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[CONST1]]:_(s32), [[CONST1]]:_(s32)
Matt Arsenault8121ec22019-02-04 19:15:50 +000027 CHECK: [[FCONST1:%[0-9]+]]:_(s32) = G_FCONSTANT float 2.000000e+00
Matt Arsenault52133812019-01-22 21:31:02 +000028 CHECK: [[VEC1:%[0-9]+]]:_(<2 x s32>) = G_BUILD_VECTOR [[FCONST1]]:_(s32), [[FCONST1]]:_(s32)
Matt Arsenault52133812019-01-22 21:31:02 +000029 )";
30
Matt Arsenaultb3e86702019-02-04 18:58:27 +000031 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
Matt Arsenault52133812019-01-22 21:31:02 +000032}
Matt Arsenault8121ec22019-02-04 19:15:50 +000033
34
35#ifdef GTEST_HAS_DEATH_TEST
36#ifndef NDEBUG
37
38TEST_F(GISelMITest, TestBuildConstantFConstantDeath) {
39 if (!TM)
40 return;
41
42 LLVMContext &Ctx = MF->getFunction().getContext();
43 APInt APV32(32, 12345);
44
45 // Test APInt version breaks
46 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), APV32),
47 "creating constant with the wrong size");
48 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), APV32),
49 "creating constant with the wrong size");
50
51 // Test ConstantInt version breaks
52 ConstantInt *CI = ConstantInt::get(Ctx, APV32);
53 EXPECT_DEATH(B.buildConstant(LLT::scalar(16), *CI),
54 "creating constant with the wrong size");
55 EXPECT_DEATH(B.buildConstant(LLT::vector(2, 16), *CI),
56 "creating constant with the wrong size");
57
58 APFloat DoubleVal(APFloat::IEEEdouble());
59 ConstantFP *CF = ConstantFP::get(Ctx, DoubleVal);
60 EXPECT_DEATH(B.buildFConstant(LLT::scalar(16), *CF),
61 "creating fconstant with the wrong size");
62 EXPECT_DEATH(B.buildFConstant(LLT::vector(2, 16), *CF),
63 "creating fconstant with the wrong size");
64}
65
66#endif
67#endif
Matt Arsenaultf3a46d02019-02-04 19:53:19 +000068
69TEST_F(GISelMITest, DstOpSrcOp) {
70 if (!TM)
71 return;
72
73 SmallVector<unsigned, 4> Copies;
74 collectCopies(Copies, MF);
75
76 LLT s64 = LLT::scalar(64);
77 auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
78
79 // Test SrcOp and DstOp can be constructed directly from MachineOperand by
80 // copying the instruction
81 B.buildAdd(MIBAdd->getOperand(0), MIBAdd->getOperand(1), MIBAdd->getOperand(2));
82
83
84 auto CheckStr = R"(
85 ; CHECK: [[COPY0:%[0-9]+]]:_(s64) = COPY $x0
86 ; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
87 ; CHECK: [[ADD:%[0-9]+]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_
88 ; CHECK: [[ADD]]:_(s64) = G_ADD [[COPY0]]:_, [[COPY1]]:_
89 )";
90
91 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
92}