[PATCH] D75408: [ConstantFolding] Make sure GEPs are fully folded

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 29 10:16:25 PST 2020


nikic created this revision.
nikic added reviewers: spatel, lebedev.ri, efriedma.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

ConstantFolding can currently return partially folded constants if certain folds create new GEP expressions. These GEPs may be created with incorrect index types by the DL-independent layer, in which case ConstantFolding will change them to be DL-conforming.

I'm fixing this by explicitly re-running ConstantFolding in the two places I have found where this can occur (they create zero-index GEPs from bitcasts). I have to say that this is fairly ugly (especially as some care has to be taken to avoid infinite recursion), but I'm not sure how else to handle this.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75408

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/ConstProp/bitcast.ll


Index: test/Transforms/ConstProp/bitcast.ll
===================================================================
--- test/Transforms/ConstProp/bitcast.ll
+++ test/Transforms/ConstProp/bitcast.ll
@@ -77,7 +77,7 @@
 
 define i8* @bitcast_to_gep() {
 ; CHECK-LABEL: @bitcast_to_gep(
-; CHECK-NEXT:    ret i8* getelementptr inbounds (%T, %T* @G, i32 0, i32 0)
+; CHECK-NEXT:    ret i8* getelementptr inbounds (%T, %T* @G, i64 0, i32 0)
 ;
   %p = bitcast %T* @G to i8*
   ret i8* %p
@@ -85,7 +85,7 @@
 
 define i8 addrspace(1)* @addrspacecast_to_gep() {
 ; CHECK-LABEL: @addrspacecast_to_gep(
-; CHECK-NEXT:    ret i8 addrspace(1)* addrspacecast (i8* getelementptr inbounds (%T, %T* @G, i32 0, i32 0) to i8 addrspace(1)*)
+; CHECK-NEXT:    ret i8 addrspace(1)* addrspacecast (i8* getelementptr inbounds (%T, %T* @G, i64 0, i32 0) to i8 addrspace(1)*)
 ;
   %p = addrspacecast %T* @G to i8 addrspace(1)*
   ret i8 addrspace(1)* %p
Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -138,8 +138,15 @@
 
   // The code below only handles casts to vectors currently.
   auto *DestVTy = dyn_cast<VectorType>(DestTy);
-  if (!DestVTy)
-    return ConstantExpr::getBitCast(C, DestTy);
+  if (!DestVTy) {
+    Constant *NewC = ConstantExpr::getBitCast(C, DestTy);
+    // If bitcast was converted to GEP, fold with data layout to normalize
+    // index types.
+    if (isa<GEPOperator>(NewC))
+      if (Constant *FoldedGEP = ConstantFoldConstant(NewC, DL))
+        return FoldedGEP;
+    return NewC;
+  }
 
   // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
   // vector so the code below can handle it uniformly.
@@ -1368,8 +1375,19 @@
   case Instruction::SIToFP:
   case Instruction::FPToUI:
   case Instruction::FPToSI:
-  case Instruction::AddrSpaceCast:
-      return ConstantExpr::getCast(Opcode, C, DestTy);
+    return ConstantExpr::getCast(Opcode, C, DestTy);
+  case Instruction::AddrSpaceCast: {
+    Constant *NewC = ConstantExpr::getCast(Opcode, C, DestTy);
+    // Operand of addrspacecast may have been converted to a GEP, perform
+    // constant folding to normalize index types. Only do this if the
+    // addrspacecast operand is not C, as we'd recurse infinitely otherwise.
+    if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC))
+      if (NewCE->getOpcode() == Instruction::AddrSpaceCast &&
+          NewCE->getOperand(0) != C)
+        if (Constant *FoldedC = ConstantFoldConstant(NewC, DL))
+          return FoldedC;
+    return NewC;
+  }
   case Instruction::BitCast:
     return FoldBitCast(C, DestTy, DL);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75408.247450.patch
Type: text/x-patch
Size: 2702 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200229/2eeb5c41/attachment.bin>


More information about the llvm-commits mailing list