1 //===- llvm/unittest/IR/ConstantsTest.cpp - Constants 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 //===----------------------------------------------------------------------===// 8 9 #include "llvm/IR/Constants.h" 10 #include "llvm-c/Core.h" 11 #include "llvm/AsmParser/Parser.h" 12 #include "llvm/IR/DerivedTypes.h" 13 #include "llvm/IR/InstrTypes.h" 14 #include "llvm/IR/Instruction.h" 15 #include "llvm/IR/LLVMContext.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/Support/SourceMgr.h" 18 #include "gtest/gtest.h" 19 20 namespace llvm { 21 namespace { 22 23 TEST(ConstantsTest, Integer_i1) { 24 LLVMContext Context; 25 IntegerType *Int1 = IntegerType::get(Context, 1); 26 Constant *One = ConstantInt::get(Int1, 1, true); 27 Constant *Zero = ConstantInt::get(Int1, 0); 28 Constant *NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true); 29 EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1)); 30 Constant *Poison = PoisonValue::get(Int1); 31 32 // Input: @b = constant i1 add(i1 1 , i1 1) 33 // Output: @b = constant i1 false 34 EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One)); 35 36 // @c = constant i1 add(i1 -1, i1 1) 37 // @c = constant i1 false 38 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One)); 39 40 // @d = constant i1 add(i1 -1, i1 -1) 41 // @d = constant i1 false 42 EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne)); 43 44 // @e = constant i1 sub(i1 -1, i1 1) 45 // @e = constant i1 false 46 EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One)); 47 48 // @f = constant i1 sub(i1 1 , i1 -1) 49 // @f = constant i1 false 50 EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne)); 51 52 // @g = constant i1 sub(i1 1 , i1 1) 53 // @g = constant i1 false 54 EXPECT_EQ(Zero, ConstantExpr::getSub(One, One)); 55 56 // @h = constant i1 shl(i1 1 , i1 1) ; poison 57 // @h = constant i1 poison 58 EXPECT_EQ(Poison, ConstantExpr::getShl(One, One)); 59 60 // @i = constant i1 shl(i1 1 , i1 0) 61 // @i = constant i1 true 62 EXPECT_EQ(One, ConstantExpr::getShl(One, Zero)); 63 64 // @j = constant i1 lshr(i1 1, i1 1) ; poison 65 // @j = constant i1 poison 66 EXPECT_EQ(Poison, ConstantExpr::getLShr(One, One)); 67 68 // @m = constant i1 ashr(i1 1, i1 1) ; poison 69 // @m = constant i1 poison 70 EXPECT_EQ(Poison, ConstantExpr::getAShr(One, One)); 71 72 // @n = constant i1 mul(i1 -1, i1 1) 73 // @n = constant i1 true 74 EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One)); 75 76 // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow 77 // @o = constant i1 true 78 EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One)); 79 80 // @p = constant i1 sdiv(i1 1 , i1 -1); overflow 81 // @p = constant i1 true 82 EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne)); 83 84 // @q = constant i1 udiv(i1 -1, i1 1) 85 // @q = constant i1 true 86 EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One)); 87 88 // @r = constant i1 udiv(i1 1, i1 -1) 89 // @r = constant i1 true 90 EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne)); 91 92 // @s = constant i1 srem(i1 -1, i1 1) ; overflow 93 // @s = constant i1 false 94 EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One)); 95 96 // @t = constant i1 urem(i1 -1, i1 1) 97 // @t = constant i1 false 98 EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One)); 99 100 // @u = constant i1 srem(i1 1, i1 -1) ; overflow 101 // @u = constant i1 false 102 EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne)); 103 } 104 105 TEST(ConstantsTest, IntSigns) { 106 LLVMContext Context; 107 IntegerType *Int8Ty = Type::getInt8Ty(Context); 108 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue()); 109 EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue()); 110 EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue()); 111 EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue()); 112 EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue()); 113 EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue()); 114 115 // Overflow is handled by truncation. 116 EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue()); 117 } 118 119 TEST(ConstantsTest, FP128Test) { 120 LLVMContext Context; 121 Type *FP128Ty = Type::getFP128Ty(Context); 122 123 IntegerType *Int128Ty = Type::getIntNTy(Context, 128); 124 Constant *Zero128 = Constant::getNullValue(Int128Ty); 125 Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty); 126 EXPECT_TRUE(isa<ConstantFP>(X)); 127 } 128 129 TEST(ConstantsTest, PointerCast) { 130 LLVMContext C; 131 Type *Int8PtrTy = Type::getInt8PtrTy(C); 132 Type *Int32PtrTy = Type::getInt32PtrTy(C); 133 Type *Int64Ty = Type::getInt64Ty(C); 134 VectorType *Int8PtrVecTy = FixedVectorType::get(Int8PtrTy, 4); 135 VectorType *Int32PtrVecTy = FixedVectorType::get(Int32PtrTy, 4); 136 VectorType *Int64VecTy = FixedVectorType::get(Int64Ty, 4); 137 138 // ptrtoint i8* to i64 139 EXPECT_EQ( 140 Constant::getNullValue(Int64Ty), 141 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy), Int64Ty)); 142 143 // bitcast i8* to i32* 144 EXPECT_EQ(Constant::getNullValue(Int32PtrTy), 145 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrTy), 146 Int32PtrTy)); 147 148 // ptrtoint <4 x i8*> to <4 x i64> 149 EXPECT_EQ(Constant::getNullValue(Int64VecTy), 150 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy), 151 Int64VecTy)); 152 153 // bitcast <4 x i8*> to <4 x i32*> 154 EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy), 155 ConstantExpr::getPointerCast(Constant::getNullValue(Int8PtrVecTy), 156 Int32PtrVecTy)); 157 158 Type *Int32Ptr1Ty = Type::getInt32PtrTy(C, 1); 159 ConstantInt *K = ConstantInt::get(Type::getInt64Ty(C), 1234); 160 161 // Make sure that addrspacecast of inttoptr is not folded away. 162 EXPECT_NE(K, ConstantExpr::getAddrSpaceCast( 163 ConstantExpr::getIntToPtr(K, Int32PtrTy), Int32Ptr1Ty)); 164 EXPECT_NE(K, ConstantExpr::getAddrSpaceCast( 165 ConstantExpr::getIntToPtr(K, Int32Ptr1Ty), Int32PtrTy)); 166 167 Constant *NullInt32Ptr0 = Constant::getNullValue(Int32PtrTy); 168 Constant *NullInt32Ptr1 = Constant::getNullValue(Int32Ptr1Ty); 169 170 // Make sure that addrspacecast of null is not folded away. 171 EXPECT_NE(Constant::getNullValue(Int32PtrTy), 172 ConstantExpr::getAddrSpaceCast(NullInt32Ptr0, Int32Ptr1Ty)); 173 174 EXPECT_NE(Constant::getNullValue(Int32Ptr1Ty), 175 ConstantExpr::getAddrSpaceCast(NullInt32Ptr1, Int32PtrTy)); 176 } 177 178 #define CHECK(x, y) \ 179 { \ 180 std::string __s; \ 181 raw_string_ostream __o(__s); \ 182 Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction(); \ 183 __I->print(__o); \ 184 __I->deleteValue(); \ 185 __o.flush(); \ 186 EXPECT_EQ(std::string(" <badref> = " y), __s); \ 187 } 188 189 TEST(ConstantsTest, AsInstructionsTest) { 190 LLVMContext Context; 191 std::unique_ptr<Module> M(new Module("MyModule", Context)); 192 193 Type *Int64Ty = Type::getInt64Ty(Context); 194 Type *Int32Ty = Type::getInt32Ty(Context); 195 Type *Int16Ty = Type::getInt16Ty(Context); 196 Type *Int1Ty = Type::getInt1Ty(Context); 197 Type *FloatTy = Type::getFloatTy(Context); 198 Type *DoubleTy = Type::getDoubleTy(Context); 199 200 Constant *Global = 201 M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty)); 202 Constant *Global2 = 203 M->getOrInsertGlobal("dummy2", PointerType::getUnqual(Int32Ty)); 204 205 Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty); 206 Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy); 207 Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy); 208 Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty); 209 Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty); 210 Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy); 211 Constant *P6 = ConstantExpr::getBitCast(P4, FixedVectorType::get(Int16Ty, 2)); 212 213 Constant *One = ConstantInt::get(Int32Ty, 1); 214 Constant *Two = ConstantInt::get(Int64Ty, 2); 215 Constant *Big = ConstantInt::get(Context, APInt{256, uint64_t(-1), true}); 216 Constant *Elt = ConstantInt::get(Int16Ty, 2015); 217 Constant *Poison16 = PoisonValue::get(Int16Ty); 218 Constant *Undef64 = UndefValue::get(Int64Ty); 219 Constant *PoisonV16 = PoisonValue::get(P6->getType()); 220 221 #define P0STR "ptrtoint (i32** @dummy to i32)" 222 #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)" 223 #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)" 224 #define P3STR "ptrtoint (i32** @dummy to i1)" 225 #define P4STR "ptrtoint (i32** @dummy2 to i32)" 226 #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)" 227 #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)" 228 229 CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR); 230 CHECK(ConstantExpr::getFNeg(P1), "fneg float " P1STR); 231 CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1"); 232 CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR); 233 CHECK(ConstantExpr::getAdd(P0, P0, false, true), 234 "add nsw i32 " P0STR ", " P0STR); 235 CHECK(ConstantExpr::getAdd(P0, P0, true, true), 236 "add nuw nsw i32 " P0STR ", " P0STR); 237 CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR); 238 CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR); 239 CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR); 240 CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR); 241 CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR); 242 CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR); 243 CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR); 244 CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR); 245 CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR); 246 CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR); 247 CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR); 248 CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR); 249 CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR); 250 CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR); 251 CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR); 252 CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR); 253 CHECK(ConstantExpr::getShl(P0, P0, false, true), 254 "shl nsw i32 " P0STR ", " P0STR); 255 CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR); 256 CHECK(ConstantExpr::getLShr(P0, P0, true), 257 "lshr exact i32 " P0STR ", " P0STR); 258 CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR); 259 CHECK(ConstantExpr::getAShr(P0, P0, true), 260 "ashr exact i32 " P0STR ", " P0STR); 261 262 CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64"); 263 CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64"); 264 CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), 265 "fptrunc double " P2STR " to float"); 266 CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), 267 "fpext float " P1STR " to double"); 268 269 CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR); 270 271 CHECK(ConstantExpr::getSelect(P3, P0, P4), 272 "select i1 " P3STR ", i32 " P0STR ", i32 " P4STR); 273 CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), 274 "icmp eq i32 " P0STR ", " P4STR); 275 CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), 276 "fcmp ult float " P1STR ", " P5STR); 277 278 std::vector<Constant *> V; 279 V.push_back(One); 280 // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP, 281 // not a normal one! 282 // CHECK(ConstantExpr::getGetElementPtr(Global, V, false), 283 // "getelementptr i32*, i32** @dummy, i32 1"); 284 CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty), 285 Global, V), 286 "getelementptr inbounds i32*, i32** @dummy, i32 1"); 287 288 CHECK(ConstantExpr::getExtractElement(P6, One), 289 "extractelement <2 x i16> " P6STR ", i32 1"); 290 291 EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Two)); 292 EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Big)); 293 EXPECT_EQ(Poison16, ConstantExpr::getExtractElement(P6, Undef64)); 294 295 EXPECT_EQ(Elt, ConstantExpr::getExtractElement( 296 ConstantExpr::getInsertElement(P6, Elt, One), One)); 297 EXPECT_EQ(PoisonV16, ConstantExpr::getInsertElement(P6, Elt, Two)); 298 EXPECT_EQ(PoisonV16, ConstantExpr::getInsertElement(P6, Elt, Big)); 299 EXPECT_EQ(PoisonV16, ConstantExpr::getInsertElement(P6, Elt, Undef64)); 300 } 301 302 #ifdef GTEST_HAS_DEATH_TEST 303 #ifndef NDEBUG 304 TEST(ConstantsTest, ReplaceWithConstantTest) { 305 LLVMContext Context; 306 std::unique_ptr<Module> M(new Module("MyModule", Context)); 307 308 Type *Int32Ty = Type::getInt32Ty(Context); 309 Constant *One = ConstantInt::get(Int32Ty, 1); 310 311 Constant *Global = 312 M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty)); 313 Constant *GEP = ConstantExpr::getGetElementPtr( 314 PointerType::getUnqual(Int32Ty), Global, One); 315 EXPECT_DEATH(Global->replaceAllUsesWith(GEP), 316 "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!"); 317 } 318 319 #endif 320 #endif 321 322 #undef CHECK 323 324 TEST(ConstantsTest, ConstantArrayReplaceWithConstant) { 325 LLVMContext Context; 326 std::unique_ptr<Module> M(new Module("MyModule", Context)); 327 328 Type *IntTy = Type::getInt8Ty(Context); 329 ArrayType *ArrayTy = ArrayType::get(IntTy, 2); 330 Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0), 331 ConstantInt::get(IntTy, 1)}; 332 Constant *A01 = ConstantArray::get(ArrayTy, A01Vals); 333 334 Constant *Global = new GlobalVariable(*M, IntTy, false, 335 GlobalValue::ExternalLinkage, nullptr); 336 Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy); 337 Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt}; 338 Constant *A0G = ConstantArray::get(ArrayTy, A0GVals); 339 ASSERT_NE(A01, A0G); 340 341 GlobalVariable *RefArray = 342 new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G); 343 ASSERT_EQ(A0G, RefArray->getInitializer()); 344 345 GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1)); 346 ASSERT_EQ(A01, RefArray->getInitializer()); 347 } 348 349 TEST(ConstantsTest, ConstantExprReplaceWithConstant) { 350 LLVMContext Context; 351 std::unique_ptr<Module> M(new Module("MyModule", Context)); 352 353 Type *IntTy = Type::getInt8Ty(Context); 354 Constant *G1 = new GlobalVariable(*M, IntTy, false, 355 GlobalValue::ExternalLinkage, nullptr); 356 Constant *G2 = new GlobalVariable(*M, IntTy, false, 357 GlobalValue::ExternalLinkage, nullptr); 358 ASSERT_NE(G1, G2); 359 360 Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy); 361 Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy); 362 ASSERT_NE(Int1, Int2); 363 364 GlobalVariable *Ref = 365 new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1); 366 ASSERT_EQ(Int1, Ref->getInitializer()); 367 368 G1->replaceAllUsesWith(G2); 369 ASSERT_EQ(Int2, Ref->getInitializer()); 370 } 371 372 TEST(ConstantsTest, GEPReplaceWithConstant) { 373 LLVMContext Context; 374 std::unique_ptr<Module> M(new Module("MyModule", Context)); 375 376 Type *IntTy = Type::getInt32Ty(Context); 377 Type *PtrTy = PointerType::get(IntTy, 0); 378 auto *C1 = ConstantInt::get(IntTy, 1); 379 auto *Placeholder = new GlobalVariable( 380 *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr); 381 auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1); 382 ASSERT_EQ(GEP->getOperand(0), Placeholder); 383 384 auto *Ref = 385 new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP); 386 ASSERT_EQ(GEP, Ref->getInitializer()); 387 388 auto *Global = new GlobalVariable(*M, IntTy, false, 389 GlobalValue::ExternalLinkage, nullptr); 390 auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage, 391 "alias", Global, M.get()); 392 Placeholder->replaceAllUsesWith(Alias); 393 ASSERT_EQ(GEP, Ref->getInitializer()); 394 ASSERT_EQ(GEP->getOperand(0), Alias); 395 } 396 397 TEST(ConstantsTest, AliasCAPI) { 398 LLVMContext Context; 399 SMDiagnostic Error; 400 std::unique_ptr<Module> M = 401 parseAssemblyString("@g = global i32 42", Error, Context); 402 GlobalVariable *G = M->getGlobalVariable("g"); 403 Type *I16Ty = Type::getInt16Ty(Context); 404 Type *I16PTy = PointerType::get(I16Ty, 0); 405 Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy); 406 LLVMValueRef AliasRef = 407 LLVMAddAlias2(wrap(M.get()), wrap(I16Ty), 0, wrap(Aliasee), "a"); 408 ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee); 409 } 410 411 static std::string getNameOfType(Type *T) { 412 std::string S; 413 raw_string_ostream RSOS(S); 414 T->print(RSOS); 415 return S; 416 } 417 418 TEST(ConstantsTest, BuildConstantDataArrays) { 419 LLVMContext Context; 420 421 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context), 422 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) { 423 ArrayType *ArrayTy = ArrayType::get(T, 2); 424 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)}; 425 Constant *CA = ConstantArray::get(ArrayTy, Vals); 426 ASSERT_TRUE(isa<ConstantDataArray>(CA)) << " T = " << getNameOfType(T); 427 auto *CDA = cast<ConstantDataArray>(CA); 428 Constant *CA2 = ConstantDataArray::getRaw( 429 CDA->getRawDataValues(), CDA->getNumElements(), CDA->getElementType()); 430 ASSERT_TRUE(CA == CA2) << " T = " << getNameOfType(T); 431 } 432 433 for (Type *T : {Type::getHalfTy(Context), Type::getBFloatTy(Context), 434 Type::getFloatTy(Context), Type::getDoubleTy(Context)}) { 435 ArrayType *ArrayTy = ArrayType::get(T, 2); 436 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)}; 437 Constant *CA = ConstantArray::get(ArrayTy, Vals); 438 ASSERT_TRUE(isa<ConstantDataArray>(CA)) << " T = " << getNameOfType(T); 439 auto *CDA = cast<ConstantDataArray>(CA); 440 Constant *CA2 = ConstantDataArray::getRaw( 441 CDA->getRawDataValues(), CDA->getNumElements(), CDA->getElementType()); 442 ASSERT_TRUE(CA == CA2) << " T = " << getNameOfType(T); 443 } 444 } 445 446 TEST(ConstantsTest, BuildConstantDataVectors) { 447 LLVMContext Context; 448 449 for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context), 450 Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) { 451 Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)}; 452 Constant *CV = ConstantVector::get(Vals); 453 ASSERT_TRUE(isa<ConstantDataVector>(CV)) << " T = " << getNameOfType(T); 454 auto *CDV = cast<ConstantDataVector>(CV); 455 Constant *CV2 = ConstantDataVector::getRaw( 456 CDV->getRawDataValues(), CDV->getNumElements(), CDV->getElementType()); 457 ASSERT_TRUE(CV == CV2) << " T = " << getNameOfType(T); 458 } 459 460 for (Type *T : {Type::getHalfTy(Context), Type::getBFloatTy(Context), 461 Type::getFloatTy(Context), Type::getDoubleTy(Context)}) { 462 Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)}; 463 Constant *CV = ConstantVector::get(Vals); 464 ASSERT_TRUE(isa<ConstantDataVector>(CV)) << " T = " << getNameOfType(T); 465 auto *CDV = cast<ConstantDataVector>(CV); 466 Constant *CV2 = ConstantDataVector::getRaw( 467 CDV->getRawDataValues(), CDV->getNumElements(), CDV->getElementType()); 468 ASSERT_TRUE(CV == CV2) << " T = " << getNameOfType(T); 469 } 470 } 471 472 void bitcastToGEPHelper(bool useOpaquePointers) { 473 LLVMContext Context; 474 std::unique_ptr<Module> M(new Module("MyModule", Context)); 475 if (useOpaquePointers) 476 Context.enableOpaquePointers(); 477 478 auto *i32 = Type::getInt32Ty(Context); 479 auto *U = StructType::create(Context, "Unsized"); 480 Type *EltTys[] = {i32, U}; 481 auto *S = StructType::create(EltTys); 482 483 auto *G = 484 new GlobalVariable(*M, S, false, GlobalValue::ExternalLinkage, nullptr); 485 auto *PtrTy = PointerType::get(i32, 0); 486 auto *C = ConstantExpr::getBitCast(G, PtrTy); 487 if (Context.supportsTypedPointers()) { 488 EXPECT_EQ(cast<ConstantExpr>(C)->getOpcode(), Instruction::BitCast); 489 } else { 490 /* With opaque pointers, no cast is necessary. */ 491 EXPECT_EQ(C, G); 492 } 493 } 494 495 TEST(ConstantsTest, BitcastToGEP) { 496 bitcastToGEPHelper(true); 497 bitcastToGEPHelper(false); 498 } 499 500 bool foldFuncPtrAndConstToNull(LLVMContext &Context, Module *TheModule, 501 uint64_t AndValue, 502 MaybeAlign FunctionAlign = llvm::None) { 503 Type *VoidType(Type::getVoidTy(Context)); 504 FunctionType *FuncType(FunctionType::get(VoidType, false)); 505 Function *Func( 506 Function::Create(FuncType, GlobalValue::ExternalLinkage, "", TheModule)); 507 508 if (FunctionAlign) 509 Func->setAlignment(*FunctionAlign); 510 511 IntegerType *ConstantIntType(Type::getInt32Ty(Context)); 512 ConstantInt *TheConstant(ConstantInt::get(ConstantIntType, AndValue)); 513 514 Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Func, ConstantIntType)); 515 516 bool Result = 517 ConstantExpr::get(Instruction::And, TheConstantExpr, TheConstant) 518 ->isNullValue(); 519 520 if (!TheModule) { 521 // If the Module exists then it will delete the Function. 522 delete Func; 523 } 524 525 return Result; 526 } 527 528 TEST(ConstantsTest, FoldFunctionPtrAlignUnknownAnd2) { 529 LLVMContext Context; 530 Module TheModule("TestModule", Context); 531 // When the DataLayout doesn't specify a function pointer alignment we 532 // assume in this case that it is 4 byte aligned. This is a bug but we can't 533 // fix it directly because it causes a code size regression on X86. 534 // FIXME: This test should be changed once existing targets have 535 // appropriate defaults. See associated FIXME in ConstantFoldBinaryInstruction 536 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2)); 537 } 538 539 TEST(ConstantsTest, DontFoldFunctionPtrAlignUnknownAnd4) { 540 LLVMContext Context; 541 Module TheModule("TestModule", Context); 542 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 4)); 543 } 544 545 TEST(ConstantsTest, FoldFunctionPtrAlign4) { 546 LLVMContext Context; 547 Module TheModule("TestModule", Context); 548 const char *AlignmentStrings[] = {"Fi32", "Fn32"}; 549 550 for (unsigned AndValue = 1; AndValue <= 2; ++AndValue) { 551 for (const char *AlignmentString : AlignmentStrings) { 552 TheModule.setDataLayout(AlignmentString); 553 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, AndValue)); 554 } 555 } 556 } 557 558 TEST(ConstantsTest, DontFoldFunctionPtrAlign1) { 559 LLVMContext Context; 560 Module TheModule("TestModule", Context); 561 const char *AlignmentStrings[] = {"Fi8", "Fn8"}; 562 563 for (const char *AlignmentString : AlignmentStrings) { 564 TheModule.setDataLayout(AlignmentString); 565 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2)); 566 } 567 } 568 569 TEST(ConstantsTest, FoldFunctionAlign4PtrAlignMultiple) { 570 LLVMContext Context; 571 Module TheModule("TestModule", Context); 572 TheModule.setDataLayout("Fn8"); 573 ASSERT_TRUE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4))); 574 } 575 576 TEST(ConstantsTest, DontFoldFunctionAlign4PtrAlignIndependent) { 577 LLVMContext Context; 578 Module TheModule("TestModule", Context); 579 TheModule.setDataLayout("Fi8"); 580 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, &TheModule, 2, Align(4))); 581 } 582 583 TEST(ConstantsTest, DontFoldFunctionPtrIfNoModule) { 584 LLVMContext Context; 585 // Even though the function is explicitly 4 byte aligned, in the absence of a 586 // DataLayout we can't assume that the function pointer is aligned. 587 ASSERT_FALSE(foldFuncPtrAndConstToNull(Context, nullptr, 2, Align(4))); 588 } 589 590 TEST(ConstantsTest, FoldGlobalVariablePtr) { 591 LLVMContext Context; 592 593 IntegerType *IntType(Type::getInt32Ty(Context)); 594 595 std::unique_ptr<GlobalVariable> Global( 596 new GlobalVariable(IntType, true, GlobalValue::ExternalLinkage)); 597 598 Global->setAlignment(Align(4)); 599 600 ConstantInt *TheConstant(ConstantInt::get(IntType, 2)); 601 602 Constant *TheConstantExpr(ConstantExpr::getPtrToInt(Global.get(), IntType)); 603 604 ASSERT_TRUE(ConstantExpr::get(Instruction::And, TheConstantExpr, TheConstant) 605 ->isNullValue()); 606 } 607 608 // Check that containsUndefOrPoisonElement and containsPoisonElement is working 609 // great 610 611 TEST(ConstantsTest, containsUndefElemTest) { 612 LLVMContext Context; 613 614 Type *Int32Ty = Type::getInt32Ty(Context); 615 Constant *CU = UndefValue::get(Int32Ty); 616 Constant *CP = PoisonValue::get(Int32Ty); 617 Constant *C1 = ConstantInt::get(Int32Ty, 1); 618 Constant *C2 = ConstantInt::get(Int32Ty, 2); 619 620 { 621 Constant *V1 = ConstantVector::get({C1, C2}); 622 EXPECT_FALSE(V1->containsUndefOrPoisonElement()); 623 EXPECT_FALSE(V1->containsPoisonElement()); 624 } 625 626 { 627 Constant *V2 = ConstantVector::get({C1, CU}); 628 EXPECT_TRUE(V2->containsUndefOrPoisonElement()); 629 EXPECT_FALSE(V2->containsPoisonElement()); 630 } 631 632 { 633 Constant *V3 = ConstantVector::get({C1, CP}); 634 EXPECT_TRUE(V3->containsUndefOrPoisonElement()); 635 EXPECT_TRUE(V3->containsPoisonElement()); 636 } 637 638 { 639 Constant *V4 = ConstantVector::get({CU, CP}); 640 EXPECT_TRUE(V4->containsUndefOrPoisonElement()); 641 EXPECT_TRUE(V4->containsPoisonElement()); 642 } 643 } 644 645 // Check that undefined elements in vector constants are matched 646 // correctly for both integer and floating-point types. Just don't 647 // crash on vectors of pointers (could be handled?). 648 649 TEST(ConstantsTest, isElementWiseEqual) { 650 LLVMContext Context; 651 652 Type *Int32Ty = Type::getInt32Ty(Context); 653 Constant *CU = UndefValue::get(Int32Ty); 654 Constant *C1 = ConstantInt::get(Int32Ty, 1); 655 Constant *C2 = ConstantInt::get(Int32Ty, 2); 656 657 Constant *C1211 = ConstantVector::get({C1, C2, C1, C1}); 658 Constant *C12U1 = ConstantVector::get({C1, C2, CU, C1}); 659 Constant *C12U2 = ConstantVector::get({C1, C2, CU, C2}); 660 Constant *C12U21 = ConstantVector::get({C1, C2, CU, C2, C1}); 661 662 EXPECT_TRUE(C1211->isElementWiseEqual(C12U1)); 663 EXPECT_TRUE(C12U1->isElementWiseEqual(C1211)); 664 EXPECT_FALSE(C12U2->isElementWiseEqual(C12U1)); 665 EXPECT_FALSE(C12U1->isElementWiseEqual(C12U2)); 666 EXPECT_FALSE(C12U21->isElementWiseEqual(C12U2)); 667 668 Type *FltTy = Type::getFloatTy(Context); 669 Constant *CFU = UndefValue::get(FltTy); 670 Constant *CF1 = ConstantFP::get(FltTy, 1.0); 671 Constant *CF2 = ConstantFP::get(FltTy, 2.0); 672 673 Constant *CF1211 = ConstantVector::get({CF1, CF2, CF1, CF1}); 674 Constant *CF12U1 = ConstantVector::get({CF1, CF2, CFU, CF1}); 675 Constant *CF12U2 = ConstantVector::get({CF1, CF2, CFU, CF2}); 676 Constant *CFUU1U = ConstantVector::get({CFU, CFU, CF1, CFU}); 677 678 EXPECT_TRUE(CF1211->isElementWiseEqual(CF12U1)); 679 EXPECT_TRUE(CF12U1->isElementWiseEqual(CF1211)); 680 EXPECT_TRUE(CFUU1U->isElementWiseEqual(CF12U1)); 681 EXPECT_FALSE(CF12U2->isElementWiseEqual(CF12U1)); 682 EXPECT_FALSE(CF12U1->isElementWiseEqual(CF12U2)); 683 684 PointerType *PtrTy = Type::getInt8PtrTy(Context); 685 Constant *CPU = UndefValue::get(PtrTy); 686 Constant *CP0 = ConstantPointerNull::get(PtrTy); 687 688 Constant *CP0000 = ConstantVector::get({CP0, CP0, CP0, CP0}); 689 Constant *CP00U0 = ConstantVector::get({CP0, CP0, CPU, CP0}); 690 Constant *CP00U = ConstantVector::get({CP0, CP0, CPU}); 691 692 EXPECT_FALSE(CP0000->isElementWiseEqual(CP00U0)); 693 EXPECT_FALSE(CP00U0->isElementWiseEqual(CP0000)); 694 EXPECT_FALSE(CP0000->isElementWiseEqual(CP00U)); 695 EXPECT_FALSE(CP00U->isElementWiseEqual(CP00U0)); 696 } 697 698 // Check that vector/aggregate constants correctly store undef and poison 699 // elements. 700 701 TEST(ConstantsTest, CheckElementWiseUndefPoison) { 702 LLVMContext Context; 703 704 Type *Int32Ty = Type::getInt32Ty(Context); 705 StructType *STy = StructType::get(Int32Ty, Int32Ty); 706 ArrayType *ATy = ArrayType::get(Int32Ty, 2); 707 Constant *CU = UndefValue::get(Int32Ty); 708 Constant *CP = PoisonValue::get(Int32Ty); 709 710 { 711 Constant *CUU = ConstantVector::get({CU, CU}); 712 Constant *CPP = ConstantVector::get({CP, CP}); 713 Constant *CUP = ConstantVector::get({CU, CP}); 714 Constant *CPU = ConstantVector::get({CP, CU}); 715 EXPECT_EQ(CUU, UndefValue::get(CUU->getType())); 716 EXPECT_EQ(CPP, PoisonValue::get(CPP->getType())); 717 EXPECT_NE(CUP, UndefValue::get(CUP->getType())); 718 EXPECT_NE(CPU, UndefValue::get(CPU->getType())); 719 } 720 721 { 722 Constant *CUU = ConstantStruct::get(STy, {CU, CU}); 723 Constant *CPP = ConstantStruct::get(STy, {CP, CP}); 724 Constant *CUP = ConstantStruct::get(STy, {CU, CP}); 725 Constant *CPU = ConstantStruct::get(STy, {CP, CU}); 726 EXPECT_EQ(CUU, UndefValue::get(CUU->getType())); 727 EXPECT_EQ(CPP, PoisonValue::get(CPP->getType())); 728 EXPECT_NE(CUP, UndefValue::get(CUP->getType())); 729 EXPECT_NE(CPU, UndefValue::get(CPU->getType())); 730 } 731 732 { 733 Constant *CUU = ConstantArray::get(ATy, {CU, CU}); 734 Constant *CPP = ConstantArray::get(ATy, {CP, CP}); 735 Constant *CUP = ConstantArray::get(ATy, {CU, CP}); 736 Constant *CPU = ConstantArray::get(ATy, {CP, CU}); 737 EXPECT_EQ(CUU, UndefValue::get(CUU->getType())); 738 EXPECT_EQ(CPP, PoisonValue::get(CPP->getType())); 739 EXPECT_NE(CUP, UndefValue::get(CUP->getType())); 740 EXPECT_NE(CPU, UndefValue::get(CPU->getType())); 741 } 742 } 743 744 TEST(ConstantsTest, GetSplatValueRoundTrip) { 745 LLVMContext Context; 746 747 Type *FloatTy = Type::getFloatTy(Context); 748 Type *Int32Ty = Type::getInt32Ty(Context); 749 Type *Int8Ty = Type::getInt8Ty(Context); 750 751 for (unsigned Min : {1, 2, 8}) { 752 auto ScalableEC = ElementCount::getScalable(Min); 753 auto FixedEC = ElementCount::getFixed(Min); 754 755 for (auto EC : {ScalableEC, FixedEC}) { 756 for (auto *Ty : {FloatTy, Int32Ty, Int8Ty}) { 757 Constant *Zero = Constant::getNullValue(Ty); 758 Constant *One = Constant::getAllOnesValue(Ty); 759 760 for (auto *C : {Zero, One}) { 761 Constant *Splat = ConstantVector::getSplat(EC, C); 762 ASSERT_NE(nullptr, Splat); 763 764 Constant *SplatVal = Splat->getSplatValue(); 765 EXPECT_NE(nullptr, SplatVal); 766 EXPECT_EQ(SplatVal, C); 767 } 768 } 769 } 770 } 771 } 772 773 TEST(ConstantsTest, ComdatUserTracking) { 774 LLVMContext Context; 775 Module M("MyModule", Context); 776 777 Comdat *C = M.getOrInsertComdat("comdat"); 778 const SmallPtrSetImpl<GlobalObject *> &Users = C->getUsers(); 779 EXPECT_TRUE(Users.size() == 0); 780 781 Type *Ty = Type::getInt8Ty(Context); 782 GlobalVariable *GV1 = cast<GlobalVariable>(M.getOrInsertGlobal("gv1", Ty)); 783 GV1->setComdat(C); 784 EXPECT_TRUE(Users.size() == 1); 785 EXPECT_TRUE(Users.contains(GV1)); 786 787 GlobalVariable *GV2 = cast<GlobalVariable>(M.getOrInsertGlobal("gv2", Ty)); 788 GV2->setComdat(C); 789 EXPECT_TRUE(Users.size() == 2); 790 EXPECT_TRUE(Users.contains(GV2)); 791 792 GV1->eraseFromParent(); 793 EXPECT_TRUE(Users.size() == 1); 794 EXPECT_TRUE(Users.contains(GV2)); 795 796 GV2->eraseFromParent(); 797 EXPECT_TRUE(Users.size() == 0); 798 } 799 800 } // end anonymous namespace 801 } // end namespace llvm 802