[PATCH] D15264: Fix bitcast to gep constant folding transform.
Amaury SECHET via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 5 23:37:41 PST 2015
deadalnix updated this revision to Diff 42004.
deadalnix added a comment.
Add test
http://reviews.llvm.org/D15264
Files:
lib/IR/ConstantFold.cpp
unittests/IR/ConstantsTest.cpp
Index: unittests/IR/ConstantsTest.cpp
===================================================================
--- unittests/IR/ConstantsTest.cpp
+++ unittests/IR/ConstantsTest.cpp
@@ -382,5 +382,31 @@
ASSERT_EQ(unwrap<GlobalAlias>(AliasRef)->getAliasee(), Aliasee);
}
+TEST(ConstantsTest, BitcastToGEP) {
+ LLVMContext Context;
+ std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+ auto *i32 = Type::getInt32Ty(Context);
+ Type *S1Elts[] = {i32, i32};
+ auto *S1 = StructType::create(S1Elts);
+
+ auto *G1 = new GlobalVariable(*M, S1, false,
+ GlobalValue::ExternalLinkage, nullptr);
+ auto *PtrTy = PointerType::get(i32, 0);
+ auto *C1 = ConstantExpr::getBitCast(G1, PtrTy);
+ ASSERT_EQ(dyn_cast<ConstantExpr>(C1)->getOpcode(),
+ Instruction::GetElementPtr);
+
+ auto *U = StructType::create(Context, "Unsized");
+ Type *S2Elts[] = {i32, U};
+ auto *S2 = StructType::create(S2Elts);
+
+ auto *G2 = new GlobalVariable(*M, S2, false,
+ GlobalValue::ExternalLinkage, nullptr);
+ auto *C2 = ConstantExpr::getBitCast(G2, PtrTy);
+ ASSERT_EQ(dyn_cast<ConstantExpr>(C2)->getOpcode(),
+ Instruction::BitCast);
+}
+
} // end anonymous namespace
} // end namespace llvm
Index: lib/IR/ConstantFold.cpp
===================================================================
--- lib/IR/ConstantFold.cpp
+++ lib/IR/ConstantFold.cpp
@@ -109,7 +109,7 @@
if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
if (PTy->getAddressSpace() == DPTy->getAddressSpace()
- && DPTy->getElementType()->isSized()) {
+ && PTy->getElementType()->isSized()) {
SmallVector<Value*, 8> IdxList;
Value *Zero =
Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15264.42004.patch
Type: text/x-patch
Size: 1875 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151206/98eb1a85/attachment.bin>
More information about the llvm-commits
mailing list