xref: /llvm-project/llvm/unittests/CodeGen/MachineOperandTest.cpp (revision bbd610ae925b3d21babc5d41e184cd4a93d172fd)
1 //===- 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/CodeGen/MachineOperand.h"
11 #include "llvm/ADT/ilist_node.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/ModuleSlotTracker.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "gtest/gtest.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 
25 TEST(MachineOperandTest, ChangeToTargetIndexTest) {
26   // Creating a MachineOperand to change it to TargetIndex
27   MachineOperand MO = MachineOperand::CreateImm(50);
28 
29   // Checking some precondition on the newly created
30   // MachineOperand.
31   ASSERT_TRUE(MO.isImm());
32   ASSERT_TRUE(MO.getImm() == 50);
33   ASSERT_FALSE(MO.isTargetIndex());
34 
35   // Changing to TargetIndex with some arbitrary values
36   // for index, offset and flags.
37   MO.ChangeToTargetIndex(74, 57, 12);
38 
39   // Checking that the mutation to TargetIndex happened
40   // correctly.
41   ASSERT_TRUE(MO.isTargetIndex());
42   ASSERT_TRUE(MO.getIndex() == 74);
43   ASSERT_TRUE(MO.getOffset() == 57);
44   ASSERT_TRUE(MO.getTargetFlags() == 12);
45 }
46 
47 TEST(MachineOperandTest, PrintRegisterMask) {
48   uint32_t Dummy;
49   MachineOperand MO = MachineOperand::CreateRegMask(&Dummy);
50 
51   // Checking some preconditions on the newly created
52   // MachineOperand.
53   ASSERT_TRUE(MO.isRegMask());
54   ASSERT_TRUE(MO.getRegMask() == &Dummy);
55 
56   // Print a MachineOperand containing a RegMask. Here we check that without a
57   // TRI and IntrinsicInfo we still print a less detailed regmask.
58   std::string str;
59   raw_string_ostream OS(str);
60   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
61   ASSERT_TRUE(OS.str() == "<regmask ...>");
62 }
63 
64 TEST(MachineOperandTest, PrintSubReg) {
65   // Create a MachineOperand with RegNum=1 and SubReg=5.
66   MachineOperand MO = MachineOperand::CreateReg(
67       /*Reg=*/1, /*isDef=*/false, /*isImp=*/false, /*isKill=*/false,
68       /*isDead=*/false, /*isUndef=*/false, /*isEarlyClobber=*/false,
69       /*SubReg=*/5, /*isDebug=*/false, /*isInternalRead=*/false);
70 
71   // Checking some preconditions on the newly created
72   // MachineOperand.
73   ASSERT_TRUE(MO.isReg());
74   ASSERT_TRUE(MO.getReg() == 1);
75   ASSERT_TRUE(MO.getSubReg() == 5);
76 
77   // Print a MachineOperand containing a SubReg. Here we check that without a
78   // TRI and IntrinsicInfo we can still print the subreg index.
79   std::string str;
80   raw_string_ostream OS(str);
81   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
82   ASSERT_TRUE(OS.str() == "%physreg1.subreg5");
83 }
84 
85 TEST(MachineOperandTest, PrintCImm) {
86   LLVMContext Context;
87   APInt Int(128, UINT64_MAX);
88   ++Int;
89   ConstantInt *CImm = ConstantInt::get(Context, Int);
90   // Create a MachineOperand with an Imm=(UINT64_MAX + 1)
91   MachineOperand MO = MachineOperand::CreateCImm(CImm);
92 
93   // Checking some preconditions on the newly created
94   // MachineOperand.
95   ASSERT_TRUE(MO.isCImm());
96   ASSERT_TRUE(MO.getCImm() == CImm);
97   ASSERT_TRUE(MO.getCImm()->getValue() == Int);
98 
99   // Print a MachineOperand containing a SubReg. Here we check that without a
100   // TRI and IntrinsicInfo we can still print the subreg index.
101   std::string str;
102   raw_string_ostream OS(str);
103   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
104   ASSERT_TRUE(OS.str() == "i128 18446744073709551616");
105 }
106 
107 TEST(MachineOperandTest, PrintSubRegIndex) {
108   // Create a MachineOperand with an immediate and print it as a subreg index.
109   MachineOperand MO = MachineOperand::CreateImm(3);
110 
111   // Checking some preconditions on the newly created
112   // MachineOperand.
113   ASSERT_TRUE(MO.isImm());
114   ASSERT_TRUE(MO.getImm() == 3);
115 
116   // Print a MachineOperand containing a SubRegIdx. Here we check that without a
117   // TRI and IntrinsicInfo we can print the operand as a subreg index.
118   std::string str;
119   raw_string_ostream OS(str);
120   ModuleSlotTracker DummyMST(nullptr);
121   MachineOperand::printSubregIdx(OS, MO.getImm(), nullptr);
122   ASSERT_TRUE(OS.str() == "%subreg.3");
123 }
124 
125 TEST(MachineOperandTest, PrintCPI) {
126   // Create a MachineOperand with a constant pool index and print it.
127   MachineOperand MO = MachineOperand::CreateCPI(0, 8);
128 
129   // Checking some preconditions on the newly created
130   // MachineOperand.
131   ASSERT_TRUE(MO.isCPI());
132   ASSERT_TRUE(MO.getIndex() == 0);
133   ASSERT_TRUE(MO.getOffset() == 8);
134 
135   // Print a MachineOperand containing a constant pool index and a positive
136   // offset.
137   std::string str;
138   {
139     raw_string_ostream OS(str);
140     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
141     ASSERT_TRUE(OS.str() == "%const.0 + 8");
142   }
143 
144   str.clear();
145 
146   MO.setOffset(-12);
147 
148   // Print a MachineOperand containing a constant pool index and a negative
149   // offset.
150   {
151     raw_string_ostream OS(str);
152     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
153     ASSERT_TRUE(OS.str() == "%const.0 - 12");
154   }
155 }
156 
157 TEST(MachineOperandTest, PrintTargetIndexName) {
158   // Create a MachineOperand with a target index and print it.
159   MachineOperand MO = MachineOperand::CreateTargetIndex(0, 8);
160 
161   // Checking some preconditions on the newly created
162   // MachineOperand.
163   ASSERT_TRUE(MO.isTargetIndex());
164   ASSERT_TRUE(MO.getIndex() == 0);
165   ASSERT_TRUE(MO.getOffset() == 8);
166 
167   // Print a MachineOperand containing a target index and a positive offset.
168   std::string str;
169   {
170     raw_string_ostream OS(str);
171     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
172     ASSERT_TRUE(OS.str() == "target-index(<unknown>) + 8");
173   }
174 
175   str.clear();
176 
177   MO.setOffset(-12);
178 
179   // Print a MachineOperand containing a target index and a negative offset.
180   {
181     raw_string_ostream OS(str);
182     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
183     ASSERT_TRUE(OS.str() == "target-index(<unknown>) - 12");
184   }
185 }
186 
187 TEST(MachineOperandTest, PrintJumpTableIndex) {
188   // Create a MachineOperand with a jump-table index and print it.
189   MachineOperand MO = MachineOperand::CreateJTI(3);
190 
191   // Checking some preconditions on the newly created
192   // MachineOperand.
193   ASSERT_TRUE(MO.isJTI());
194   ASSERT_TRUE(MO.getIndex() == 3);
195 
196   // Print a MachineOperand containing a jump-table index.
197   std::string str;
198   raw_string_ostream OS(str);
199   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
200   ASSERT_TRUE(OS.str() == "%jump-table.3");
201 }
202 
203 TEST(MachineOperandTest, PrintExternalSymbol) {
204   // Create a MachineOperand with an external symbol and print it.
205   MachineOperand MO = MachineOperand::CreateES("foo");
206 
207   // Checking some preconditions on the newly created
208   // MachineOperand.
209   ASSERT_TRUE(MO.isSymbol());
210   ASSERT_TRUE(MO.getSymbolName() == StringRef("foo"));
211 
212   // Print a MachineOperand containing an external symbol and no offset.
213   std::string str;
214   {
215     raw_string_ostream OS(str);
216     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
217     ASSERT_TRUE(OS.str() == "$foo");
218   }
219 
220   str.clear();
221   MO.setOffset(12);
222 
223   // Print a MachineOperand containing an external symbol and a positive offset.
224   {
225     raw_string_ostream OS(str);
226     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
227     ASSERT_TRUE(OS.str() == "$foo + 12");
228   }
229 
230   str.clear();
231   MO.setOffset(-12);
232 
233   // Print a MachineOperand containing an external symbol and a negative offset.
234   {
235     raw_string_ostream OS(str);
236     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
237     ASSERT_TRUE(OS.str() == "$foo - 12");
238   }
239 }
240 
241 TEST(MachineOperandTest, PrintGlobalAddress) {
242   LLVMContext Ctx;
243   Module M("MachineOperandGVTest", Ctx);
244   M.getOrInsertGlobal("foo", Type::getInt32Ty(Ctx));
245 
246   GlobalValue *GV = M.getNamedValue("foo");
247 
248   // Create a MachineOperand with a global address and a positive offset and
249   // print it.
250   MachineOperand MO = MachineOperand::CreateGA(GV, 12);
251 
252   // Checking some preconditions on the newly created
253   // MachineOperand.
254   ASSERT_TRUE(MO.isGlobal());
255   ASSERT_TRUE(MO.getGlobal() == GV);
256   ASSERT_TRUE(MO.getOffset() == 12);
257 
258   std::string str;
259   // Print a MachineOperand containing a global address and a positive offset.
260   {
261     raw_string_ostream OS(str);
262     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
263     ASSERT_TRUE(OS.str() == "@foo + 12");
264   }
265 
266   str.clear();
267   MO.setOffset(-12);
268 
269   // Print a MachineOperand containing a global address and a negative offset.
270   {
271     raw_string_ostream OS(str);
272     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
273     ASSERT_TRUE(OS.str() == "@foo - 12");
274   }
275 }
276 
277 TEST(MachineOperandTest, PrintRegisterLiveOut) {
278   // Create a MachineOperand with a register live out list and print it.
279   uint32_t Mask = 0;
280   MachineOperand MO = MachineOperand::CreateRegLiveOut(&Mask);
281 
282   // Checking some preconditions on the newly created
283   // MachineOperand.
284   ASSERT_TRUE(MO.isRegLiveOut());
285   ASSERT_TRUE(MO.getRegLiveOut() == &Mask);
286 
287   std::string str;
288   // Print a MachineOperand containing a register live out list without a TRI.
289   raw_string_ostream OS(str);
290   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
291   ASSERT_TRUE(OS.str() == "liveout(<unknown>)");
292 }
293 
294 TEST(MachineOperandTest, PrintMetadata) {
295   LLVMContext Ctx;
296   Module M("MachineOperandMDNodeTest", Ctx);
297   NamedMDNode *MD = M.getOrInsertNamedMetadata("namedmd");
298   ModuleSlotTracker DummyMST(&M);
299   Metadata *MDS = MDString::get(Ctx, "foo");
300   MDNode *Node = MDNode::get(Ctx, MDS);
301   MD->addOperand(Node);
302 
303   // Create a MachineOperand with a metadata and print it.
304   MachineOperand MO = MachineOperand::CreateMetadata(Node);
305 
306   // Checking some preconditions on the newly created
307   // MachineOperand.
308   ASSERT_TRUE(MO.isMetadata());
309   ASSERT_TRUE(MO.getMetadata() == Node);
310 
311   std::string str;
312   // Print a MachineOperand containing a metadata node.
313   raw_string_ostream OS(str);
314   MO.print(OS, DummyMST, LLT{}, false, false, 0, /*TRI=*/nullptr,
315            /*IntrinsicInfo=*/nullptr);
316   ASSERT_TRUE(OS.str() == "!0");
317 }
318 
319 TEST(MachineOperandTest, PrintMCSymbol) {
320   MCAsmInfo MAI;
321   MCContext Ctx(&MAI, /*MRI=*/nullptr, /*MOFI=*/nullptr);
322   MCSymbol *Sym = Ctx.getOrCreateSymbol("foo");
323 
324   // Create a MachineOperand with a metadata and print it.
325   MachineOperand MO = MachineOperand::CreateMCSymbol(Sym);
326 
327   // Checking some preconditions on the newly created
328   // MachineOperand.
329   ASSERT_TRUE(MO.isMCSymbol());
330   ASSERT_TRUE(MO.getMCSymbol() == Sym);
331 
332   std::string str;
333   // Print a MachineOperand containing a metadata node.
334   raw_string_ostream OS(str);
335   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
336   ASSERT_TRUE(OS.str() == "<mcsymbol foo>");
337 }
338 
339 TEST(MachineOperandTest, PrintCFI) {
340   // Create a MachineOperand with a CFI index but no function and print it.
341   MachineOperand MO = MachineOperand::CreateCFIIndex(8);
342 
343   // Checking some preconditions on the newly created
344   // MachineOperand.
345   ASSERT_TRUE(MO.isCFIIndex());
346   ASSERT_TRUE(MO.getCFIIndex() == 8);
347 
348   std::string str;
349   // Print a MachineOperand containing a CFI Index node but no machine function
350   // attached to it.
351   raw_string_ostream OS(str);
352   MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
353   ASSERT_TRUE(OS.str() == "<cfi directive>");
354 }
355 
356 TEST(MachineOperandTest, PrintIntrinsicID) {
357   // Create a MachineOperand with a generic intrinsic ID.
358   MachineOperand MO = MachineOperand::CreateIntrinsicID(Intrinsic::bswap);
359 
360   // Checking some preconditions on the newly created
361   // MachineOperand.
362   ASSERT_TRUE(MO.isIntrinsicID());
363   ASSERT_TRUE(MO.getIntrinsicID() == Intrinsic::bswap);
364 
365   std::string str;
366   {
367     // Print a MachineOperand containing a generic intrinsic ID.
368     raw_string_ostream OS(str);
369     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
370     ASSERT_TRUE(OS.str() == "intrinsic(@llvm.bswap)");
371   }
372 
373   str.clear();
374   // Set a target-specific intrinsic.
375   MO = MachineOperand::CreateIntrinsicID((Intrinsic::ID)-1);
376   {
377     // Print a MachineOperand containing a target-specific intrinsic ID but not
378     // IntrinsicInfo.
379     raw_string_ostream OS(str);
380     MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
381     ASSERT_TRUE(OS.str() == "intrinsic(4294967295)");
382   }
383 }
384 
385 } // end namespace
386