Chandler Carruth | c779e96 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===// |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "llvm-c/Core.h" |
| 12 | #include "llvm/AsmParser/Parser.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 13 | #include "llvm/IR/DerivedTypes.h" |
| 14 | #include "llvm/IR/InstrTypes.h" |
| 15 | #include "llvm/IR/Instruction.h" |
| 16 | #include "llvm/IR/LLVMContext.h" |
| 17 | #include "llvm/IR/Module.h" |
Rafael Espindola | 38ce6c7 | 2015-02-23 21:51:06 +0000 | [diff] [blame] | 18 | #include "llvm/Support/SourceMgr.h" |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace { |
| 23 | |
| 24 | TEST(ConstantsTest, Integer_i1) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 25 | LLVMContext Context; |
| 26 | IntegerType *Int1 = IntegerType::get(Context, 1); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 27 | Constant* One = ConstantInt::get(Int1, 1, true); |
| 28 | Constant* Zero = ConstantInt::get(Int1, 0); |
| 29 | Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true); |
| 30 | EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1)); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 31 | Constant* Undef = UndefValue::get(Int1); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 32 | |
| 33 | // Input: @b = constant i1 add(i1 1 , i1 1) |
| 34 | // Output: @b = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 35 | EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 36 | |
| 37 | // @c = constant i1 add(i1 -1, i1 1) |
| 38 | // @c = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 39 | EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 40 | |
| 41 | // @d = constant i1 add(i1 -1, i1 -1) |
| 42 | // @d = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 43 | EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 44 | |
| 45 | // @e = constant i1 sub(i1 -1, i1 1) |
| 46 | // @e = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 47 | EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 48 | |
| 49 | // @f = constant i1 sub(i1 1 , i1 -1) |
| 50 | // @f = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 51 | EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 52 | |
| 53 | // @g = constant i1 sub(i1 1 , i1 1) |
| 54 | // @g = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 55 | EXPECT_EQ(Zero, ConstantExpr::getSub(One, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 56 | |
| 57 | // @h = constant i1 shl(i1 1 , i1 1) ; undefined |
| 58 | // @h = constant i1 undef |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 59 | EXPECT_EQ(Undef, ConstantExpr::getShl(One, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 60 | |
| 61 | // @i = constant i1 shl(i1 1 , i1 0) |
| 62 | // @i = constant i1 true |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 63 | EXPECT_EQ(One, ConstantExpr::getShl(One, Zero)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 64 | |
| 65 | // @j = constant i1 lshr(i1 1, i1 1) ; undefined |
| 66 | // @j = constant i1 undef |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 67 | EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 68 | |
| 69 | // @m = constant i1 ashr(i1 1, i1 1) ; undefined |
| 70 | // @m = constant i1 undef |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 71 | EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 72 | |
| 73 | // @n = constant i1 mul(i1 -1, i1 1) |
| 74 | // @n = constant i1 true |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 75 | EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 76 | |
| 77 | // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow |
| 78 | // @o = constant i1 true |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 79 | EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 80 | |
| 81 | // @p = constant i1 sdiv(i1 1 , i1 -1); overflow |
| 82 | // @p = constant i1 true |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 83 | EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 84 | |
| 85 | // @q = constant i1 udiv(i1 -1, i1 1) |
| 86 | // @q = constant i1 true |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 87 | EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 88 | |
| 89 | // @r = constant i1 udiv(i1 1, i1 -1) |
| 90 | // @r = constant i1 true |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 91 | EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 92 | |
| 93 | // @s = constant i1 srem(i1 -1, i1 1) ; overflow |
| 94 | // @s = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 95 | EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 96 | |
| 97 | // @t = constant i1 urem(i1 -1, i1 1) |
| 98 | // @t = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 99 | EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 100 | |
| 101 | // @u = constant i1 srem(i1 1, i1 -1) ; overflow |
| 102 | // @u = constant i1 false |
Benjamin Kramer | 9138b19 | 2009-07-29 19:18:13 +0000 | [diff] [blame] | 103 | EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne)); |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | f3b0aac | 2009-04-24 05:30:14 +0000 | [diff] [blame] | 106 | TEST(ConstantsTest, IntSigns) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 107 | LLVMContext Context; |
| 108 | IntegerType *Int8Ty = Type::getInt8Ty(Context); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 109 | EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue()); |
| 110 | EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue()); |
| 111 | EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue()); |
| 112 | EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue()); |
| 113 | EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue()); |
| 114 | EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue()); |
Chris Lattner | f3b0aac | 2009-04-24 05:30:14 +0000 | [diff] [blame] | 115 | |
| 116 | // Overflow is handled by truncation. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 117 | EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue()); |
Chris Lattner | f3b0aac | 2009-04-24 05:30:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | e562dba | 2010-12-29 01:33:36 +0000 | [diff] [blame] | 120 | TEST(ConstantsTest, FP128Test) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 121 | LLVMContext Context; |
| 122 | Type *FP128Ty = Type::getFP128Ty(Context); |
Chris Lattner | e562dba | 2010-12-29 01:33:36 +0000 | [diff] [blame] | 123 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 124 | IntegerType *Int128Ty = Type::getIntNTy(Context, 128); |
Chris Lattner | e562dba | 2010-12-29 01:33:36 +0000 | [diff] [blame] | 125 | Constant *Zero128 = Constant::getNullValue(Int128Ty); |
| 126 | Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty); |
| 127 | EXPECT_TRUE(isa<ConstantFP>(X)); |
| 128 | } |
| 129 | |
Evgeniy Stepanov | 655578f | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 130 | TEST(ConstantsTest, PointerCast) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 131 | LLVMContext C; |
Evgeniy Stepanov | 655578f | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 132 | Type *Int8PtrTy = Type::getInt8PtrTy(C); |
| 133 | Type *Int32PtrTy = Type::getInt32PtrTy(C); |
| 134 | Type *Int64Ty = Type::getInt64Ty(C); |
| 135 | VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4); |
| 136 | VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4); |
| 137 | VectorType *Int64VecTy = VectorType::get(Int64Ty, 4); |
| 138 | |
| 139 | // ptrtoint i8* to i64 |
| 140 | EXPECT_EQ(Constant::getNullValue(Int64Ty), |
| 141 | ConstantExpr::getPointerCast( |
| 142 | Constant::getNullValue(Int8PtrTy), Int64Ty)); |
| 143 | |
| 144 | // bitcast i8* to i32* |
| 145 | EXPECT_EQ(Constant::getNullValue(Int32PtrTy), |
| 146 | ConstantExpr::getPointerCast( |
| 147 | Constant::getNullValue(Int8PtrTy), Int32PtrTy)); |
| 148 | |
| 149 | // ptrtoint <4 x i8*> to <4 x i64> |
| 150 | EXPECT_EQ(Constant::getNullValue(Int64VecTy), |
| 151 | ConstantExpr::getPointerCast( |
| 152 | Constant::getNullValue(Int8PtrVecTy), Int64VecTy)); |
| 153 | |
| 154 | // bitcast <4 x i8*> to <4 x i32*> |
| 155 | EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy), |
| 156 | ConstantExpr::getPointerCast( |
| 157 | Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy)); |
Matt Arsenault | bca8aba | 2016-05-21 00:14:04 +0000 | [diff] [blame] | 158 | |
| 159 | Type *Int32Ptr1Ty = Type::getInt32PtrTy(C, 1); |
| 160 | ConstantInt *K = ConstantInt::get(Type::getInt64Ty(C), 1234); |
| 161 | |
| 162 | // Make sure that addrspacecast of inttoptr is not folded away. |
| 163 | EXPECT_NE(K, |
| 164 | ConstantExpr::getAddrSpaceCast( |
| 165 | ConstantExpr::getIntToPtr(K, Int32PtrTy), Int32Ptr1Ty)); |
| 166 | EXPECT_NE(K, |
| 167 | ConstantExpr::getAddrSpaceCast( |
| 168 | ConstantExpr::getIntToPtr(K, Int32Ptr1Ty), Int32PtrTy)); |
| 169 | |
| 170 | Constant *NullInt32Ptr0 = Constant::getNullValue(Int32PtrTy); |
| 171 | Constant *NullInt32Ptr1 = Constant::getNullValue(Int32Ptr1Ty); |
| 172 | |
| 173 | // Make sure that addrspacecast of null is not folded away. |
| 174 | EXPECT_NE(Constant::getNullValue(Int32PtrTy), |
| 175 | ConstantExpr::getAddrSpaceCast(NullInt32Ptr0, Int32Ptr1Ty)); |
| 176 | |
| 177 | EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty), |
| 178 | ConstantExpr::getAddrSpaceCast(NullInt32Ptr1, Int32PtrTy)); |
Evgeniy Stepanov | 655578f | 2013-01-16 14:41:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Reid Kleckner | 816047d | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 181 | #define CHECK(x, y) \ |
| 182 | { \ |
| 183 | std::string __s; \ |
| 184 | raw_string_ostream __o(__s); \ |
| 185 | Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \ |
| 186 | __I->print(__o); \ |
| 187 | __I->deleteValue(); \ |
| 188 | __o.flush(); \ |
| 189 | EXPECT_EQ(std::string(" <badref> = " y), __s); \ |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST(ConstantsTest, AsInstructionsTest) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 193 | LLVMContext Context; |
| 194 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 195 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 196 | Type *Int64Ty = Type::getInt64Ty(Context); |
| 197 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 198 | Type *Int16Ty = Type::getInt16Ty(Context); |
| 199 | Type *Int1Ty = Type::getInt1Ty(Context); |
| 200 | Type *FloatTy = Type::getFloatTy(Context); |
| 201 | Type *DoubleTy = Type::getDoubleTy(Context); |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 202 | |
| 203 | Constant *Global = M->getOrInsertGlobal("dummy", |
| 204 | PointerType::getUnqual(Int32Ty)); |
| 205 | Constant *Global2 = M->getOrInsertGlobal("dummy2", |
| 206 | PointerType::getUnqual(Int32Ty)); |
| 207 | |
| 208 | Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty); |
| 209 | Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy); |
| 210 | Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy); |
| 211 | Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty); |
| 212 | Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty); |
| 213 | Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy); |
| 214 | Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2)); |
| 215 | |
| 216 | Constant *One = ConstantInt::get(Int32Ty, 1); |
Pawel Bylica | 8d00f2a | 2015-04-24 07:42:35 +0000 | [diff] [blame] | 217 | Constant *Two = ConstantInt::get(Int64Ty, 2); |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 218 | Constant *Big = ConstantInt::get(Context, APInt{256, uint64_t(-1), true}); |
Pawel Bylica | 59764b9 | 2015-04-27 09:30:49 +0000 | [diff] [blame] | 219 | Constant *Elt = ConstantInt::get(Int16Ty, 2015); |
| 220 | Constant *Undef16 = UndefValue::get(Int16Ty); |
| 221 | Constant *Undef64 = UndefValue::get(Int64Ty); |
| 222 | Constant *UndefV16 = UndefValue::get(P6->getType()); |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 223 | |
| 224 | #define P0STR "ptrtoint (i32** @dummy to i32)" |
| 225 | #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)" |
| 226 | #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)" |
| 227 | #define P3STR "ptrtoint (i32** @dummy to i1)" |
| 228 | #define P4STR "ptrtoint (i32** @dummy2 to i32)" |
| 229 | #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)" |
| 230 | #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)" |
| 231 | |
| 232 | CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR); |
| 233 | CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR); |
| 234 | CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1"); |
| 235 | CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR); |
| 236 | CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", " |
| 237 | P0STR); |
| 238 | CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", " |
| 239 | P0STR); |
| 240 | CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR); |
| 241 | CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR); |
| 242 | CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR); |
| 243 | CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR); |
| 244 | CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR); |
| 245 | CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR); |
| 246 | CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR); |
| 247 | CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR); |
| 248 | CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR); |
| 249 | CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR); |
| 250 | CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR); |
| 251 | CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR); |
| 252 | CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR); |
| 253 | CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR); |
| 254 | CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR); |
| 255 | CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR); |
| 256 | CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", " |
| 257 | P0STR); |
| 258 | CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR); |
| 259 | CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR); |
| 260 | CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR); |
| 261 | CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR); |
| 262 | |
| 263 | CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64"); |
| 264 | CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64"); |
| 265 | CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR |
| 266 | " to float"); |
| 267 | CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR |
| 268 | " to double"); |
| 269 | |
| 270 | CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR); |
| 271 | |
| 272 | CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR |
| 273 | ", i32 " P4STR); |
| 274 | CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR |
| 275 | ", " P4STR); |
| 276 | CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float " |
| 277 | P1STR ", " P5STR); |
| 278 | |
| 279 | std::vector<Constant*> V; |
| 280 | V.push_back(One); |
| 281 | // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP, |
| 282 | // not a normal one! |
| 283 | //CHECK(ConstantExpr::getGetElementPtr(Global, V, false), |
David Blaikie | 198d8ba | 2015-02-27 19:29:02 +0000 | [diff] [blame] | 284 | // "getelementptr i32*, i32** @dummy, i32 1"); |
David Blaikie | 19443c1 | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 285 | CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty), |
| 286 | Global, V), |
David Blaikie | 198d8ba | 2015-02-27 19:29:02 +0000 | [diff] [blame] | 287 | "getelementptr inbounds i32*, i32** @dummy, i32 1"); |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 288 | |
| 289 | CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> " |
| 290 | P6STR ", i32 1"); |
Pawel Bylica | 8d00f2a | 2015-04-24 07:42:35 +0000 | [diff] [blame] | 291 | |
Pawel Bylica | 59764b9 | 2015-04-27 09:30:49 +0000 | [diff] [blame] | 292 | EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two)); |
| 293 | EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big)); |
| 294 | EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64)); |
| 295 | |
| 296 | EXPECT_EQ(Elt, ConstantExpr::getExtractElement( |
| 297 | ConstantExpr::getInsertElement(P6, Elt, One), One)); |
| 298 | EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two)); |
| 299 | EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big)); |
| 300 | EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64)); |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 303 | #ifdef GTEST_HAS_DEATH_TEST |
| 304 | #ifndef NDEBUG |
| 305 | TEST(ConstantsTest, ReplaceWithConstantTest) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 306 | LLVMContext Context; |
| 307 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 308 | |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 309 | Type *Int32Ty = Type::getInt32Ty(Context); |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 310 | Constant *One = ConstantInt::get(Int32Ty, 1); |
| 311 | |
| 312 | Constant *Global = |
| 313 | M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty)); |
David Blaikie | 19443c1 | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 314 | Constant *GEP = ConstantExpr::getGetElementPtr( |
| 315 | PointerType::getUnqual(Int32Ty), Global, One); |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 316 | EXPECT_DEATH(Global->replaceAllUsesWith(GEP), |
| 317 | "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!"); |
| 318 | } |
Rafael Espindola | 27c076a | 2014-05-16 19:35:39 +0000 | [diff] [blame] | 319 | |
Rafael Espindola | e2b3744 | 2014-05-13 01:23:21 +0000 | [diff] [blame] | 320 | #endif |
| 321 | #endif |
| 322 | |
James Molloy | b9478c2 | 2012-11-17 17:56:30 +0000 | [diff] [blame] | 323 | #undef CHECK |
| 324 | |
Duncan P. N. Exon Smith | 7116af6 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 325 | TEST(ConstantsTest, ConstantArrayReplaceWithConstant) { |
| 326 | LLVMContext Context; |
| 327 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
| 328 | |
| 329 | Type *IntTy = Type::getInt8Ty(Context); |
| 330 | ArrayType *ArrayTy = ArrayType::get(IntTy, 2); |
| 331 | Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0), |
| 332 | ConstantInt::get(IntTy, 1)}; |
| 333 | Constant *A01 = ConstantArray::get(ArrayTy, A01Vals); |
| 334 | |
| 335 | Constant *Global = new GlobalVariable(*M, IntTy, false, |
| 336 | GlobalValue::ExternalLinkage, nullptr); |
| 337 | Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy); |
| 338 | Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt}; |
| 339 | Constant *A0G = ConstantArray::get(ArrayTy, A0GVals); |
| 340 | ASSERT_NE(A01, A0G); |
| 341 | |
| 342 | GlobalVariable *RefArray = |
| 343 | new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G); |
| 344 | ASSERT_EQ(A0G, RefArray->getInitializer()); |
| 345 | |
| 346 | GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1)); |
| 347 | ASSERT_EQ(A01, RefArray->getInitializer()); |
| 348 | } |
| 349 | |
Duncan P. N. Exon Smith | b03916a | 2014-08-19 20:03:35 +0000 | [diff] [blame] | 350 | TEST(ConstantsTest, ConstantExprReplaceWithConstant) { |
| 351 | LLVMContext Context; |
| 352 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
| 353 | |
| 354 | Type *IntTy = Type::getInt8Ty(Context); |
| 355 | Constant *G1 = new GlobalVariable(*M, IntTy, false, |
| 356 | GlobalValue::ExternalLinkage, nullptr); |
| 357 | Constant *G2 = new GlobalVariable(*M, IntTy, false, |
| 358 | GlobalValue::ExternalLinkage, nullptr); |
| 359 | ASSERT_NE(G1, G2); |
| 360 | |
| 361 | Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy); |
| 362 | Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy); |
| 363 | ASSERT_NE(Int1, Int2); |
| 364 | |
| 365 | GlobalVariable *Ref = |
| 366 | new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1); |
| 367 | ASSERT_EQ(Int1, Ref->getInitializer()); |
| 368 | |
| 369 | G1->replaceAllUsesWith(G2); |
| 370 | ASSERT_EQ(Int2, Ref->getInitializer()); |
| 371 | } |
| 372 | |
Duncan P. N. Exon Smith | 8db4ddb | 2014-08-19 21:18:21 +0000 | [diff] [blame] | 373 | TEST(ConstantsTest, GEPReplaceWithConstant) { |
| 374 | LLVMContext Context; |
| 375 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
| 376 | |
| 377 | Type *IntTy = Type::getInt32Ty(Context); |
David Blaikie | 2d35348 | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 378 | Type *PtrTy = PointerType::get(IntTy, 0); |
Duncan P. N. Exon Smith | 8db4ddb | 2014-08-19 21:18:21 +0000 | [diff] [blame] | 379 | auto *C1 = ConstantInt::get(IntTy, 1); |
| 380 | auto *Placeholder = new GlobalVariable( |
| 381 | *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr); |
David Blaikie | 19443c1 | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 382 | auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1); |
Duncan P. N. Exon Smith | 8db4ddb | 2014-08-19 21:18:21 +0000 | [diff] [blame] | 383 | ASSERT_EQ(GEP->getOperand(0), Placeholder); |
| 384 | |
| 385 | auto *Ref = |
| 386 | new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP); |
| 387 | ASSERT_EQ(GEP, Ref->getInitializer()); |
| 388 | |
| 389 | auto *Global = new GlobalVariable(*M, PtrTy, false, |
| 390 | GlobalValue::ExternalLinkage, nullptr); |
David Blaikie | 2d35348 | 2015-09-14 18:01:59 +0000 | [diff] [blame] | 391 | auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage, |
Duncan P. N. Exon Smith | 8db4ddb | 2014-08-19 21:18:21 +0000 | [diff] [blame] | 392 | "alias", Global, M.get()); |
| 393 | Placeholder->replaceAllUsesWith(Alias); |
| 394 | ASSERT_EQ(GEP, Ref->getInitializer()); |
| 395 | ASSERT_EQ(GEP->getOperand(0), Alias); |
| 396 | } |
| 397 | |
Rafael Espindola | 38ce6c7 | 2015-02-23 21:51:06 +0000 | [diff] [blame] | 398 | TEST(ConstantsTest, AliasCAPI) { |
| 399 | LLVMContext Context; |
| 400 | SMDiagnostic Error; |
| 401 | std::unique_ptr<Module> M = |
| 402 | parseAssemblyString("@g = global i32 42", Error, Context); |
| 403 | GlobalVariable *G = M->getGlobalVariable("g"); |
| 404 | Type *I16Ty = Type::getInt16Ty(Context); |
| 405 | Type *I16PTy = PointerType::get(I16Ty, 0); |
| 406 | Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy); |
| 407 | LLVMValueRef AliasRef = |
| 408 | LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a"); |
| 409 | ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee); |
| 410 | } |
| 411 | |
Justin Bogner | e32f0e2 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 412 | static std::string getNameOfType(Type *T) { |
| 413 | std::string S; |
| 414 | raw_string_ostream RSOS(S); |
| 415 | T->print(RSOS); |
| 416 | return S; |
| 417 | } |
| 418 | |
Justin Bogner | 4381284 | 2015-12-09 21:21:07 +0000 | [diff] [blame] | 419 | TEST(ConstantsTest, BuildConstantDataArrays) { |
| 420 | LLVMContext Context; |
| 421 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
| 422 | |
| 423 | for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context), |
| 424 | Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) { |
| 425 | ArrayType *ArrayTy = ArrayType::get(T, 2); |
| 426 | Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)}; |
| 427 | Constant *CDV = ConstantArray::get(ArrayTy, Vals); |
| 428 | ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr) |
| 429 | << " T = " << getNameOfType(T); |
| 430 | } |
| 431 | |
| 432 | for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context), |
| 433 | Type::getDoubleTy(Context)}) { |
| 434 | ArrayType *ArrayTy = ArrayType::get(T, 2); |
| 435 | Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)}; |
| 436 | Constant *CDV = ConstantArray::get(ArrayTy, Vals); |
| 437 | ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr) |
| 438 | << " T = " << getNameOfType(T); |
| 439 | } |
| 440 | } |
| 441 | |
Justin Bogner | e32f0e2 | 2015-12-08 03:01:16 +0000 | [diff] [blame] | 442 | TEST(ConstantsTest, BuildConstantDataVectors) { |
| 443 | LLVMContext Context; |
| 444 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
| 445 | |
| 446 | for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context), |
| 447 | Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) { |
| 448 | Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)}; |
| 449 | Constant *CDV = ConstantVector::get(Vals); |
| 450 | ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr) |
| 451 | << " T = " << getNameOfType(T); |
| 452 | } |
| 453 | |
| 454 | for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context), |
| 455 | Type::getDoubleTy(Context)}) { |
| 456 | Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)}; |
| 457 | Constant *CDV = ConstantVector::get(Vals); |
| 458 | ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr) |
| 459 | << " T = " << getNameOfType(T); |
| 460 | } |
| 461 | } |
| 462 | |
David Majnemer | 1d7a679 | 2015-12-14 19:30:32 +0000 | [diff] [blame] | 463 | TEST(ConstantsTest, BitcastToGEP) { |
| 464 | LLVMContext Context; |
| 465 | std::unique_ptr<Module> M(new Module("MyModule", Context)); |
| 466 | |
| 467 | auto *i32 = Type::getInt32Ty(Context); |
| 468 | auto *U = StructType::create(Context, "Unsized"); |
| 469 | Type *EltTys[] = {i32, U}; |
| 470 | auto *S = StructType::create(EltTys); |
| 471 | |
| 472 | auto *G = new GlobalVariable(*M, S, false, |
| 473 | GlobalValue::ExternalLinkage, nullptr); |
| 474 | auto *PtrTy = PointerType::get(i32, 0); |
| 475 | auto *C = ConstantExpr::getBitCast(G, PtrTy); |
Craig Topper | 412df0b | 2018-05-05 01:57:00 +0000 | [diff] [blame] | 476 | ASSERT_EQ(cast<ConstantExpr>(C)->getOpcode(), Instruction::BitCast); |
David Majnemer | 1d7a679 | 2015-12-14 19:30:32 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Misha Brukman | 2e73426 | 2009-03-24 21:36:09 +0000 | [diff] [blame] | 479 | } // end anonymous namespace |
| 480 | } // end namespace llvm |