]> granicus.if.org Git - llvm/commitdiff
[InstCombine] Preserve debug value when simplifying cast-of-select
authorVedant Kumar <vsk@apple.com>
Tue, 17 Jul 2018 18:08:36 +0000 (18:08 +0000)
committerVedant Kumar <vsk@apple.com>
Tue, 17 Jul 2018 18:08:36 +0000 (18:08 +0000)
InstCombine has a cast transform that matches a cast-of-select:

  Orig = cast (Src = select Cond TV FV)

And tries to replace it with a select which has the cast folded in:

  NewSel = select Cond (cast TV) (cast FV)

The combiner does RAUW(Orig, NewSel), so any debug values for Orig would
survive the transform. But debug values for Src would be lost.

This patch teaches InstCombine to replace all debug uses of Src with
NewSel (taking care of doing any necessary DIExpression rewriting).

Differential Revision: https://reviews.llvm.org/D49270

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337310 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineCasts.cpp
test/Transforms/InstCombine/debuginfo-variables.ll

index 481e40612c34daa4f89359a1c84ffb651d1616b5..e8ea7396a96a1d15e92d0e116a4f84b3927ab48b 100644 (file)
@@ -282,8 +282,10 @@ Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
     // condition may inhibit other folds and lead to worse codegen.
     auto *Cmp = dyn_cast<CmpInst>(Sel->getCondition());
     if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType())
-      if (Instruction *NV = FoldOpIntoSelect(CI, Sel))
+      if (Instruction *NV = FoldOpIntoSelect(CI, Sel)) {
+        replaceAllDbgUsesWith(*Sel, *NV, CI, DT);
         return NV;
+      }
   }
 
   // If we are casting a PHI, then fold the cast into the PHI.
index d277b6c323367ad5fb66e35208a478016035ac09..dcb07d5e678839ee62a3fdb7ca5ec6cad829402f 100644 (file)
@@ -26,6 +26,17 @@ define i64 @test_used_sext_zext(i16 %A) {
   ret i64 %c2
 }
 
+define i32 @test_cast_select(i1 %cond) {
+; CHECK-LABEL: @test_cast_select(
+; CHECK-NEXT:  [[sel:%.*]] = select i1 %cond, i32 3, i32 5
+; CHECK-NEXT:  call void @llvm.dbg.value(metadata i32 [[sel]], {{.*}}, metadata !DIExpression())
+; CHECK-NEXT:  call void @llvm.dbg.value(metadata i32 [[sel]], {{.*}}, metadata !DIExpression())
+; CHECK-NEXT:  ret i32 [[sel]]
+  %sel = select i1 %cond, i16 3, i16 5
+  %cast = zext i16 %sel to i32
+  ret i32 %cast
+}
+
 define void @test_or(i64 %A) {
 ; CHECK-LABEL: @test_or(
 ; CHECK-NEXT:  call void @llvm.dbg.value(metadata i64 %A, {{.*}}, metadata !DIExpression(DW_OP_constu, 256, DW_OP_or, DW_OP_stack_value))