2009-03-24 22:36:09 +01:00
|
|
|
//===- llvm/unittest/VMCore/ConstantsTest.cpp - Constants unit tests ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-07-14 03:26:26 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2009-03-24 22:36:09 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(ConstantsTest, Integer_i1) {
|
|
|
|
const IntegerType* Int1 = IntegerType::get(1);
|
2009-07-25 01:12:02 +02:00
|
|
|
Constant* One = ConstantInt::get(Int1, 1, true);
|
|
|
|
Constant* Zero = ConstantInt::get(Int1, 0);
|
|
|
|
Constant* NegOne = ConstantInt::get(Int1, static_cast<uint64_t>(-1), true);
|
|
|
|
EXPECT_EQ(NegOne, ConstantInt::getSigned(Int1, -1));
|
2009-07-31 01:03:37 +02:00
|
|
|
Constant* Undef = UndefValue::get(Int1);
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// Input: @b = constant i1 add(i1 1 , i1 1)
|
|
|
|
// Output: @b = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getAdd(One, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @c = constant i1 add(i1 -1, i1 1)
|
|
|
|
// @c = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @d = constant i1 add(i1 -1, i1 -1)
|
|
|
|
// @d = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getAdd(NegOne, NegOne));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @e = constant i1 sub(i1 -1, i1 1)
|
|
|
|
// @e = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getSub(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @f = constant i1 sub(i1 1 , i1 -1)
|
|
|
|
// @f = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getSub(One, NegOne));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @g = constant i1 sub(i1 1 , i1 1)
|
|
|
|
// @g = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getSub(One, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @h = constant i1 shl(i1 1 , i1 1) ; undefined
|
|
|
|
// @h = constant i1 undef
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Undef, ConstantExpr::getShl(One, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @i = constant i1 shl(i1 1 , i1 0)
|
|
|
|
// @i = constant i1 true
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(One, ConstantExpr::getShl(One, Zero));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @j = constant i1 lshr(i1 1, i1 1) ; undefined
|
|
|
|
// @j = constant i1 undef
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Undef, ConstantExpr::getLShr(One, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @m = constant i1 ashr(i1 1, i1 1) ; undefined
|
|
|
|
// @m = constant i1 undef
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Undef, ConstantExpr::getAShr(One, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @n = constant i1 mul(i1 -1, i1 1)
|
|
|
|
// @n = constant i1 true
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(One, ConstantExpr::getMul(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @o = constant i1 sdiv(i1 -1, i1 1) ; overflow
|
|
|
|
// @o = constant i1 true
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(One, ConstantExpr::getSDiv(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @p = constant i1 sdiv(i1 1 , i1 -1); overflow
|
|
|
|
// @p = constant i1 true
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(One, ConstantExpr::getSDiv(One, NegOne));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @q = constant i1 udiv(i1 -1, i1 1)
|
|
|
|
// @q = constant i1 true
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(One, ConstantExpr::getUDiv(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @r = constant i1 udiv(i1 1, i1 -1)
|
|
|
|
// @r = constant i1 true
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(One, ConstantExpr::getUDiv(One, NegOne));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @s = constant i1 srem(i1 -1, i1 1) ; overflow
|
|
|
|
// @s = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getSRem(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @t = constant i1 urem(i1 -1, i1 1)
|
|
|
|
// @t = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getURem(NegOne, One));
|
2009-03-24 22:36:09 +01:00
|
|
|
|
|
|
|
// @u = constant i1 srem(i1 1, i1 -1) ; overflow
|
|
|
|
// @u = constant i1 false
|
2009-07-29 21:18:13 +02:00
|
|
|
EXPECT_EQ(Zero, ConstantExpr::getSRem(One, NegOne));
|
2009-03-24 22:36:09 +01:00
|
|
|
}
|
|
|
|
|
2009-04-24 07:30:14 +02:00
|
|
|
TEST(ConstantsTest, IntSigns) {
|
|
|
|
const IntegerType* Int8Ty = Type::Int8Ty;
|
2009-07-25 01:12:02 +02:00
|
|
|
EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, false)->getSExtValue());
|
|
|
|
EXPECT_EQ(100, ConstantInt::get(Int8Ty, 100, true)->getSExtValue());
|
|
|
|
EXPECT_EQ(100, ConstantInt::getSigned(Int8Ty, 100)->getSExtValue());
|
|
|
|
EXPECT_EQ(-50, ConstantInt::get(Int8Ty, 206)->getSExtValue());
|
|
|
|
EXPECT_EQ(-50, ConstantInt::getSigned(Int8Ty, -50)->getSExtValue());
|
|
|
|
EXPECT_EQ(206U, ConstantInt::getSigned(Int8Ty, -50)->getZExtValue());
|
2009-04-24 07:30:14 +02:00
|
|
|
|
|
|
|
// Overflow is handled by truncation.
|
2009-07-25 01:12:02 +02:00
|
|
|
EXPECT_EQ(0x3b, ConstantInt::get(Int8Ty, 0x13b)->getSExtValue());
|
2009-04-24 07:30:14 +02:00
|
|
|
}
|
|
|
|
|
2009-03-24 22:36:09 +01:00
|
|
|
} // end anonymous namespace
|
|
|
|
} // end namespace llvm
|