[llvm-commits] [llvm] r124987 - in /llvm/trunk: lib/Analysis/ConstantFolding.cpp test/Transforms/ConstProp/loads.ll
Anders Carlsson
andersca at mac.com
Sun Feb 6 12:11:56 PST 2011
Author: andersca
Date: Sun Feb 6 14:11:56 2011
New Revision: 124987
URL: http://llvm.org/viewvc/llvm-project?rev=124987&view=rev
Log:
When loading from a constant, fold inttoptr if the integer type and the resulting pointer type both have the same size.
Modified:
llvm/trunk/lib/Analysis/ConstantFolding.cpp
llvm/trunk/test/Transforms/ConstProp/loads.ll
Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=124987&r1=124986&r2=124987&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Sun Feb 6 14:11:56 2011
@@ -340,6 +340,17 @@
return true;
}
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(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;
}
Modified: llvm/trunk/test/Transforms/ConstProp/loads.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstProp/loads.ll?rev=124987&r1=124986&r2=124987&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/ConstProp/loads.ll (original)
+++ llvm/trunk/test/Transforms/ConstProp/loads.ll Sun Feb 6 14:11:56 2011
@@ -120,3 +120,20 @@
; CHECK: @test13
; CHECK: ret i1 false
}
+
+ at 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
+}
More information about the llvm-commits
mailing list