MachineOperandTest.cpp 12.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
//===- MachineOperandTest.cpp ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

TEST(MachineOperandTest, ChangeToTargetIndexTest) {
  // Creating a MachineOperand to change it to TargetIndex
  MachineOperand MO = MachineOperand::CreateImm(50);

  // Checking some precondition on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isImm());
  ASSERT_TRUE(MO.getImm() == 50);
  ASSERT_FALSE(MO.isTargetIndex());

  // Changing to TargetIndex with some arbitrary values
  // for index, offset and flags.
  MO.ChangeToTargetIndex(74, 57, 12);

  // Checking that the mutation to TargetIndex happened
  // correctly.
  ASSERT_TRUE(MO.isTargetIndex());
  ASSERT_TRUE(MO.getIndex() == 74);
  ASSERT_TRUE(MO.getOffset() == 57);
  ASSERT_TRUE(MO.getTargetFlags() == 12);
}

TEST(MachineOperandTest, PrintRegisterMask) {
  uint32_t Dummy;
  MachineOperand MO = MachineOperand::CreateRegMask(&Dummy);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isRegMask());
  ASSERT_TRUE(MO.getRegMask() == &Dummy);

  // Print a MachineOperand containing a RegMask. Here we check that without a
  // TRI and IntrinsicInfo we still print a less detailed regmask.
  std::string str;
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "<regmask ...>");
}

TEST(MachineOperandTest, PrintSubReg) {
  // Create a MachineOperand with RegNum=1 and SubReg=5.
  MachineOperand MO = MachineOperand::CreateReg(
      /*Reg=*/1, /*isDef=*/false, /*isImp=*/false, /*isKill=*/false,
      /*isDead=*/false, /*isUndef=*/false, /*isEarlyClobber=*/false,
      /*SubReg=*/5, /*isDebug=*/false, /*isInternalRead=*/false);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isReg());
  ASSERT_TRUE(MO.getReg() == 1);
  ASSERT_TRUE(MO.getSubReg() == 5);

  // Print a MachineOperand containing a SubReg. Here we check that without a
  // TRI and IntrinsicInfo we can still print the subreg index.
  std::string str;
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "$physreg1.subreg5");
}

TEST(MachineOperandTest, PrintCImm) {
  LLVMContext Context;
  APInt Int(128, UINT64_MAX);
  ++Int;
  ConstantInt *CImm = ConstantInt::get(Context, Int);
  // Create a MachineOperand with an Imm=(UINT64_MAX + 1)
  MachineOperand MO = MachineOperand::CreateCImm(CImm);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isCImm());
  ASSERT_TRUE(MO.getCImm() == CImm);
  ASSERT_TRUE(MO.getCImm()->getValue() == Int);

  // Print a MachineOperand containing a SubReg. Here we check that without a
  // TRI and IntrinsicInfo we can still print the subreg index.
  std::string str;
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "i128 18446744073709551616");
}

TEST(MachineOperandTest, PrintSubRegIndex) {
  // Create a MachineOperand with an immediate and print it as a subreg index.
  MachineOperand MO = MachineOperand::CreateImm(3);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isImm());
  ASSERT_TRUE(MO.getImm() == 3);

  // Print a MachineOperand containing a SubRegIdx. Here we check that without a
  // TRI and IntrinsicInfo we can print the operand as a subreg index.
  std::string str;
  raw_string_ostream OS(str);
  MachineOperand::printSubRegIdx(OS, MO.getImm(), nullptr);
  ASSERT_TRUE(OS.str() == "%subreg.3");
}

TEST(MachineOperandTest, PrintCPI) {
  // Create a MachineOperand with a constant pool index and print it.
  MachineOperand MO = MachineOperand::CreateCPI(0, 8);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isCPI());
  ASSERT_TRUE(MO.getIndex() == 0);
  ASSERT_TRUE(MO.getOffset() == 8);

  // Print a MachineOperand containing a constant pool index and a positive
  // offset.
  std::string str;
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "%const.0 + 8");
  }

  str.clear();

  MO.setOffset(-12);

  // Print a MachineOperand containing a constant pool index and a negative
  // offset.
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "%const.0 - 12");
  }
}

TEST(MachineOperandTest, PrintTargetIndexName) {
  // Create a MachineOperand with a target index and print it.
  MachineOperand MO = MachineOperand::CreateTargetIndex(0, 8);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isTargetIndex());
  ASSERT_TRUE(MO.getIndex() == 0);
  ASSERT_TRUE(MO.getOffset() == 8);

  // Print a MachineOperand containing a target index and a positive offset.
  std::string str;
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "target-index(<unknown>) + 8");
  }

  str.clear();

  MO.setOffset(-12);

  // Print a MachineOperand containing a target index and a negative offset.
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "target-index(<unknown>) - 12");
  }
}

TEST(MachineOperandTest, PrintJumpTableIndex) {
  // Create a MachineOperand with a jump-table index and print it.
  MachineOperand MO = MachineOperand::CreateJTI(3);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isJTI());
  ASSERT_TRUE(MO.getIndex() == 3);

  // Print a MachineOperand containing a jump-table index.
  std::string str;
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "%jump-table.3");
}

TEST(MachineOperandTest, PrintExternalSymbol) {
  // Create a MachineOperand with an external symbol and print it.
  MachineOperand MO = MachineOperand::CreateES("foo");

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isSymbol());
  ASSERT_TRUE(MO.getSymbolName() == StringRef("foo"));

  // Print a MachineOperand containing an external symbol and no offset.
  std::string str;
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "&foo");
  }

  str.clear();
  MO.setOffset(12);

  // Print a MachineOperand containing an external symbol and a positive offset.
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "&foo + 12");
  }

  str.clear();
  MO.setOffset(-12);

  // Print a MachineOperand containing an external symbol and a negative offset.
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "&foo - 12");
  }
}

