[PATCH] D22122: [SCCP] Teach the pass about bitcasts

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 7 16:53:54 PDT 2016


davide created this revision.
davide added reviewers: eli.friedman, majnemer.
davide added a subscriber: llvm-commits.

My longer term goal is to remove the old interprocedural constant propagation pass code from the tree but IPCP doesn't seem to get all the cases right.
This patch is a first effort in that direction. Please take a closer look because I'm not familiar with this code and I might have gotten it completely wrong. Thanks!

http://reviews.llvm.org/D22122

Files:
  lib/Transforms/Scalar/SCCP.cpp
  test/Transforms/SCCP/bitcast.ll

Index: test/Transforms/SCCP/bitcast.ll
===================================================================
--- /dev/null
+++ test/Transforms/SCCP/bitcast.ll
@@ -0,0 +1,9 @@
+; RUN: opt < %s -ipsccp -S | FileCheck %s
+
+define i128 @vector_to_int_cast() {
+  %A = bitcast <4 x i32> <i32 1073741824, i32 1073741824, i32 1073741824, i32 1073741824> to i128
+  ret i128 %A
+}
+
+; CHECK: define i128 @vector_to_int_cast(
+; CHECK-NEXT:  ret i128 85070591750041656499021422275829170176
Index: lib/Transforms/Scalar/SCCP.cpp
===================================================================
--- lib/Transforms/Scalar/SCCP.cpp
+++ lib/Transforms/Scalar/SCCP.cpp
@@ -473,6 +473,7 @@
   void visitTerminatorInst(TerminatorInst &TI);
 
   void visitCastInst(CastInst &I);
+  void visitBitCastInst(BitCastInst &I);
   void visitSelectInst(SelectInst &I);
   void visitBinaryOperator(Instruction &I);
   void visitCmpInst(CmpInst &I);
@@ -769,6 +770,22 @@
   }
 }
 
+void SCCPSolver::visitBitCastInst(BitCastInst &I) {
+  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
+    LatticeVal OpSt = getValueState(I.getOperand(i));
+    if (OpSt.isOverdefined())
+      return markOverdefined(&I);
+    if (OpSt.isUndefined())
+      return;  // Operands are not resolved yet.
+  }
+  /* All the operands are known to be constant */
+  Constant *C = ConstantFoldCastOperand(I.getOpcode(),
+                                        getValueState(I.getOperand(0)).getConstant(),
+                                        I.getType(), DL);
+  if (isa<UndefValue>(C))
+    return;
+  markConstant(&I, C);
+}
 
 void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
   // If this returns a struct, mark all elements over defined, we don't track


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22122.63164.patch
Type: text/x-patch
Size: 1743 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160707/6399f1e5/attachment.bin>


More information about the llvm-commits mailing list