blob: 4884d374521af78e579736fc27d116edc9b36f78 [file] [log] [blame]
Marcello Maggioni61f48ca2017-08-10 15:35:25 +00001//===- MachineOperandTest.cpp ---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/ilist_node.h"
11#include "llvm/CodeGen/MachineOperand.h"
Francis Visoiu Mistrih6c4ca712017-12-08 11:40:06 +000012#include "llvm/IR/Constants.h"
13#include "llvm/IR/LLVMContext.h"
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000014#include "llvm/Support/raw_ostream.h"
Marcello Maggioni61f48ca2017-08-10 15:35:25 +000015#include "gtest/gtest.h"
16
17using namespace llvm;
18
19namespace {
20
21TEST(MachineOperandTest, ChangeToTargetIndexTest) {
22 // Creating a MachineOperand to change it to TargetIndex
23 MachineOperand MO = MachineOperand::CreateImm(50);
24
25 // Checking some precondition on the newly created
26 // MachineOperand.
27 ASSERT_TRUE(MO.isImm());
28 ASSERT_TRUE(MO.getImm() == 50);
29 ASSERT_FALSE(MO.isTargetIndex());
30
31 // Changing to TargetIndex with some arbitrary values
32 // for index, offset and flags.
33 MO.ChangeToTargetIndex(74, 57, 12);
34
35 // Checking that the mutation to TargetIndex happened
36 // correctly.
37 ASSERT_TRUE(MO.isTargetIndex());
38 ASSERT_TRUE(MO.getIndex() == 74);
39 ASSERT_TRUE(MO.getOffset() == 57);
40 ASSERT_TRUE(MO.getTargetFlags() == 12);
41}
42
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000043TEST(MachineOperandTest, PrintRegisterMask) {
44 uint32_t Dummy;
45 MachineOperand MO = MachineOperand::CreateRegMask(&Dummy);
46
47 // Checking some preconditions on the newly created
48 // MachineOperand.
49 ASSERT_TRUE(MO.isRegMask());
50 ASSERT_TRUE(MO.getRegMask() == &Dummy);
51
52 // Print a MachineOperand containing a RegMask. Here we check that without a
53 // TRI and IntrinsicInfo we still print a less detailed regmask.
54 std::string str;
55 raw_string_ostream OS(str);
56 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
57 ASSERT_TRUE(OS.str() == "<regmask ...>");
58}
59
60TEST(MachineOperandTest, PrintSubReg) {
61 // Create a MachineOperand with RegNum=1 and SubReg=5.
62 MachineOperand MO = MachineOperand::CreateReg(
63 /*Reg=*/1, /*isDef=*/false, /*isImp=*/false, /*isKill=*/false,
64 /*isDead=*/false, /*isUndef=*/false, /*isEarlyClobber=*/false,
65 /*SubReg=*/5, /*isDebug=*/false, /*isInternalRead=*/false);
66
67 // Checking some preconditions on the newly created
68 // MachineOperand.
69 ASSERT_TRUE(MO.isReg());
70 ASSERT_TRUE(MO.getReg() == 1);
71 ASSERT_TRUE(MO.getSubReg() == 5);
72
73 // Print a MachineOperand containing a SubReg. Here we check that without a
74 // TRI and IntrinsicInfo we can still print the subreg index.
75 std::string str;
76 raw_string_ostream OS(str);
77 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
78 ASSERT_TRUE(OS.str() == "%physreg1.subreg5");
79}
80
Francis Visoiu Mistrih6c4ca712017-12-08 11:40:06 +000081TEST(MachineOperandTest, PrintCImm) {
82 LLVMContext Context;
83 APInt Int(128, UINT64_MAX);
84 ++Int;
85 ConstantInt *CImm = ConstantInt::get(Context, Int);
86 // Create a MachineOperand with an Imm=(UINT64_MAX + 1)
87 MachineOperand MO = MachineOperand::CreateCImm(CImm);
88
89 // Checking some preconditions on the newly created
90 // MachineOperand.
91 ASSERT_TRUE(MO.isCImm());
92 ASSERT_TRUE(MO.getCImm() == CImm);
93 ASSERT_TRUE(MO.getCImm()->getValue() == Int);
94
95 // Print a MachineOperand containing a SubReg. Here we check that without a
96 // TRI and IntrinsicInfo we can still print the subreg index.
97 std::string str;
98 raw_string_ostream OS(str);
99 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
100 ASSERT_TRUE(OS.str() == "i128 18446744073709551616");
101}
102
Marcello Maggioni61f48ca2017-08-10 15:35:25 +0000103} // end namespace