[PATCH] D38546: [ConstantFolding] Avoid assert when folding ptrtoint of vectorized GEP

Bjorn Pettersson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 24 05:08:38 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL316430: [ConstantFolding] Avoid assert when folding ptrtoint of vectorized GEP (authored by bjope).

Changed prior to commit:
  https://reviews.llvm.org/D38546?vs=119784&id=120054#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D38546

Files:
  llvm/trunk/lib/IR/ConstantFold.cpp
  llvm/trunk/test/Analysis/ConstantFolding/cast-vector.ll


Index: llvm/trunk/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp
+++ llvm/trunk/lib/IR/ConstantFold.cpp
@@ -629,6 +629,15 @@
     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
       if (CE->getOpcode() == Instruction::GetElementPtr &&
           CE->getOperand(0)->isNullValue()) {
+        // FIXME: Looks like getFoldedSizeOf(), getFoldedOffsetOf() and
+        // getFoldedAlignOf() don't handle the case when DestTy is a vector of
+        // pointers yet. We end up in asserts in CastInst::getCastOpcode (see
+        // test/Analysis/ConstantFolding/cast-vector.ll). I've only seen this
+        // happen in one "real" C-code test case, so it does not seem to be an
+        // important optimization to handle vectors here. For now, simply bail
+        // out.
+        if (DestTy->isVectorTy())
+          return nullptr;
         GEPOperator *GEPO = cast<GEPOperator>(CE);
         Type *Ty = GEPO->getSourceElementType();
         if (CE->getNumOperands() == 2) {
Index: llvm/trunk/test/Analysis/ConstantFolding/cast-vector.ll
===================================================================
--- llvm/trunk/test/Analysis/ConstantFolding/cast-vector.ll
+++ llvm/trunk/test/Analysis/ConstantFolding/cast-vector.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+; Test constant fold of constant expression GEP used by ptrtoint (the
+; "offsetof-like expression" case).
+; This used to hit an assert due to not supporting vectors in
+; llvm::ConstantFoldCastInstruction when handling ptrtoint.
+define <2 x i16> @test1() {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret <2 x i16> ptrtoint (<2 x i32*> getelementptr ([10 x i32], [10 x i32]* null, <2 x i64> zeroinitializer, <2 x i64> <i64 5, i64 7>) to <2 x i16>)
+;
+entry:
+  %gep = getelementptr inbounds [10 x i32], [10 x i32]* null, i16 0, <2 x i16> <i16 5, i16 7>
+  %vec = ptrtoint <2 x i32*> %gep to <2 x i16>
+  ret <2 x i16> %vec
+}
+
+; Test constant fold of constant expression GEP used by ptrtoint (the
+; "sizeof-like expression" case).
+; This used to hit an assert due to not supporting vectors in
+; llvm::ConstantFoldCastInstruction when handling ptrtoint.
+define <2 x i16> @test2() {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    ret <2 x i16> ptrtoint (<2 x i32*> getelementptr (i32, i32* null, <2 x i64> <i64 5, i64 7>) to <2 x i16>)
+;
+entry:
+  %gep = getelementptr i32, i32* null, <2 x i16> <i16 5, i16 7>
+  %vec = ptrtoint <2 x i32*> %gep to <2 x i16>
+  ret <2 x i16> %vec
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38546.120054.patch
Type: text/x-patch
Size: 2692 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171024/0d355071/attachment.bin>


More information about the llvm-commits mailing list