[llvm] r255536 - [ConstantFold] Fix bitcast to gep constant folding transform.

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 14 11:30:33 PST 2015


Author: majnemer
Date: Mon Dec 14 13:30:32 2015
New Revision: 255536

URL: http://llvm.org/viewvc/llvm-project?rev=255536&view=rev
Log:
[ConstantFold] Fix bitcast to gep constant folding transform.

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

Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Assembler/ConstantExprFoldCast.ll
    llvm/trunk/unittests/IR/ConstantsTest.cpp

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=255536&r1=255535&r2=255536&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Mon Dec 14 13:30:32 2015
@@ -109,7 +109,7 @@ static Constant *FoldBitCast(Constant *V
   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()));

Modified: llvm/trunk/test/Assembler/ConstantExprFoldCast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprFoldCast.ll?rev=255536&r1=255535&r2=255536&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprFoldCast.ll (original)
+++ llvm/trunk/test/Assembler/ConstantExprFoldCast.ll Mon Dec 14 13:30:32 2015
@@ -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*)

Modified: llvm/trunk/unittests/IR/ConstantsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ConstantsTest.cpp?rev=255536&r1=255535&r2=255536&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ConstantsTest.cpp (original)
+++ llvm/trunk/unittests/IR/ConstantsTest.cpp Mon Dec 14 13:30:32 2015
@@ -433,5 +433,22 @@ TEST(ConstantsTest, BuildConstantDataVec
   }
 }
 
+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




More information about the llvm-commits mailing list