dbg_func_start, // Start of a function
dbg_declare, // Declare a local object
-
- // Standard libc functions.
+ // Standard C library intrinsics.
memcpy, // Copy non-overlapping memory blocks
memmove, // Copy potentially overlapping memory blocks
memset, // Fill memory with a byte value
-
- // libm related functions.
isunordered, // Return true if either argument is a NaN
- ctpop, //count population
- ctlz, //count leading zeros
- cttz, //count trailing zeros
- sqrt, //square root
-
+ sqrt, // Square root
+
+ // Bit manipulation instrinsics.
+ bswap_i16, // Byteswap 16 bits
+ bswap_i32, // Byteswap 32 bits
+ bswap_i64, // Byteswap 64 bits
+ ctpop, // Count population
+ ctlz, // Count leading zeros
+ cttz, // Count trailing zeros
+
// Input/Output intrinsics.
readport,
writeport,
return Value && !(Value & (Value - 1LL));
}
+// ByteSwap_16 - This function returns a byte-swapped representation of the
+// 16-bit argument, Value.
+inline unsigned short ByteSwap_16(unsigned short Value) {
+ unsigned short Hi = Value << 8;
+ unsigned short Lo = Value >> 8;
+ return Hi | Lo;
+}
+
+// ByteSwap_32 - This function returns a byte-swapped representation of the
+// 32-bit argument, Value.
+inline unsigned ByteSwap_32(unsigned Value) {
+ unsigned Byte0 = Value & 0x000000FF;
+ unsigned Byte1 = Value & 0x0000FF00;
+ unsigned Byte2 = Value & 0x00FF0000;
+ unsigned Byte3 = Value & 0xFF000000;
+ return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
+}
+
+// ByteSwap_64 - This function returns a byte-swapped representation of the
+// 64-bit argument, Value.
+inline uint64_t ByteSwap_64(uint64_t Value) {
+ uint64_t Hi = ByteSwap_32(Value);
+ uint64_t Lo = ByteSwap_32(Value >> 32);
+ return (Hi << 32) | Lo;
+}
+
// CountLeadingZeros_32 - this function performs the platform optimal form of
// counting the number of zeros from the most significant bit to the first one
// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
static const char *DoesntAccessMemoryTable[] = {
// LLVM intrinsics:
"llvm.frameaddress", "llvm.returnaddress", "llvm.readport",
- "llvm.isunordered", "llvm.sqrt", "llvm.ctpop", "llvm.ctlz", "llvm.cttz",
+ "llvm.isunordered", "llvm.sqrt", "llvm.bswap.i16", "llvm.bswap.i32",
+ "llvm.bswap.i64", "llvm.ctpop", "llvm.ctlz", "llvm.cttz",
"abs", "labs", "llabs", "imaxabs", "fabs", "fabsf", "fabsl",
"trunc", "truncf", "truncl", "ldexp",
switch (F->getIntrinsicID()) {
case Intrinsic::isunordered:
case Intrinsic::sqrt:
+ case Intrinsic::bswap_i16:
+ case Intrinsic::bswap_i32:
+ case Intrinsic::bswap_i64:
+ // FIXME: these should be constant folded as well
+ //case Intrinsic::ctpop:
+ //case Intrinsic::ctlz:
+ //case Intrinsic::cttz:
return true;
default: break;
}
default:
break;
}
+ } else if (ConstantUInt *Op = dyn_cast<ConstantUInt>(Operands[0])) {
+ uint64_t V = Op->getValue();
+ if (Name == "llvm.bswap.i16")
+ return ConstantUInt::get(Ty, ByteSwap_16(V));
+ else if (Name == "llvm.bswap.i32")
+ return ConstantUInt::get(Ty, ByteSwap_32(V));
+ else if (Name == "llvm.bswap.i64")
+ return ConstantUInt::get(Ty, ByteSwap_64(V));
}
} else if (Operands.size() == 2) {
if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
case Intrinsic::frameaddress:
case Intrinsic::stacksave:
case Intrinsic::isunordered:
+ case Intrinsic::bswap_i16:
+ case Intrinsic::bswap_i32:
+ case Intrinsic::bswap_i64:
case Intrinsic::ctpop:
case Intrinsic::ctlz:
case Intrinsic::cttz:
assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
switch (getName()[5]) {
+ case 'b':
+ if (getName() == "llvm.bswap.i16") return Intrinsic::bswap_i16;
+ if (getName() == "llvm.bswap.i32") return Intrinsic::bswap_i32;
+ if (getName() == "llvm.bswap.i64") return Intrinsic::bswap_i64;
+ break;
case 'c':
if (getName() == "llvm.ctpop") return Intrinsic::ctpop;
if (getName() == "llvm.cttz") return Intrinsic::cttz;
NumArgs = 0;
break;
+ case Intrinsic::bswap_i16:
+ Assert1(FT->getNumParams() == 1,
+ "Illegal # arguments for intrinsic function!", IF);
+ Assert1(FT->getReturnType() == FT->getParamType(0),
+ "Return type does not match source type", IF);
+ Assert1(FT->getReturnType() == Type::UShortTy,
+ "Return type is not ushort!", IF);
+ NumArgs = 1;
+ break;
+
+ case Intrinsic::bswap_i32:
+ Assert1(FT->getNumParams() == 1,
+ "Illegal # arguments for intrinsic function!", IF);
+ Assert1(FT->getReturnType() == FT->getParamType(0),
+ "Return type does not match source type", IF);
+ Assert1(FT->getReturnType() == Type::UIntTy,
+ "Return type is not uint!", IF);
+ NumArgs = 1;
+ break;
+
+ case Intrinsic::bswap_i64:
+ Assert1(FT->getNumParams() == 1,
+ "Illegal # arguments for intrinsic function!", IF);
+ Assert1(FT->getReturnType() == FT->getParamType(0),
+ "Return type does not match source type", IF);
+ Assert1(FT->getReturnType() == Type::ULongTy,
+ "Return type is not ulong!", IF);
+ NumArgs = 1;
+ break;
+
case Intrinsic::ctpop:
case Intrinsic::ctlz:
case Intrinsic::cttz:
--- /dev/null
+; bswap should be constant folded when it is passed a constant argument
+
+; RUN: llvm-as < %s | opt -constprop | llvm-dis | not grep call
+
+declare ushort %llvm.bswap.i16(ushort)
+declare uint %llvm.bswap.i32(uint)
+declare ulong %llvm.bswap.i64(ulong)
+
+ushort %W() {
+ %Z = call ushort %llvm.bswap.i16(ushort 1)
+ ret ushort %Z
+}
+
+uint %X() {
+ %Z = call uint %llvm.bswap.i32(uint 1)
+ ret uint %Z
+}
+
+ulong %Y() {
+ %Z = call ulong %llvm.bswap.i64(ulong 1)
+ ret ulong %Z
+}