[PATCH] D20144: Fix constant folding of addrspacecast

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue May 10 17:20:17 PDT 2016


arsenm created this revision.
arsenm added a subscriber: llvm-commits.

http://reviews.llvm.org/D20144

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/InstCombine/addrspacecast.ll

Index: test/Transforms/InstCombine/addrspacecast.ll
===================================================================
--- test/Transforms/InstCombine/addrspacecast.ll
+++ test/Transforms/InstCombine/addrspacecast.ll
@@ -145,3 +145,42 @@
   ret i32 %sum.inc
 }
 
+; CHECK-LABEL: @constant_fold_null(
+; CHECK: addrspacecast i32 addrspace(3)* null to i32 addrspace(4)*
+define void @constant_fold_null() #0 {
+  %cast = addrspacecast i32 addrspace(3)* null to i32 addrspace(4)*
+  store i32 7, i32 addrspace(4)* %cast
+  ret void
+}
+
+; CHECK-LABEL: @constant_fold_undef(
+; CHECK: ret i32 addrspace(4)* undef
+define i32 addrspace(4)* @constant_fold_undef() #0 {
+  %cast = addrspacecast i32 addrspace(3)* undef to i32 addrspace(4)*
+  ret i32 addrspace(4)* %cast
+}
+
+; CHECK-LABEL: @constant_fold_null_vector(
+ ; CHECK: addrspacecast <4 x i32 addrspace(3)*> zeroinitializer to <4 x i32 addrspace(4)*>
+define <4 x i32 addrspace(4)*> @constant_fold_null_vector() #0 {
+  %cast = addrspacecast <4 x i32 addrspace(3)*> zeroinitializer to <4 x i32 addrspace(4)*>
+  ret <4 x i32 addrspace(4)*> %cast
+}
+
+; CHECK-LABEL: @constant_fold_inttoptr(
+; CHECK: addrspacecast i32 addrspace(3)* inttoptr (i32 -1 to i32 addrspace(3)*) to i32 addrspace(4)*
+define void @constant_fold_inttoptr() #0 {
+  %cast = addrspacecast i32 addrspace(3)* inttoptr (i32 -1 to i32 addrspace(3)*) to i32 addrspace(4)*
+  store i32 7, i32 addrspace(4)* %cast
+  ret void
+}
+
+; CHECK-LABEL: @constant_fold_gep_inttoptr(
+; CHECK: addrspacecast i32 addrspace(3)* inttoptr (i64 1274 to i32 addrspace(3)*) to i32 addrspace(4)*
+define void @constant_fold_gep_inttoptr() #0 {
+  %k = inttoptr i32 1234 to i32 addrspace(3)*
+  %gep = getelementptr i32, i32 addrspace(3)* %k, i32 10
+  %cast = addrspacecast i32 addrspace(3)* %gep to i32 addrspace(4)*
+  store i32 7, i32 addrspace(4)* %cast
+  ret void
+}
Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -998,6 +998,8 @@
     // Fold the Instruction's operands.
     if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
       Op = ConstantFoldConstantExpression(NewCE, DL, TLI);
+    if (!Op)
+      return nullptr;
 
     Ops.push_back(Op);
   }
@@ -1041,6 +1043,10 @@
       if (FoldedOps.insert(NewCE).second)
         NewC = ConstantFoldConstantExpressionImpl(NewCE, DL, TLI, FoldedOps);
     }
+
+    if (!NewC)
+      return nullptr;
+
     Ops.push_back(NewC);
   }
 
@@ -1222,8 +1228,22 @@
   case Instruction::SIToFP:
   case Instruction::FPToUI:
   case Instruction::FPToSI:
-  case Instruction::AddrSpaceCast:
       return ConstantExpr::getCast(Opcode, C, DestTy);
+  case Instruction::AddrSpaceCast: {
+    if (isa<UndefValue>(C))
+      return UndefValue::get(DestTy);
+
+    // It is up to the target for how to constant fold immediates.
+    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+      if (CE->getOpcode() == Instruction::IntToPtr)
+        return nullptr;
+
+      if (auto *NewCE = ConstantFoldConstantExpression(CE, DL, nullptr))
+        return ConstantExpr::getAddrSpaceCast(NewCE, DestTy);
+    }
+
+    return nullptr;
+  }
   case Instruction::BitCast:
     return FoldBitCast(C, DestTy, DL);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20144.56841.patch
Type: text/x-patch
Size: 3300 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160511/3c1d742d/attachment.bin>


More information about the llvm-commits mailing list