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