[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / unittests / IR / UseTest.cpp
bloba647ec721081e26635a40ef06aea8ae1012099dd
1 //===- llvm/unittest/IR/UseTest.cpp - Use unit tests ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/AsmParser/Parser.h"
10 #include "llvm/IR/Function.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/IR/User.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
18 using namespace llvm;
20 namespace {
22 TEST(UseTest, sort) {
23 LLVMContext C;
25 const char *ModuleString = "define void @f(i32 %x) {\n"
26 "entry:\n"
27 " %v0 = add i32 %x, 0\n"
28 " %v2 = add i32 %x, 2\n"
29 " %v5 = add i32 %x, 5\n"
30 " %v1 = add i32 %x, 1\n"
31 " %v3 = add i32 %x, 3\n"
32 " %v7 = add i32 %x, 7\n"
33 " %v6 = add i32 %x, 6\n"
34 " %v4 = add i32 %x, 4\n"
35 " ret void\n"
36 "}\n";
37 SMDiagnostic Err;
38 char vnbuf[8];
39 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
40 Function *F = M->getFunction("f");
41 ASSERT_TRUE(F);
42 ASSERT_TRUE(F->arg_begin() != F->arg_end());
43 Argument &X = *F->arg_begin();
44 ASSERT_EQ("x", X.getName());
46 X.sortUseList([](const Use &L, const Use &R) {
47 return L.getUser()->getName() < R.getUser()->getName();
48 });
49 unsigned I = 0;
50 for (User *U : X.users()) {
51 format("v%u", I++).snprint(vnbuf, sizeof(vnbuf));
52 EXPECT_EQ(vnbuf, U->getName());
54 ASSERT_EQ(8u, I);
56 X.sortUseList([](const Use &L, const Use &R) {
57 return L.getUser()->getName() > R.getUser()->getName();
58 });
59 I = 0;
60 for (User *U : X.users()) {
61 format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf));
62 EXPECT_EQ(vnbuf, U->getName());
64 ASSERT_EQ(8u, I);
67 TEST(UseTest, reverse) {
68 LLVMContext C;
70 const char *ModuleString = "define void @f(i32 %x) {\n"
71 "entry:\n"
72 " %v0 = add i32 %x, 0\n"
73 " %v2 = add i32 %x, 2\n"
74 " %v5 = add i32 %x, 5\n"
75 " %v1 = add i32 %x, 1\n"
76 " %v3 = add i32 %x, 3\n"
77 " %v7 = add i32 %x, 7\n"
78 " %v6 = add i32 %x, 6\n"
79 " %v4 = add i32 %x, 4\n"
80 " ret void\n"
81 "}\n";
82 SMDiagnostic Err;
83 char vnbuf[8];
84 std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
85 Function *F = M->getFunction("f");
86 ASSERT_TRUE(F);
87 ASSERT_TRUE(F->arg_begin() != F->arg_end());
88 Argument &X = *F->arg_begin();
89 ASSERT_EQ("x", X.getName());
91 X.sortUseList([](const Use &L, const Use &R) {
92 return L.getUser()->getName() < R.getUser()->getName();
93 });
94 unsigned I = 0;
95 for (User *U : X.users()) {
96 format("v%u", I++).snprint(vnbuf, sizeof(vnbuf));
97 EXPECT_EQ(vnbuf, U->getName());
99 ASSERT_EQ(8u, I);
101 X.reverseUseList();
102 I = 0;
103 for (User *U : X.users()) {
104 format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf));
105 EXPECT_EQ(vnbuf, U->getName());
107 ASSERT_EQ(8u, I);
110 } // end anonymous namespace