2008-11-28 23:31:44 +00:00
|
|
|
//===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the PointerIntPair class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_POINTERINTPAIR_H
|
|
|
|
#define LLVM_ADT_POINTERINTPAIR_H
|
|
|
|
|
2013-09-23 23:26:57 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-03-29 04:32:37 +00:00
|
|
|
#include "llvm/Support/PointerLikeTypeTraits.h"
|
2008-11-28 23:31:44 +00:00
|
|
|
#include <cassert>
|
2014-03-07 19:19:56 +00:00
|
|
|
#include <limits>
|
2008-11-28 23:31:44 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-29 01:18:05 +00:00
|
|
|
template<typename T>
|
|
|
|
struct DenseMapInfo;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
/// PointerIntPair - This class implements a pair of a pointer and small
|
2008-11-28 23:57:26 +00:00
|
|
|
/// integer. It is designed to represent this in the space required by one
|
2008-11-28 23:31:44 +00:00
|
|
|
/// pointer by bitmangling the integer into the low part of the pointer. This
|
|
|
|
/// can only be done for small integers: typically up to 3 bits, but it depends
|
2009-03-29 04:32:37 +00:00
|
|
|
/// on the number of bits available according to PointerLikeTypeTraits for the
|
|
|
|
/// type.
|
|
|
|
///
|
2013-04-09 00:01:51 +00:00
|
|
|
/// Note that PointerIntPair always puts the IntVal part in the highest bits
|
2009-03-29 13:26:05 +00:00
|
|
|
/// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
|
2009-03-29 04:32:37 +00:00
|
|
|
/// the bool into bit #2, not bit #0, which allows the low two bits to be used
|
|
|
|
/// for something else. For example, this allows:
|
2009-03-29 13:26:05 +00:00
|
|
|
/// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
|
2009-03-29 04:32:37 +00:00
|
|
|
/// ... and the two bools will land in different bits.
|
2008-11-28 23:31:44 +00:00
|
|
|
///
|
2009-03-29 06:02:20 +00:00
|
|
|
template <typename PointerTy, unsigned IntBits, typename IntType=unsigned,
|
|
|
|
typename PtrTraits = PointerLikeTypeTraits<PointerTy> >
|
2008-11-28 23:31:44 +00:00
|
|
|
class PointerIntPair {
|
|
|
|
intptr_t Value;
|
2014-03-07 19:19:56 +00:00
|
|
|
static_assert(PtrTraits::NumLowBitsAvailable <
|
|
|
|
std::numeric_limits<uintptr_t>::digits,
|
|
|
|
"cannot use a pointer type that has all bits free");
|
2014-03-15 18:10:49 +00:00
|
|
|
static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
|
|
|
|
"PointerIntPair with integer size too large for pointer");
|
2014-03-02 03:20:38 +00:00
|
|
|
enum : uintptr_t {
|
2009-03-29 04:32:37 +00:00
|
|
|
/// PointerBitMask - The bits that come from the pointer.
|
2009-04-26 19:46:41 +00:00
|
|
|
PointerBitMask =
|
2009-04-26 20:12:38 +00:00
|
|
|
~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable)-1),
|
2009-04-26 19:46:41 +00:00
|
|
|
|
2009-03-29 04:32:37 +00:00
|
|
|
/// IntShift - The number of low bits that we reserve for other uses, and
|
|
|
|
/// keep zero.
|
2009-04-26 20:12:38 +00:00
|
|
|
IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable-IntBits,
|
2009-03-29 04:32:37 +00:00
|
|
|
|
|
|
|
/// IntMask - This is the unshifted mask for valid bits of the int type.
|
2009-04-26 20:12:38 +00:00
|
|
|
IntMask = (uintptr_t)(((intptr_t)1 << IntBits)-1),
|
2009-03-29 04:32:37 +00:00
|
|
|
|
|
|
|
// ShiftedIntMask - This is the bits for the integer shifted in place.
|
2009-04-26 20:12:38 +00:00
|
|
|
ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
|
2009-03-29 04:32:37 +00:00
|
|
|
};
|
2008-11-28 23:31:44 +00:00
|
|
|
public:
|
|
|
|
PointerIntPair() : Value(0) {}
|
2013-04-09 00:01:51 +00:00
|
|
|
PointerIntPair(PointerTy PtrVal, IntType IntVal) {
|
|
|
|
setPointerAndInt(PtrVal, IntVal);
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
}
|
2013-04-09 00:01:51 +00:00
|
|
|
explicit PointerIntPair(PointerTy PtrVal) {
|
|
|
|
initWithPointer(PtrVal);
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PointerTy getPointer() const {
|
2009-09-17 20:35:18 +00:00
|
|
|
return PtrTraits::getFromVoidPointer(
|
|
|
|
reinterpret_cast<void*>(Value & PointerBitMask));
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
IntType getInt() const {
|
2009-03-29 04:32:37 +00:00
|
|
|
return (IntType)((Value >> IntShift) & IntMask);
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2013-04-09 00:01:51 +00:00
|
|
|
void setPointer(PointerTy PtrVal) {
|
|
|
|
intptr_t PtrWord
|
|
|
|
= reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
|
2014-03-07 19:19:56 +00:00
|
|
|
assert((PtrWord & ~PointerBitMask) == 0 &&
|
2008-11-29 08:03:35 +00:00
|
|
|
"Pointer is not sufficiently aligned");
|
2009-03-29 04:32:37 +00:00
|
|
|
// Preserve all low bits, just update the pointer.
|
2013-04-09 00:01:51 +00:00
|
|
|
Value = PtrWord | (Value & ~PointerBitMask);
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2013-04-09 00:01:51 +00:00
|
|
|
void setInt(IntType IntVal) {
|
|
|
|
intptr_t IntWord = static_cast<intptr_t>(IntVal);
|
2014-03-07 19:19:56 +00:00
|
|
|
assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
|
2009-03-29 04:32:37 +00:00
|
|
|
|
|
|
|
// Preserve all bits other than the ones we are updating.
|
|
|
|
Value &= ~ShiftedIntMask; // Remove integer field.
|
2013-04-09 00:01:51 +00:00
|
|
|
Value |= IntWord << IntShift; // Set new integer.
|
2008-11-28 23:31:44 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2013-04-09 00:01:51 +00:00
|
|
|
void initWithPointer(PointerTy PtrVal) {
|
|
|
|
intptr_t PtrWord
|
|
|
|
= reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
|
2014-03-07 19:19:56 +00:00
|
|
|
assert((PtrWord & ~PointerBitMask) == 0 &&
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
"Pointer is not sufficiently aligned");
|
2013-04-09 00:01:51 +00:00
|
|
|
Value = PtrWord;
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 00:01:51 +00:00
|
|
|
void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
|
|
|
|
intptr_t PtrWord
|
|
|
|
= reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
|
2014-03-07 19:19:56 +00:00
|
|
|
assert((PtrWord & ~PointerBitMask) == 0 &&
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
"Pointer is not sufficiently aligned");
|
2013-04-09 00:01:51 +00:00
|
|
|
intptr_t IntWord = static_cast<intptr_t>(IntVal);
|
2014-03-07 19:19:56 +00:00
|
|
|
assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
|
2013-04-09 00:01:51 +00:00
|
|
|
Value = PtrWord | (IntWord << IntShift);
|
Eliminate redundant bitwise operations when using a llvm/ADT/PointerUnion.
For comparison, with this code sample:
PointerUnion<int *, char *> Data;
PointerUnion<int *, char *> foo1() {
Data = new int;
return new int;
}
PointerUnion<int *, char *> foo2() {
Data = new char;
return new char;
}
Before this patch we would get:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%.masked.i = and i64 %2, -3
%5 = or i64 %4, %.masked.i
store i64 %5, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%6 = tail call noalias i8* @_Znwm(i64 4)
%7 = ptrtoint i8* %6 to i64
%8 = and i64 %7, -3
ret i64 %8
}
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = load i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = and i64 %3, 1
%5 = or i64 %2, %4
%6 = or i64 %5, 2
store i64 %6, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%7 = tail call noalias i8* @_Znwm(i64 1)
%8 = ptrtoint i8* %7 to i64
%9 = or i64 %8, 2
ret i64 %9
}
After the patch:
define i64 @_Z4foo1v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 4)
%2 = ptrtoint i8* %1 to i64
store i64 %2, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%3 = tail call noalias i8* @_Znwm(i64 4)
%4 = ptrtoint i8* %3 to i64
ret i64 %4
}
declare noalias i8* @_Znwm(i64)
define i64 @_Z4foo2v() uwtable ssp {
%1 = tail call noalias i8* @_Znwm(i64 1)
%2 = ptrtoint i8* %1 to i64
%3 = or i64 %2, 2
store i64 %3, i64* getelementptr inbounds (%"class.llvm::PointerUnion"* @Data, i64 0, i32 0, i32 0), align 8
%4 = tail call noalias i8* @_Znwm(i64 1)
%5 = ptrtoint i8* %4 to i64
%6 = or i64 %5, 2
ret i64 %6
}
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169147 91177308-0d34-0410-b5e6-96231b3b80d8
2012-12-03 19:59:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 03:55:58 +00:00
|
|
|
PointerTy const *getAddrOfPointer() const {
|
2012-03-06 07:14:54 +00:00
|
|
|
return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerTy *getAddrOfPointer() {
|
2011-02-19 03:55:58 +00:00
|
|
|
assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
|
|
|
|
"Can only return the address if IntBits is cleared and "
|
|
|
|
"PtrTraits doesn't change the pointer");
|
2012-03-06 07:14:54 +00:00
|
|
|
return reinterpret_cast<PointerTy *>(&Value);
|
2011-02-19 03:55:58 +00:00
|
|
|
}
|
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
|
|
|
|
void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2009-03-29 00:18:42 +00:00
|
|
|
static PointerIntPair getFromOpaqueValue(void *V) {
|
|
|
|
PointerIntPair P; P.setFromOpaqueValue(V); return P;
|
|
|
|
}
|
2012-07-18 21:58:49 +00:00
|
|
|
|
|
|
|
// Allow PointerIntPairs to be created from const void * if and only if the
|
|
|
|
// pointer type could be created from a const void *.
|
|
|
|
static PointerIntPair getFromOpaqueValue(const void *V) {
|
|
|
|
(void)PtrTraits::getFromVoidPointer(V);
|
|
|
|
return getFromOpaqueValue(const_cast<void *>(V));
|
|
|
|
}
|
|
|
|
|
2008-11-30 19:10:41 +00:00
|
|
|
bool operator==(const PointerIntPair &RHS) const {return Value == RHS.Value;}
|
|
|
|
bool operator!=(const PointerIntPair &RHS) const {return Value != RHS.Value;}
|
|
|
|
bool operator<(const PointerIntPair &RHS) const {return Value < RHS.Value;}
|
|
|
|
bool operator>(const PointerIntPair &RHS) const {return Value > RHS.Value;}
|
|
|
|
bool operator<=(const PointerIntPair &RHS) const {return Value <= RHS.Value;}
|
|
|
|
bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
|
2008-11-28 23:31:44 +00:00
|
|
|
};
|
|
|
|
|
2009-12-15 07:26:43 +00:00
|
|
|
template <typename T> struct isPodLike;
|
|
|
|
template<typename PointerTy, unsigned IntBits, typename IntType>
|
|
|
|
struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType> > {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2008-11-29 01:18:05 +00:00
|
|
|
// Provide specialization of DenseMapInfo for PointerIntPair.
|
|
|
|
template<typename PointerTy, unsigned IntBits, typename IntType>
|
|
|
|
struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
|
|
|
|
typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
|
|
|
|
static Ty getEmptyKey() {
|
2012-08-24 23:29:28 +00:00
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-1);
|
2014-03-15 18:10:49 +00:00
|
|
|
Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
|
|
|
|
return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
|
2008-11-29 01:18:05 +00:00
|
|
|
}
|
|
|
|
static Ty getTombstoneKey() {
|
2012-08-24 23:29:28 +00:00
|
|
|
uintptr_t Val = static_cast<uintptr_t>(-2);
|
2009-03-29 06:33:22 +00:00
|
|
|
Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
|
2014-03-15 18:10:49 +00:00
|
|
|
return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
|
2008-11-29 01:18:05 +00:00
|
|
|
}
|
|
|
|
static unsigned getHashValue(Ty V) {
|
|
|
|
uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
|
|
|
|
return unsigned(IV) ^ unsigned(IV >> 9);
|
|
|
|
}
|
|
|
|
static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
|
|
|
|
};
|
|
|
|
|
2009-03-29 00:18:42 +00:00
|
|
|
// Teach SmallPtrSet that PointerIntPair is "basically a pointer".
|
2009-03-30 20:28:50 +00:00
|
|
|
template<typename PointerTy, unsigned IntBits, typename IntType,
|
|
|
|
typename PtrTraits>
|
|
|
|
class PointerLikeTypeTraits<PointerIntPair<PointerTy, IntBits, IntType,
|
|
|
|
PtrTraits> > {
|
2009-03-29 00:18:42 +00:00
|
|
|
public:
|
|
|
|
static inline void *
|
|
|
|
getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
|
|
|
|
return P.getOpaqueValue();
|
|
|
|
}
|
|
|
|
static inline PointerIntPair<PointerTy, IntBits, IntType>
|
|
|
|
getFromVoidPointer(void *P) {
|
|
|
|
return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
|
|
|
|
}
|
2012-07-18 21:58:49 +00:00
|
|
|
static inline PointerIntPair<PointerTy, IntBits, IntType>
|
|
|
|
getFromVoidPointer(const void *P) {
|
|
|
|
return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
|
|
|
|
}
|
2009-03-29 04:32:37 +00:00
|
|
|
enum {
|
2009-09-17 20:35:18 +00:00
|
|
|
NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits
|
2009-03-29 04:32:37 +00:00
|
|
|
};
|
2009-03-29 00:18:42 +00:00
|
|
|
};
|
|
|
|
|
2008-11-28 23:31:44 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
#endif
|