TEST(MachineOperandTest, PrintGlobalAddress) {
  LLVMContext Ctx;
  Module M("MachineOperandGVTest", Ctx);
  M.getOrInsertGlobal("foo", Type::getInt32Ty(Ctx));

  GlobalValue *GV = M.getNamedValue("foo");

  // Create a MachineOperand with a global address and a positive offset and
  // print it.
  MachineOperand MO = MachineOperand::CreateGA(GV, 12);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isGlobal());
  ASSERT_TRUE(MO.getGlobal() == GV);
  ASSERT_TRUE(MO.getOffset() == 12);

  std::string str;
  // Print a MachineOperand containing a global address and a positive offset.
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "@foo + 12");
  }

  str.clear();
  MO.setOffset(-12);

  // Print a MachineOperand containing a global address and a negative offset.
  {
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "@foo - 12");
  }
}

TEST(MachineOperandTest, PrintRegisterLiveOut) {
  // Create a MachineOperand with a register live out list and print it.
  uint32_t Mask = 0;
  MachineOperand MO = MachineOperand::CreateRegLiveOut(&Mask);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isRegLiveOut());
  ASSERT_TRUE(MO.getRegLiveOut() == &Mask);

  std::string str;
  // Print a MachineOperand containing a register live out list without a TRI.
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "liveout(<unknown>)");
}

TEST(MachineOperandTest, PrintMetadata) {
  LLVMContext Ctx;
  Module M("MachineOperandMDNodeTest", Ctx);
  NamedMDNode *MD = M.getOrInsertNamedMetadata("namedmd");
  ModuleSlotTracker MST(&M);
  Metadata *MDS = MDString::get(Ctx, "foo");
  MDNode *Node = MDNode::get(Ctx, MDS);
  MD->addOperand(Node);

  // Create a MachineOperand with a metadata and print it.
  MachineOperand MO = MachineOperand::CreateMetadata(Node);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isMetadata());
  ASSERT_TRUE(MO.getMetadata() == Node);

  std::string str;
  // Print a MachineOperand containing a metadata node.
  raw_string_ostream OS(str);
  MO.print(OS, MST, LLT{}, /*OpIdx*/~0U, /*PrintDef=*/false, /*IsStandalone=*/false,
           /*ShouldPrintRegisterTies=*/false, 0, /*TRI=*/nullptr,
           /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "!0");
}

TEST(MachineOperandTest, PrintMCSymbol) {
  MCAsmInfo MAI;
  MCContext Ctx(&MAI, /*MRI=*/nullptr, /*MOFI=*/nullptr);
  MCSymbol *Sym = Ctx.getOrCreateSymbol("foo");

  // Create a MachineOperand with a metadata and print it.
  MachineOperand MO = MachineOperand::CreateMCSymbol(Sym);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isMCSymbol());
  ASSERT_TRUE(MO.getMCSymbol() == Sym);

  std::string str;
  // Print a MachineOperand containing a metadata node.
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "<mcsymbol foo>");
}

TEST(MachineOperandTest, PrintCFI) {
  // Create a MachineOperand with a CFI index but no function and print it.
  MachineOperand MO = MachineOperand::CreateCFIIndex(8);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isCFIIndex());
  ASSERT_TRUE(MO.getCFIIndex() == 8);

  std::string str;
  // Print a MachineOperand containing a CFI Index node but no machine function
  // attached to it.
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "<cfi directive>");
}

TEST(MachineOperandTest, PrintIntrinsicID) {
  // Create a MachineOperand with a generic intrinsic ID.
  MachineOperand MO = MachineOperand::CreateIntrinsicID(Intrinsic::bswap);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isIntrinsicID());
  ASSERT_TRUE(MO.getIntrinsicID() == Intrinsic::bswap);

  std::string str;
  {
    // Print a MachineOperand containing a generic intrinsic ID.
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "intrinsic(@llvm.bswap)");
  }

  str.clear();
  // Set a target-specific intrinsic.
  MO = MachineOperand::CreateIntrinsicID((Intrinsic::ID)-1);
  {
    // Print a MachineOperand containing a target-specific intrinsic ID but not
    // IntrinsicInfo.
    raw_string_ostream OS(str);
    MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
    ASSERT_TRUE(OS.str() == "intrinsic(4294967295)");
  }
}

TEST(MachineOperandTest, PrintPredicate) {
  // Create a MachineOperand with a generic intrinsic ID.
  MachineOperand MO = MachineOperand::CreatePredicate(CmpInst::ICMP_EQ);

  // Checking some preconditions on the newly created
  // MachineOperand.
  ASSERT_TRUE(MO.isPredicate());
  ASSERT_TRUE(MO.getPredicate() == CmpInst::ICMP_EQ);

  std::string str;
  // Print a MachineOperand containing a int predicate ICMP_EQ.
  raw_string_ostream OS(str);
  MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
  ASSERT_TRUE(OS.str() == "intpred(eq)");
}

TEST(MachineOperandTest, HashValue) {
  char SymName1[] = "test";
  char SymName2[] = "test";
  MachineOperand MO1 = MachineOperand::CreateES(SymName1);
  MachineOperand MO2 = MachineOperand::CreateES(SymName2);
  ASSERT_NE(SymName1, SymName2);
  ASSERT_EQ(hash_value(MO1), hash_value(MO2));
  ASSERT_TRUE(MO1.isIdenticalTo(MO2));
}

} // end namespace