ConstantsTest.cpp revision 0c7f116bb6950ef819323d855415b2f2b0aad987
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"
182e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman#include "gtest/gtest.h"
192e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
202e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukmannamespace llvm {
212e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukmannamespace {
222e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
232e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha BrukmanTEST(ConstantsTest, Integer_i1) {
24db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType* Int1 = IntegerType::get(getGlobalContext(), 1);
25eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  Constant* One = ConstantInt::get(Int1, 1, true);
26eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  Constant* Zero = ConstantInt::get(Int1, 0);
27eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
28eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
299e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson  Constant* Undef = UndefValue::get(Int1);
302e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
312e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // Input:  @b = constant i1 add(i1 1 , i1 1)
322e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // Output: @b = constant i1 false
339138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
342e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
352e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @c = constant i1 add(i1 -1, i1 1)
362e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @c = constant i1 false
379138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
382e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
392e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @d = constant i1 add(i1 -1, i1 -1)
402e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @d = constant i1 false
419138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
422e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
432e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @e = constant i1 sub(i1 -1, i1 1)
442e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @e = constant i1 false
459138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
462e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
472e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @f = constant i1 sub(i1 1 , i1 -1)
482e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @f = constant i1 false
499138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
502e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
512e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @g = constant i1 sub(i1 1 , i1 1)
522e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @g = constant i1 false
539138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
542e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
552e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @h = constant i1 shl(i1 1 , i1 1)  ; undefined
562e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @h = constant i1 undef
579138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
582e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
592e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @i = constant i1 shl(i1 1 , i1 0)
602e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @i = constant i1 true
619138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
622e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
632e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @j = constant i1 lshr(i1 1, i1 1)  ; undefined
642e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @j = constant i1 undef
659138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
662e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
672e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @m = constant i1 ashr(i1 1, i1 1)  ; undefined
682e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @m = constant i1 undef
699138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
702e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
712e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @n = constant i1 mul(i1 -1, i1 1)
722e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @n = constant i1 true
739138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
742e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
752e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
762e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @o = constant i1 true
779138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
782e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
792e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @p = constant i1 sdiv(i1 1 , i1 -1); overflow
802e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @p = constant i1 true
819138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
822e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
832e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @q = constant i1 udiv(i1 -1, i1 1)
842e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @q = constant i1 true
859138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
862e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
872e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @r = constant i1 udiv(i1 1, i1 -1)
882e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @r = constant i1 true
899138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
902e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
912e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @s = constant i1 srem(i1 -1, i1 1) ; overflow
922e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @s = constant i1 false
939138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
942e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
952e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @t = constant i1 urem(i1 -1, i1 1)
962e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @t = constant i1 false
979138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
982e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
992e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @u = constant i1 srem(i1  1, i1 -1) ; overflow
1002e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman  // @u = constant i1 false
1019138b1909a4c2d88aa88f807b296356f09206e87Benjamin Kramer  EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
1022e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman}
1032e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman
104f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris LattnerTEST(ConstantsTest, IntSigns) {
105db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType* Int8Ty = Type::getInt8Ty(getGlobalContext());
106eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
107eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
108eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
109eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
110eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
111eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
112f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner
113f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner  // Overflow is handled by truncation.
114eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson  EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
115f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner}
116f3b0aac1902d6e01ed9a633f4a93c26317b43835Chris Lattner
117e562dba845265b5577699eb857fead1ea0f7905bChris LattnerTEST(ConstantsTest, FP128Test) {
118db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *FP128Ty = Type::getFP128Ty(getGlobalContext());
119e562dba845265b5577699eb857fead1ea0f7905bChris Lattner
120db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  IntegerType *Int128Ty = Type::getIntNTy(getGlobalContext(), 128);
121e562dba845265b5577699eb857fead1ea0f7905bChris Lattner  Constant *Zero128 = Constant::getNullValue(Int128Ty);
122e562dba845265b5577699eb857fead1ea0f7905bChris Lattner  Constant *X = ConstantExpr::getUIToFP(Zero128, FP128Ty);
123e562dba845265b5577699eb857fead1ea0f7905bChris Lattner  EXPECT_TRUE(isa<ConstantFP>(X));
124e562dba845265b5577699eb857fead1ea0f7905bChris Lattner}
125e562dba845265b5577699eb857fead1ea0f7905bChris Lattner
126655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy StepanovTEST(ConstantsTest, PointerCast) {
127655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  LLVMContext &C(getGlobalContext());
128655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  Type *Int8PtrTy = Type::getInt8PtrTy(C);
129655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  Type *Int32PtrTy = Type::getInt32PtrTy(C);
130655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  Type *Int64Ty = Type::getInt64Ty(C);
131655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  VectorType *Int8PtrVecTy = VectorType::get(Int8PtrTy, 4);
132655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  VectorType *Int32PtrVecTy = VectorType::get(Int32PtrTy, 4);
133655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  VectorType *Int64VecTy = VectorType::get(Int64Ty, 4);
134655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
135655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // ptrtoint i8* to i64
136655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int64Ty),
137655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
138655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrTy), Int64Ty));
139655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
140655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // bitcast i8* to i32*
141655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int32PtrTy),
142655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
143655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrTy), Int32PtrTy));
144655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
145655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // ptrtoint <4 x i8*> to <4 x i64>
146655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int64VecTy),
147655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
148655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrVecTy), Int64VecTy));
149655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
150655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  // bitcast <4 x i8*> to <4 x i32*>
151655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov  EXPECT_EQ(Constant::getNullValue(Int32PtrVecTy),
152655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov            ConstantExpr::getPointerCast(
153655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov              Constant::getNullValue(Int8PtrVecTy), Int32PtrVecTy));
154655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov}
155655578f8b5275e7c59b87d4709b0d56b2621caacEvgeniy Stepanov
1566bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi#define CHECK(x, y) {                                         		\
1576bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    std::string __s;                                            	\
1586bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    raw_string_ostream __o(__s);                                	\
1596bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    Instruction *__I = cast<ConstantExpr>(x)->getAsInstruction();	\
1606bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    __I->print(__o);      						\
1616bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    delete __I; 							\
1626bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    __o.flush();                                                	\
1636bf3e46aa87d0cb6044f2282a40411211bf95a5dNAKAMURA Takumi    EXPECT_EQ(std::string("  <badref> = " y), __s);             	\
164b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  }
165b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
166b9478c2aef060aa6b0ede41c05859c34b1527bf8James MolloyTEST(ConstantsTest, AsInstructionsTest) {
16736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
168b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
169b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int64Ty = Type::getInt64Ty(getGlobalContext());
170b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
171b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int16Ty = Type::getInt16Ty(getGlobalContext());
172b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *Int1Ty = Type::getInt1Ty(getGlobalContext());
173b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *FloatTy = Type::getFloatTy(getGlobalContext());
174b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Type *DoubleTy = Type::getDoubleTy(getGlobalContext());
175b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
176b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *Global = M->getOrInsertGlobal("dummy",
177b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy                                         PointerType::getUnqual(Int32Ty));
178b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *Global2 = M->getOrInsertGlobal("dummy2",
179b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy                                         PointerType::getUnqual(Int32Ty));
180b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
181b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P0 = ConstantExpr::getPtrToInt(Global, Int32Ty);
182b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P1 = ConstantExpr::getUIToFP(P0, FloatTy);
183b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P2 = ConstantExpr::getUIToFP(P0, DoubleTy);
184b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P3 = ConstantExpr::getTrunc(P0, Int1Ty);
185b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P4 = ConstantExpr::getPtrToInt(Global2, Int32Ty);
186b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P5 = ConstantExpr::getUIToFP(P4, FloatTy);
187b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *P6 = ConstantExpr::getBitCast(P4, VectorType::get(Int16Ty, 2));
188b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
189b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  Constant *One = ConstantInt::get(Int32Ty, 1);
190b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
191b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P0STR "ptrtoint (i32** @dummy to i32)"
192b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P1STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to float)"
193b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P2STR "uitofp (i32 ptrtoint (i32** @dummy to i32) to double)"
194b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P3STR "ptrtoint (i32** @dummy to i1)"
195b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P4STR "ptrtoint (i32** @dummy2 to i32)"
196b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P5STR "uitofp (i32 ptrtoint (i32** @dummy2 to i32) to float)"
197b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  #define P6STR "bitcast (i32 ptrtoint (i32** @dummy2 to i32) to <2 x i16>)"
198b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
199b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getNeg(P0), "sub i32 0, " P0STR);
200b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFNeg(P1), "fsub float -0.000000e+00, " P1STR);
201b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getNot(P0), "xor i32 " P0STR ", -1");
202b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAdd(P0, P0), "add i32 " P0STR ", " P0STR);
203b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAdd(P0, P0, false, true), "add nsw i32 " P0STR ", "
204b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P0STR);
205b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAdd(P0, P0, true, true), "add nuw nsw i32 " P0STR ", "
206b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P0STR);
207b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFAdd(P1, P1), "fadd float " P1STR ", " P1STR);
208b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSub(P0, P0), "sub i32 " P0STR ", " P0STR);
209b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFSub(P1, P1), "fsub float " P1STR ", " P1STR);
210b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getMul(P0, P0), "mul i32 " P0STR ", " P0STR);
211b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFMul(P1, P1), "fmul float " P1STR ", " P1STR);
212b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getUDiv(P0, P0), "udiv i32 " P0STR ", " P0STR);
213b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSDiv(P0, P0), "sdiv i32 " P0STR ", " P0STR);
214b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFDiv(P1, P1), "fdiv float " P1STR ", " P1STR);
215b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getURem(P0, P0), "urem i32 " P0STR ", " P0STR);
216b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSRem(P0, P0), "srem i32 " P0STR ", " P0STR);
217b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFRem(P1, P1), "frem float " P1STR ", " P1STR);
218b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAnd(P0, P0), "and i32 " P0STR ", " P0STR);
219b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getOr(P0, P0), "or i32 " P0STR ", " P0STR);
220b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getXor(P0, P0), "xor i32 " P0STR ", " P0STR);
221b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getShl(P0, P0), "shl i32 " P0STR ", " P0STR);
222b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getShl(P0, P0, true), "shl nuw i32 " P0STR ", " P0STR);
223b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getShl(P0, P0, false, true), "shl nsw i32 " P0STR ", "
224b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P0STR);
225b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getLShr(P0, P0, false), "lshr i32 " P0STR ", " P0STR);
226b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getLShr(P0, P0, true), "lshr exact i32 " P0STR ", " P0STR);
227b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAShr(P0, P0, false), "ashr i32 " P0STR ", " P0STR);
228b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getAShr(P0, P0, true), "ashr exact i32 " P0STR ", " P0STR);
229b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
230b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSExt(P0, Int64Ty), "sext i32 " P0STR " to i64");
231b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getZExt(P0, Int64Ty), "zext i32 " P0STR " to i64");
232b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFPTrunc(P2, FloatTy), "fptrunc double " P2STR
233b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        " to float");
234b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFPExtend(P1, DoubleTy), "fpext float " P1STR
235b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        " to double");
236b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
237b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getExactUDiv(P0, P0), "udiv exact i32 " P0STR ", " P0STR);
238b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
239b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getSelect(P3, P0, P4), "select i1 " P3STR ", i32 " P0STR
240b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        ", i32 " P4STR);
241b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getICmp(CmpInst::ICMP_EQ, P0, P4), "icmp eq i32 " P0STR
242b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        ", " P4STR);
243b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getFCmp(CmpInst::FCMP_ULT, P1, P5), "fcmp ult float "
244b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P1STR ", " P5STR);
245b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
246b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  std::vector<Constant*> V;
247b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  V.push_back(One);
248b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  // FIXME: getGetElementPtr() actually creates an inbounds ConstantGEP,
249b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  //        not a normal one!
250b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  //CHECK(ConstantExpr::getGetElementPtr(Global, V, false),
2514c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar  //      "getelementptr i32*, i32** @dummy, i32 1");
2520c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  CHECK(ConstantExpr::getInBoundsGetElementPtr(PointerType::getUnqual(Int32Ty),
2530c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar                                               Global, V),
2544c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar        "getelementptr inbounds i32*, i32** @dummy, i32 1");
255b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
256b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy  CHECK(ConstantExpr::getExtractElement(P6, One), "extractelement <2 x i16> "
257b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy        P6STR ", i32 1");
258b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy}
259b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
260dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#ifdef GTEST_HAS_DEATH_TEST
261dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#ifndef NDEBUG
262dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen HinesTEST(ConstantsTest, ReplaceWithConstantTest) {
263dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
264dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
265dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
266dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Constant *One = ConstantInt::get(Int32Ty, 1);
267dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
268dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Constant *Global =
269dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
2700c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  Constant *GEP = ConstantExpr::getGetElementPtr(
2710c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar      PointerType::getUnqual(Int32Ty), Global, One);
272dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
273dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines               "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
274dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
275dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
276dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#endif
277dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#endif
278dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
279b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy#undef CHECK
280b9478c2aef060aa6b0ede41c05859c34b1527bf8James Molloy
28137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesTEST(ConstantsTest, ConstantArrayReplaceWithConstant) {
28237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext Context;
28337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", Context));
28437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
28537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *IntTy = Type::getInt8Ty(Context);
28637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ArrayType *ArrayTy = ArrayType::get(IntTy, 2);
28737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A01Vals[2] = {ConstantInt::get(IntTy, 0),
28837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                          ConstantInt::get(IntTy, 1)};
28937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A01 = ConstantArray::get(ArrayTy, A01Vals);
29037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
29137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *Global = new GlobalVariable(*M, IntTy, false,
29237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                        GlobalValue::ExternalLinkage, nullptr);
29337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *GlobalInt = ConstantExpr::getPtrToInt(Global, IntTy);
29437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A0GVals[2] = {ConstantInt::get(IntTy, 0), GlobalInt};
29537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *A0G = ConstantArray::get(ArrayTy, A0GVals);
29637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_NE(A01, A0G);
29737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
29837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GlobalVariable *RefArray =
29937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      new GlobalVariable(*M, ArrayTy, false, GlobalValue::ExternalLinkage, A0G);
30037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(A0G, RefArray->getInitializer());
30137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
30237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GlobalInt->replaceAllUsesWith(ConstantInt::get(IntTy, 1));
30337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(A01, RefArray->getInitializer());
30437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
30537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
30637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesTEST(ConstantsTest, ConstantExprReplaceWithConstant) {
30737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext Context;
30837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", Context));
30937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
31037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *IntTy = Type::getInt8Ty(Context);
31137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *G1 = new GlobalVariable(*M, IntTy, false,
31237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    GlobalValue::ExternalLinkage, nullptr);
31337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *G2 = new GlobalVariable(*M, IntTy, false,
31437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    GlobalValue::ExternalLinkage, nullptr);
31537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_NE(G1, G2);
31637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
31737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *Int1 = ConstantExpr::getPtrToInt(G1, IntTy);
31837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Constant *Int2 = ConstantExpr::getPtrToInt(G2, IntTy);
31937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_NE(Int1, Int2);
32037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  GlobalVariable *Ref =
32237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      new GlobalVariable(*M, IntTy, false, GlobalValue::ExternalLinkage, Int1);
32337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(Int1, Ref->getInitializer());
32437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  G1->replaceAllUsesWith(G2);
32637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(Int2, Ref->getInitializer());
32737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
32837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
32937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesTEST(ConstantsTest, GEPReplaceWithConstant) {
33037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  LLVMContext Context;
33137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> M(new Module("MyModule", Context));
33237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
33337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *IntTy = Type::getInt32Ty(Context);
33437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Type *PtrTy = PointerType::get(IntTy, 0);
33537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *C1 = ConstantInt::get(IntTy, 1);
33637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Placeholder = new GlobalVariable(
33737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      *M, IntTy, false, GlobalValue::ExternalWeakLinkage, nullptr);
3380c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  auto *GEP = ConstantExpr::getGetElementPtr(IntTy, Placeholder, C1);
33937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP->getOperand(0), Placeholder);
34037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Ref =
34237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines      new GlobalVariable(*M, PtrTy, false, GlobalValue::ExternalLinkage, GEP);
34337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP, Ref->getInitializer());
34437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
34537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Global = new GlobalVariable(*M, PtrTy, false,
34637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    GlobalValue::ExternalLinkage, nullptr);
34737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  auto *Alias = GlobalAlias::create(IntTy, 0, GlobalValue::ExternalLinkage,
34837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                    "alias", Global, M.get());
34937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  Placeholder->replaceAllUsesWith(Alias);
35037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP, Ref->getInitializer());
35137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  ASSERT_EQ(GEP->getOperand(0), Alias);
35237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
35337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
354ebe69fe11e48d322045d5949c83283927a0d790bStephen HinesTEST(ConstantsTest, AliasCAPI) {
355ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LLVMContext Context;
356ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  SMDiagnostic Error;
357ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  std::unique_ptr<Module> M =
358ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      parseAssemblyString("@g = global i32 42", Error, Context);
359ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  GlobalVariable *G = M->getGlobalVariable("g");
360ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Type *I16Ty = Type::getInt16Ty(Context);
361ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Type *I16PTy = PointerType::get(I16Ty, 0);
362ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  Constant *Aliasee = ConstantExpr::getBitCast(G, I16PTy);
363ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  LLVMValueRef AliasRef =
364ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      LLVMAddAlias(wrap(M.get()), wrap(I16PTy), wrap(Aliasee), "a");
365ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
366ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
367ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
3682e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman}  // end anonymous namespace
3692e734269e3f354e52bd9e55d791e1885aa7d4cd8Misha Brukman}  // end namespace llvm
370