From 6475d9434ff3e981160f85d3f1aa07c9e0ace6f2 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 6 Feb 2011 20:11:56 +0000 Subject: [PATCH] When loading from a constant, fold inttoptr if the integer type and the resulting pointer type both have the same size. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124987 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ConstantFolding.cpp | 11 +++++++++++ test/Transforms/ConstProp/loads.ll | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 243611053c2..6c99ad32bbe 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -340,6 +340,17 @@ static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, return true; } + if (ConstantExpr *CE = dyn_cast(C)) { + if (CE->getOpcode() == Instruction::IntToPtr) { + uint64_t PtrSize = TD.getTypeAllocSize(C->getType()); + uint64_t IntSize = TD.getTypeAllocSize(C->getOperand(0)->getType()); + + if (PtrSize == IntSize) + return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr, + BytesLeft, TD); + } + } + // Otherwise, unknown initializer type. return false; } diff --git a/test/Transforms/ConstProp/loads.ll b/test/Transforms/ConstProp/loads.ll index 9fbba2b3552..74d80aa1872 100644 --- a/test/Transforms/ConstProp/loads.ll +++ b/test/Transforms/ConstProp/loads.ll @@ -120,3 +120,20 @@ define i1 @test13() { ; CHECK: @test13 ; CHECK: ret i1 false } + +@g6 = constant [2 x i8*] [i8* inttoptr (i64 1 to i8*), i8* inttoptr (i64 2 to i8*)] +define i64 @test14() nounwind { +entry: + %tmp = load i64* bitcast ([2 x i8*]* @g6 to i64*) + ret i64 %tmp +; CHECK: @test14 +; CHECK: ret i64 1 +} + +define i64 @test15() nounwind { +entry: + %tmp = load i64* bitcast (i8** getelementptr inbounds ([2 x i8*]* @g6, i32 0, i64 1) to i64*) + ret i64 %tmp +; CHECK: @test15 +; CHECK: ret i64 2 +} -- 2.34.1