Marcello Maggioni | 61f48ca | 2017-08-10 15:35:25 +0000 | [diff] [blame] | 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/ADT/ilist_node.h" |
| 11 | #include "llvm/CodeGen/MachineOperand.h" |
Francis Visoiu Mistrih | 6c4ca71 | 2017-12-08 11:40:06 +0000 | [diff] [blame^] | 12 | #include "llvm/IR/Constants.h" |
| 13 | #include "llvm/IR/LLVMContext.h" |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Marcello Maggioni | 61f48ca | 2017-08-10 15:35:25 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(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 Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 43 | TEST(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 | |
| 60 | TEST(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 Mistrih | 6c4ca71 | 2017-12-08 11:40:06 +0000 | [diff] [blame^] | 81 | TEST(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 Maggioni | 61f48ca | 2017-08-10 15:35:25 +0000 | [diff] [blame] | 103 | } // end namespace |