1c779e96158cbac4c62df8e2053ab6a933eba5868Chandler Carruth//===- llvm/unittest/IR/ConstantsTest.cpp - Constants unit tests ----------===//
22e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman//
32e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman//                     The LLVM Compiler Infrastructure
42e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman//
52e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman// This file is distributed under the University of Illinois Open Source
62e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman// License. See LICENSE.TXT for details.
72e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman//
82e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman//===----------------------------------------------------------------------===//
92e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
10ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/AsmParser/Parser.h"
110b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
120b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
130b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/InstrTypes.h"
140b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instruction.h"
150b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
17ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/Support/SourceMgr.h"
18cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar#include "llvm-c/Core.h"
192e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman#include "gtest/gtest.h"
202e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
212e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukmannamespace llvm {
222e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukmannamespace {
232e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
242e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha BrukmanTEST(ConstantsTest, Integer_i1) {
25db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
26eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  Constant* One = ConstantInt::get(Int1, 1, true);
27eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  Constant* Zero = ConstantInt::get(Int1, 0);
28eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
29eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
309e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson  Constant* Undef = UndefValue::get(Int1);
312e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
322e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // Input:  @b = constant i1 add(i1 1 , i1 1)
332e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // Output: @b = constant i1 false
349138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
352e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
362e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @c = constant i1 add(i1 -1, i1 1)
372e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @c = constant i1 false
389138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
392e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
402e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @d = constant i1 add(i1 -1, i1 -1)
412e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @d = constant i1 false
429138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
432e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
442e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @e = constant i1 sub(i1 -1, i1 1)
452e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @e = constant i1 false
469138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
472e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
482e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @f = constant i1 sub(i1 1 , i1 -1)
492e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @f = constant i1 false
509138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
512e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
522e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @g = constant i1 sub(i1 1 , i1 1)
532e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @g = constant i1 false
549138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
552e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
562e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @h = constant i1 shl(i1 1 , i1 1)  ; undefined
572e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @h = constant i1 undef
589138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
592e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
602e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @i = constant i1 shl(i1 1 , i1 0)
612e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @i = constant i1 true
629138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
632e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
642e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @j = constant i1 lshr(i1 1, i1 1)  ; undefined
652e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @j = constant i1 undef
669138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
672e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
682e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @m = constant i1 ashr(i1 1, i1 1)  ; undefined
692e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @m = constant i1 undef
709138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
712e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
722e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @n = constant i1 mul(i1 -1, i1 1)
732e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @n = constant i1 true
749138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
752e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
762e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
772e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @o = constant i1 true
789138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
792e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
802e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
812e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @p = constant i1 true
829138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
832e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
842e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @q = constant i1 udiv(i1 -1, i1 1)
852e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @q = constant i1 true
869138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
872e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
882e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @r = constant i1 udiv(i1 1, i1 -1)
892e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @r = constant i1 true
909138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
912e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
922e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @s = constant i1 srem(i1 -1, i1 1) ; overflow
932e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @s = constant i1 false
949138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
952e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
962e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @t = constant i1 urem(i1 -1, i1 1)
972e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @t = constant i1 false
989138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
992e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
1002e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @u = constant i1 srem(i1  1, i1 -1) ; overflow
1012e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @u = constant i1 false
1029138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
1032e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman}
1042e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
105f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris LattnerTEST(ConstantsTest, IntSigns) {
106db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
107eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
108eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
109eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
110eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
111eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
112eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
113f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner
114f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner  // Overflow is handled by truncation.
115eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
116f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner}
117f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner
118e562dba845265b5577699eb857fead1ea0f7905bChris LattnerTEST(ConstantsTest, FP128Test) {
119db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
120e562dba845265b5577699eb857fead1ea0f7905bChris Lattner
121db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
122e562dba845265b5577699eb857fead1ea0f7905bChris Lattner  Constant *Zero128 = Constant::getNullValue(Int128Ty);
123e562dba845265b5577699eb857fead1ea0f7905bChris Lattner  Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
124e562dba845265b5577699eb857fead1ea0f7905bChris Lattner  EXPECT_TRUE(isa<ConstantFP>(X));
125e562dba845265b5577699eb857fead1ea0f7905bChris Lattner}
126e562dba845265b5577699eb857fead1ea0f7905bChris Lattner
127655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy StepanovTEST(ConstantsTest, PointerCast) {
128655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  LLVMContext &C(getGlobalContext());
129655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  Type *Int8PtrTy = Type::getInt8PtrTy(C);
130655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  Type *Int32PtrTy = Type::getInt32PtrTy(C);
131655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  Type *Int64Ty = Type::getInt64Ty(C);
132655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
133655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
134655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
135655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
136655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // ptrtoint i8* to i64
137655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int64Ty),
138655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
139655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrTy), Int64Ty));
140655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
141655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // bitcast i8* to i32*
142655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
143655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
144655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrTy), Int32PtrTy));
145655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
146655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // ptrtoint <4 x i8*> to <4 x i64>
147655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int64VecTy),
148655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
149655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
150655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
151655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // bitcast <4 x i8*> to <4 x i32*>
152655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
153655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
154655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
155655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov}
156655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
1576bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi#define CHECK(x, y) {                                         		\
1586bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    std::string __s;                                            	\
1596bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    raw_string_ostream __o(__s);                                	\
1606bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction();	\
1616bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    __I->print(__o);      						\
1626bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    delete __I; 							\
1636bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    __o.flush();                                                	\
1646bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    EXPECT_EQ(std::string("  <badref> = " y), __s);             	\
165b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  }
166b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
167b9478c2aef060aa6b0ede41c05859c34b1527bf8James MolloyTEST(ConstantsTest, AsInstructionsTest) {
16836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
169b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
170b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
171b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
172b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
173b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
174b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *FloatTy = Type::getFloatTy(getGlobalContext());
175b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
176b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
177b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *Global = M->getOrInsertGlobal("dummy",
178b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy                                         PointerType::getUnqual(Int32Ty));
179b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *Global2 = M->getOrInsertGlobal("dummy2",
180b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy                                         PointerType::getUnqual(Int32Ty));
181b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
182b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
183b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
184b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
185b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
186b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
187b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
188b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
189b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
190b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *One = ConstantInt::get(Int32Ty, 1);
1916948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Constant *Two = ConstantInt::get(Int64Ty, 2);
1926948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Constant *Big = ConstantInt::get(getGlobalContext(),
1936948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                                   APInt{256, uint64_t(-1), true});
1946948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Constant *Elt = ConstantInt::get(Int16Ty, 2015);
1956948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Constant *Undef16  = UndefValue::get(Int16Ty);
1966948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Constant *Undef64  = UndefValue::get(Int64Ty);
1976948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  Constant *UndefV16 = UndefValue::get(P6->getType());
198b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
199b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P0STR "ptrtoint (i32** @dummy to i32)"
200b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
201b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
202b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P3STR "ptrtoint (i32** @dummy to i1)"
203b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P4STR "ptrtoint (i32** @dummy2 to i32)"
204b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
205b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
206b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
207b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
208b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
209b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
210b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
211b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
212b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P0STR);
213b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
214b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P0STR);
215b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
216b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
217b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
218b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
219b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
220b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
221b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
222b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
223b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
224b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
225b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
226b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
227b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
228b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
229b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
230b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
231b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
232b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P0STR);
233b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
234b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
235b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
236b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
237b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
238b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
239b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
240b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
241b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        " to float");
242b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
243b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        " to double");
244b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
245b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
246b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
247b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
248b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        ", i32 " P4STR);
249b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
250b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        ", " P4STR);
251b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
252b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P1STR ", " P5STR);
253b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
254b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  std::vector<Constant*> V;
255b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  V.push_back(One);
256b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
257b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  //        not a normal one!
258b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
2594c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  //      "getelementptr i32*, i32** @dummy, i32 1");
2600c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
2610c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                               Global, V),
2624c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar        "getelementptr inbounds i32*, i32** @dummy, i32 1");
263b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
264b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
265b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P6STR ", i32 1");
2666948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
2676948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Two));
2686948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Big));
2696948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(Undef16, ConstantExpr::getExtractElement(P6, Undef64));
2706948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar
2716948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(Elt, ConstantExpr::getExtractElement(
2726948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar                 ConstantExpr::getInsertElement(P6, Elt, One), One));
2736948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Two));
2746948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Big));
2756948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  EXPECT_EQ(UndefV16, ConstantExpr::getInsertElement(P6, Elt, Undef64));
276b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy}
277b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
278dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#ifdef GTEST_HAS_DEATH_TEST
279dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#ifndef NDEBUG
280dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesTEST(ConstantsTest, ReplaceWithConstantTest) {
281dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
282dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
283dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
284dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Constant *One = ConstantInt::get(Int32Ty, 1);
285dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
286dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Constant *Global =
287dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
2880c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  Constant *GEP = ConstantExpr::getGetElementPtr(
2890c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar      PointerType::getUnqual(Int32Ty), Global, One);
290dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
291dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines               "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
292dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
293dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
294dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#endif
295dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#endif
296dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
297b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy#undef CHECK
298b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
29937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesTEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
30037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext Context;
30137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", Context));
30237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
30337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *IntTy = Type::getInt8Ty(Context);
30437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
30537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
30637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                          ConstantInt::get(IntTy, 1)};
30737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
30837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
30937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *Global = new GlobalVariable(*M, IntTy, false,
31037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                        GlobalValue::ExternalLinkage, nullptr);
31137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
31237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
31337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
31437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_NE(A01, A0G);
31537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
31637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GlobalVariable *RefArray =
31737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
31837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(A0G, RefArray->getInitializer());
31937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
32137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(A01, RefArray->getInitializer());
32237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
32337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesTEST(ConstantsTest, ConstantExprReplaceWithConstant) {
32537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext Context;
32637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", Context));
32737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *IntTy = Type::getInt8Ty(Context);
32937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *G1 = new GlobalVariable(*M, IntTy, false,
33037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    GlobalValue::ExternalLinkage, nullptr);
33137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *G2 = new GlobalVariable(*M, IntTy, false,
33237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    GlobalValue::ExternalLinkage, nullptr);
33337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_NE(G1, G2);
33437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
33537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
33637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
33737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_NE(Int1, Int2);
33837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
33937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GlobalVariable *Ref =
34037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
34137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(Int1, Ref->getInitializer());
34237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  G1->replaceAllUsesWith(G2);
34437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(Int2, Ref->getInitializer());
34537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesTEST(ConstantsTest, GEPReplaceWithConstant) {
34837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext Context;
34937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", Context));
35037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
35137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *IntTy = Type::getInt32Ty(Context);
352cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  Type *PtrTy = PointerType::get(IntTy, 0);
35337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *C1 = ConstantInt::get(IntTy, 1);
35437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Placeholder = new GlobalVariable(
35537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
3560c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
35737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP->getOperand(0), Placeholder);
35837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
35937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Ref =
36037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
36137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP, Ref->getInitializer());
36237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
36337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Global = new GlobalVariable(*M, PtrTy, false,
36437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    GlobalValue::ExternalLinkage, nullptr);
365cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
36637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    "alias", Global, M.get());
36737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Placeholder->replaceAllUsesWith(Alias);
36837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP, Ref->getInitializer());
36937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP->getOperand(0), Alias);
37037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
37137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
372ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesTEST(ConstantsTest, AliasCAPI) {
373ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LLVMContext Context;
374ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  SMDiagnostic Error;
375ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  std::unique_ptr<Module> M =
376ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      parseAssemblyString("@g = global i32 42", Error, Context);
377ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  GlobalVariable *G = M->getGlobalVariable("g");
378ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Type *I16Ty = Type::getInt16Ty(Context);
379ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Type *I16PTy = PointerType::get(I16Ty, 0);
380ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
381ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LLVMValueRef AliasRef =
382ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
383ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
384ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
385ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
386cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstatic std::string getNameOfType(Type *T) {
387cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::string S;
388cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  raw_string_ostream RSOS(S);
389cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  T->print(RSOS);
390cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  return S;
391cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
392cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
393cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarTEST(ConstantsTest, BuildConstantDataArrays) {
394cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  LLVMContext Context;
395cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<Module> M(new Module("MyModule", Context));
396cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
397cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
398cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                  Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
399cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    ArrayType *ArrayTy = ArrayType::get(T, 2);
400cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
401cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *CDV = ConstantArray::get(ArrayTy, Vals);
402cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
403cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        << " T = " << getNameOfType(T);
404cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
405cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
406cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
407cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                  Type::getDoubleTy(Context)}) {
408cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    ArrayType *ArrayTy = ArrayType::get(T, 2);
409cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
410cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *CDV = ConstantArray::get(ArrayTy, Vals);
411cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    ASSERT_TRUE(dyn_cast<ConstantDataArray>(CDV) != nullptr)
412cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        << " T = " << getNameOfType(T);
413cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
414cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
415cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
416cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarTEST(ConstantsTest, BuildConstantDataVectors) {
417cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  LLVMContext Context;
418cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<Module> M(new Module("MyModule", Context));
419cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
420cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (Type *T : {Type::getInt8Ty(Context), Type::getInt16Ty(Context),
421cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                  Type::getInt32Ty(Context), Type::getInt64Ty(Context)}) {
422cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *Vals[] = {ConstantInt::get(T, 0), ConstantInt::get(T, 1)};
423cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *CDV = ConstantVector::get(Vals);
424cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
425cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        << " T = " << getNameOfType(T);
426cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
427cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
428cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (Type *T : {Type::getHalfTy(Context), Type::getFloatTy(Context),
429cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                  Type::getDoubleTy(Context)}) {
430cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *Vals[] = {ConstantFP::get(T, 0), ConstantFP::get(T, 1)};
431cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Constant *CDV = ConstantVector::get(Vals);
432cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    ASSERT_TRUE(dyn_cast<ConstantDataVector>(CDV) != nullptr)
433cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        << " T = " << getNameOfType(T);
434cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
435cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
436cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
437cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga NainarTEST(ConstantsTest, BitcastToGEP) {
438cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  LLVMContext Context;
439cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<Module> M(new Module("MyModule", Context));
440cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
441cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *i32 = Type::getInt32Ty(Context);
442cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *U = StructType::create(Context, "Unsized");
443cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  Type *EltTys[] = {i32, U};
444cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *S = StructType::create(EltTys);
445cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
446cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *G = new GlobalVariable(*M, S, false,
447cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar                               GlobalValue::ExternalLinkage, nullptr);
448cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *PtrTy = PointerType::get(i32, 0);
449cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  auto *C = ConstantExpr::getBitCast(G, PtrTy);
450cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  ASSERT_EQ(dyn_cast<ConstantExpr>(C)->getOpcode(),
451cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar            Instruction::BitCast);
452cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
453cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
4542e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman}  // end anonymous namespace
4552e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman}  // end namespace llvm
456