When loading from a constant, fold inttoptr if the integer type and the resulting pointer type both have the same size.
llvm-svn: 124987
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 2436110..6c99ad3 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -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;
}