[DAGcombine] Do not replace truncate node by itself when doing constant folding,...
authorAmaury Sechet <deadalnix@gmail.com>
Fri, 10 Nov 2017 20:59:53 +0000 (20:59 +0000)
committerAmaury Sechet <deadalnix@gmail.com>
Fri, 10 Nov 2017 20:59:53 +0000 (20:59 +0000)
llvm-svn: 317926

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

index 557cf6d..dd8d613 100644 (file)
@@ -8360,12 +8360,18 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
   // noop truncate
   if (N0.getValueType() == N->getValueType(0))
     return N0;
-  // fold (truncate c1) -> c1
-  if (DAG.isConstantIntBuildVectorOrConstantInt(N0))
-    return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
+
   // fold (truncate (truncate x)) -> (truncate x)
   if (N0.getOpcode() == ISD::TRUNCATE)
     return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
+
+  // fold (truncate c1) -> c1
+  if (DAG.isConstantIntBuildVectorOrConstantInt(N0)) {
+    SDValue C = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
+    if (C.getNode() != N)
+      return C;
+  }
+
   // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
   if (N0.getOpcode() == ISD::ZERO_EXTEND ||
       N0.getOpcode() == ISD::SIGN_EXTEND ||