[PATCH] D15264: Fix bitcast to gep constant folding transform.

Amaury SECHET via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 9 21:58:33 PST 2015


deadalnix updated this revision to Diff 42387.
deadalnix added a comment.

Fix rebase conflict and ping.


http://reviews.llvm.org/D15264

Files:
  lib/IR/ConstantFold.cpp
  test/Assembler/ConstantExprFoldCast.ll
  unittests/IR/ConstantsTest.cpp

Index: unittests/IR/ConstantsTest.cpp
===================================================================
--- unittests/IR/ConstantsTest.cpp
+++ unittests/IR/ConstantsTest.cpp
@@ -433,5 +433,22 @@
   }
 }
 
+TEST(ConstantsTest, BitcastToGEP) {
+  LLVMContext Context;
+  std::unique_ptr<Module> M(new Module("MyModule", Context));
+
+  auto *i32 = Type::getInt32Ty(Context);
+  auto *U = StructType::create(Context, "Unsized");
+  Type *EltTys[] = {i32, U};
+  auto *S = StructType::create(EltTys);
+
+  auto *G = new GlobalVariable(*M, S, false,
+                               GlobalValue::ExternalLinkage, nullptr);
+  auto *PtrTy = PointerType::get(i32, 0);
+  auto *C = ConstantExpr::getBitCast(G, PtrTy);
+  ASSERT_EQ(dyn_cast<ConstantExpr>(C)->getOpcode(),
+            Instruction::BitCast);
+}
+
 }  // end anonymous namespace
 }  // end namespace llvm
Index: test/Assembler/ConstantExprFoldCast.ll
===================================================================
--- test/Assembler/ConstantExprFoldCast.ll
+++ test/Assembler/ConstantExprFoldCast.ll
@@ -15,3 +15,7 @@
 
 ; Address space cast AS0 null-> AS1 null
 @H = global i32 addrspace(1)* addrspacecast(i32* null to i32 addrspace(1)*)
+
+; Bitcast -> GEP
+ at I = external global { i32 }
+ at J = global i32* bitcast ({ i32 }* @I to i32*)
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.42387.patch
Type: text/x-patch
Size: 1896 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151210/9ec9d999/attachment.bin>


More information about the llvm-commits mailing list