[ConstantFold] Fix bitcast to gep constant folding transform.
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 14 Dec 2015 19:30:32 +0000 (19:30 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 14 Dec 2015 19:30:32 +0000 (19:30 +0000)
Make sure to check that the destination type is sized.
A check was present but was incorrectly checking the source type
instead.

Patch by Amaury SECHET!

Differential Revision: http://reviews.llvm.org/D15264

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255536 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 68ddf096754733cd6d308cc1807471c086731609..979bbbd0ab7ddfad3818e49555a441f38a6f12f0 100644 (file)
@@ -109,7 +109,7 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
   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()));
index 094f87be92cffa31fed7fafed899561923f3c765..dfe840cc37dd60d515af01a8bae699c98136263e 100644 (file)
@@ -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
+@I = external global { i32 }
+@J = global i32* bitcast ({ i32 }* @I to i32*)
index 0bf98f35b3c8148150635e776bc8793d0d3ced5d..e3b303a350db7317c611ba9beba907a942d4e04f 100644 (file)
@@ -433,5 +433,22 @@ TEST(ConstantsTest, BuildConstantDataVectors) {
   }
 }
 
+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