llvm.org GIT mirror llvm / 8e52f27
Add IsBytewiseValue unit test Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63966 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365710 91177308-0d34-0410-b5e6-96231b3b80d8 Vitaly Buka a month ago
1 changed file(s) with 269 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
1212 #include "llvm/IR/LLVMContext.h"
1313 #include "llvm/IR/Module.h"
1414 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/KnownBits.h"
1516 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/KnownBits.h"
1717 #include "gtest/gtest.h"
1818
1919 using namespace llvm;
2222
2323 class ValueTrackingTest : public testing::Test {
2424 protected:
25 void parseAssembly(const char *Assembly) {
25 std::unique_ptr parseModule(StringRef Assembly) {
2626 SMDiagnostic Error;
27 M = parseAssemblyString(Assembly, Error, Context);
27 std::unique_ptr M = parseAssemblyString(Assembly, Error, Context);
2828
2929 std::string errMsg;
3030 raw_string_ostream os(errMsg);
3131 Error.print("", os);
32
33 // A failure here means that the test itself is buggy.
34 if (!M)
35 report_fatal_error(os.str());
32 EXPECT_TRUE(M) << os.str();
33
34 return M;
35 }
36
37 void parseAssembly(StringRef Assembly) {
38 M = parseModule(Assembly);
39 ASSERT_TRUE(M);
3640
3741 Function *F = M->getFunction("test");
38 if (F == nullptr)
39 report_fatal_error("Test must have a function named @test");
42 ASSERT_TRUE(F) << "Test must have a function @test";
43 if (!F)
44 return;
4045
4146 A = nullptr;
4247 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
4550 A = &*I;
4651 }
4752 }
48 if (A == nullptr)
49 report_fatal_error("@test must have an instruction %A");
53 ASSERT_TRUE(A) << "@test must have an instruction %A";
5054 }
5155
5256 LLVMContext Context;
5357 std::unique_ptr M;
54 Instruction *A;
58 Instruction *A = nullptr;
5559 };
5660
5761 class MatchSelectPatternTest : public ValueTrackingTest {
688692 "declare i8 @llvm.usub.sat.i8(i8, i8)\n");
689693 expectKnownBits(/*zero*/ 2u, /*one*/ 0u);
690694 }
695
696 class IsBytewiseValueTest : public ValueTrackingTest,
697 public ::testing::WithParamInterface<
698 std::pair> {
699 protected:
700 };
701
702 const std::pair IsBytewiseValueTests[] = {
703 {
704 "i8 0",
705 "i48* null",
706 },
707 {
708 "i8 undef",
709 "i48* undef",
710 },
711 {
712 "i8 0",
713 "i8 zeroinitializer",
714 },
715 {
716 "i8 0",
717 "i8 0",
718 },
719 {
720 "i8 -86",
721 "i8 -86",
722 },
723 {
724 "i8 -1",
725 "i8 -1",
726 },
727 {
728 "i8 undef",
729 "i16 undef",
730 },
731 {
732 "i8 0",
733 "i16 0",
734 },
735 {
736 "",
737 "i16 7",
738 },
739 {
740 "i8 -86",
741 "i16 -21846",
742 },
743 {
744 "i8 -1",
745 "i16 -1",
746 },
747 {
748 "i8 0",
749 "i48 0",
750 },
751 {
752 "i8 -1",
753 "i48 -1",
754 },
755 {
756 "i8 0",
757 "i49 0",
758 },
759 {
760 "",
761 "i49 -1",
762 },
763 {
764 "i8 0",
765 "half 0xH0000",
766 },
767 {
768 "i8 -85",
769 "half 0xHABAB",
770 },
771 {
772 "i8 0",
773 "float 0.0",
774 },
775 {
776 "i8 -1",
777 "float 0xFFFFFFFFE0000000",
778 },
779 {
780 "i8 0",
781 "double 0.0",
782 },
783 {
784 "i8 -15",
785 "double 0xF1F1F1F1F1F1F1F1",
786 },
787 {
788 "i8 undef",
789 "i16* undef",
790 },
791 {
792 "i8 0",
793 "i16* inttoptr (i64 0 to i16*)",
794 },
795 {
796 "",
797 "i16* inttoptr (i64 -1 to i16*)",
798 },
799 {
800 "",
801 "i16* inttoptr (i64 -6148914691236517206 to i16*)",
802 },
803 {
804 "",
805 "i16* inttoptr (i48 -1 to i16*)",
806 },
807 {
808 "",
809 "i16* inttoptr (i96 -1 to i16*)",
810 },
811 {
812 "i8 0",
813 "[0 x i8] zeroinitializer",
814 },
815 {
816 "i8 undef",
817 "[0 x i8] undef",
818 },
819 {
820 "i8 0",
821 "[5 x [0 x i8]] zeroinitializer",
822 },
823 {
824 "i8 undef",
825 "[5 x [0 x i8]] undef",
826 },
827 {
828 "i8 0",
829 "[6 x i8] zeroinitializer",
830 },
831 {
832 "i8 undef",
833 "[6 x i8] undef",
834 },
835 {
836 "i8 1",
837 "[5 x i8] [i8 1, i8 1, i8 1, i8 1, i8 1]",
838 },
839 {
840 "",
841 "[5 x i64] [i64 1, i64 1, i64 1, i64 1, i64 1]",
842 },
843 {
844 "i8 -1",
845 "[5 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]",
846 },
847 {
848 "",
849 "[4 x i8] [i8 1, i8 2, i8 1, i8 1]",
850 },
851 {
852 "i8 1",
853 "[4 x i8] [i8 1, i8 undef, i8 1, i8 1]",
854 },
855 {
856 "i8 0",
857 "<6 x i8> zeroinitializer",
858 },
859 {
860 "i8 undef",
861 "<6 x i8> undef",
862 },
863 {
864 "i8 1",
865 "<5 x i8> ",
866 },
867 {
868 "",
869 "<5 x i64> ",
870 },
871 {
872 "i8 -1",
873 "<5 x i64> ",
874 },
875 {
876 "",
877 "<4 x i8> ",
878 },
879 {
880 "",
881 "<2 x i8> < i8 5, i8 undef >",
882 },
883 {
884 "i8 0",
885 "[2 x [2 x i16]] zeroinitializer",
886 },
887 {
888 "i8 undef",
889 "[2 x [2 x i16]] undef",
890 },
891 {
892 "i8 -86",
893 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
894 "[2 x i16] [i16 -21846, i16 -21846]]",
895 },
896 {
897 "",
898 "[2 x [2 x i16]] [[2 x i16] [i16 -21846, i16 -21846], "
899 "[2 x i16] [i16 -21836, i16 -21846]]",
900 },
901 {
902 "i8 0",
903 "{ } zeroinitializer",
904 },
905 {
906 "i8 undef",
907 "{ } undef",
908 },
909 {
910 "i8 0",
911 "{ {}, {} } zeroinitializer",
912 },
913 {
914 "i8 undef",
915 "{ {}, {} } undef",
916 },
917 {
918 "i8 0",
919 "{i8, i64, i16*} zeroinitializer",
920 },
921 {
922 "i8 undef",
923 "{i8, i64, i16*} undef",
924 },
925 {
926 "i8 -86",
927 "{i8, i64, i16*} {i8 -86, i64 -6148914691236517206, i16* undef}",
928 },
929 {
930 "",
931 "{i8, i64, i16*} {i8 86, i64 -6148914691236517206, i16* undef}",
932 },
933 };
934
935 INSTANTIATE_TEST_CASE_P(IsBytewiseValueParamTests, IsBytewiseValueTest,
936 ::testing::ValuesIn(IsBytewiseValueTests));
937
938 TEST_P(IsBytewiseValueTest, IsBytewiseValue) {
939 auto M = parseModule(std::string("@test = global ") + GetParam().second);
940 GlobalVariable *GV = dyn_cast(M->getNamedValue("test"));
941 Value *Actual = isBytewiseValue(GV->getInitializer(), M->getDataLayout());
942 std::string Buff;
943 raw_string_ostream S(Buff);
944 if (Actual)
945 S << *Actual;
946 EXPECT_EQ(GetParam().first, S.str());
947 }