blob: 46f50ab0151cf723b4e83c45589852962628e305 [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 Mistrih440f69c2017-12-08 22:53:21 +000014#include "llvm/IR/ModuleSlotTracker.h"
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000015#include "llvm/Support/raw_ostream.h"
Marcello Maggioni61f48ca2017-08-10 15:35:25 +000016#include "gtest/gtest.h"
17
18using namespace llvm;
19
20namespace {
21
22TEST(MachineOperandTest, ChangeToTargetIndexTest) {
23 // Creating a MachineOperand to change it to TargetIndex
24 MachineOperand MO = MachineOperand::CreateImm(50);
25
26 // Checking some precondition on the newly created
27 // MachineOperand.
28 ASSERT_TRUE(MO.isImm());
29 ASSERT_TRUE(MO.getImm() == 50);
30 ASSERT_FALSE(MO.isTargetIndex());
31
32 // Changing to TargetIndex with some arbitrary values
33 // for index, offset and flags.
34 MO.ChangeToTargetIndex(74, 57, 12);
35
36 // Checking that the mutation to TargetIndex happened
37 // correctly.
38 ASSERT_TRUE(MO.isTargetIndex());
39 ASSERT_TRUE(MO.getIndex() == 74);
40 ASSERT_TRUE(MO.getOffset() == 57);
41 ASSERT_TRUE(MO.getTargetFlags() == 12);
42}
43
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000044TEST(MachineOperandTest, PrintRegisterMask) {
45 uint32_t Dummy;
46 MachineOperand MO = MachineOperand::CreateRegMask(&Dummy);
47
48 // Checking some preconditions on the newly created
49 // MachineOperand.
50 ASSERT_TRUE(MO.isRegMask());
51 ASSERT_TRUE(MO.getRegMask() == &Dummy);
52
53 // Print a MachineOperand containing a RegMask. Here we check that without a
54 // TRI and IntrinsicInfo we still print a less detailed regmask.
55 std::string str;
56 raw_string_ostream OS(str);
57 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
58 ASSERT_TRUE(OS.str() == "<regmask ...>");
59}
60
61TEST(MachineOperandTest, PrintSubReg) {
62 // Create a MachineOperand with RegNum=1 and SubReg=5.
63 MachineOperand MO = MachineOperand::CreateReg(
64 /*Reg=*/1, /*isDef=*/false, /*isImp=*/false, /*isKill=*/false,
65 /*isDead=*/false, /*isUndef=*/false, /*isEarlyClobber=*/false,
66 /*SubReg=*/5, /*isDebug=*/false, /*isInternalRead=*/false);
67
68 // Checking some preconditions on the newly created
69 // MachineOperand.
70 ASSERT_TRUE(MO.isReg());
71 ASSERT_TRUE(MO.getReg() == 1);
72 ASSERT_TRUE(MO.getSubReg() == 5);
73
74 // Print a MachineOperand containing a SubReg. Here we check that without a
75 // TRI and IntrinsicInfo we can still print the subreg index.
76 std::string str;
77 raw_string_ostream OS(str);
78 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
79 ASSERT_TRUE(OS.str() == "%physreg1.subreg5");
80}
81
Francis Visoiu Mistrih6c4ca712017-12-08 11:40:06 +000082TEST(MachineOperandTest, PrintCImm) {
83 LLVMContext Context;
84 APInt Int(128, UINT64_MAX);
85 ++Int;
86 ConstantInt *CImm = ConstantInt::get(Context, Int);
87 // Create a MachineOperand with an Imm=(UINT64_MAX + 1)
88 MachineOperand MO = MachineOperand::CreateCImm(CImm);
89
90 // Checking some preconditions on the newly created
91 // MachineOperand.
92 ASSERT_TRUE(MO.isCImm());
93 ASSERT_TRUE(MO.getCImm() == CImm);
94 ASSERT_TRUE(MO.getCImm()->getValue() == Int);
95
96 // Print a MachineOperand containing a SubReg. Here we check that without a
97 // TRI and IntrinsicInfo we can still print the subreg index.
98 std::string str;
99 raw_string_ostream OS(str);
100 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
101 ASSERT_TRUE(OS.str() == "i128 18446744073709551616");
102}
103
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +0000104TEST(MachineOperandTest, PrintSubRegIndex) {
105 // Create a MachineOperand with an immediate and print it as a subreg index.
106 MachineOperand MO = MachineOperand::CreateImm(3);
107
108 // Checking some preconditions on the newly created
109 // MachineOperand.
110 ASSERT_TRUE(MO.isImm());
111 ASSERT_TRUE(MO.getImm() == 3);
112
113 // Print a MachineOperand containing a SubRegIdx. Here we check that without a
114 // TRI and IntrinsicInfo we can print the operand as a subreg index.
115 std::string str;
116 raw_string_ostream OS(str);
117 ModuleSlotTracker DummyMST(nullptr);
118 MachineOperand::printSubregIdx(OS, MO.getImm(), nullptr);
119 ASSERT_TRUE(OS.str() == "%subreg.3");
120}
121
Francis Visoiu Mistrih26ae8a62017-12-13 10:30:45 +0000122TEST(MachineOperandTest, PrintCPI) {
123 // Create a MachineOperand with a constant pool index and print it.
124 MachineOperand MO = MachineOperand::CreateCPI(0, 8);
125
126 // Checking some preconditions on the newly created
127 // MachineOperand.
128 ASSERT_TRUE(MO.isCPI());
129 ASSERT_TRUE(MO.getIndex() == 0);
130 ASSERT_TRUE(MO.getOffset() == 8);
131
132 // Print a MachineOperand containing a constant pool index and a positive
133 // offset.
134 std::string str;
135 {
136 raw_string_ostream OS(str);
137 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
138 ASSERT_TRUE(OS.str() == "%const.0 + 8");
139 }
140
141 str.clear();
142
143 MO.setOffset(-12);
144
145 // Print a MachineOperand containing a constant pool index and a negative
146 // offset.
147 {
148 raw_string_ostream OS(str);
149 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
150 ASSERT_TRUE(OS.str() == "%const.0 - 12");
151 }
152}
153
Francis Visoiu Mistrihb3a0d512017-12-13 10:30:51 +0000154TEST(MachineOperandTest, PrintTargetIndexName) {
155 // Create a MachineOperand with a target index and print it.
156 MachineOperand MO = MachineOperand::CreateTargetIndex(0, 8);
157
158 // Checking some preconditions on the newly created
159 // MachineOperand.
160 ASSERT_TRUE(MO.isTargetIndex());
161 ASSERT_TRUE(MO.getIndex() == 0);
162 ASSERT_TRUE(MO.getOffset() == 8);
163
164 // Print a MachineOperand containing a target index and a positive offset.
165 std::string str;
166 {
167 raw_string_ostream OS(str);
168 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
169 ASSERT_TRUE(OS.str() == "target-index(<unknown>) + 8");
170 }
171
172 str.clear();
173
174 MO.setOffset(-12);
175
176 // Print a MachineOperand containing a target index and a negative offset.
177 {
178 raw_string_ostream OS(str);
179 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
180 ASSERT_TRUE(OS.str() == "target-index(<unknown>) - 12");
181 }
182}
183
Marcello Maggioni61f48ca2017-08-10 15:35:25 +0000184} // end namespace