[llvm] r229124 - [InstCombine] Fix regression introduced at r227197.

Andrea Di Biagio Andrea_DiBiagio at sn.scee.net
Fri Feb 13 08:33:36 PST 2015


Author: adibiagio
Date: Fri Feb 13 10:33:34 2015
New Revision: 229124

URL: http://llvm.org/viewvc/llvm-project?rev=229124&view=rev
Log:
[InstCombine] Fix regression introduced at r227197.

This patch fixes a problem I accidentally introduced in an instruction combine
on select instructions added at r227197. That revision taught the instruction
combiner how to fold a cttz/ctlz followed by a icmp plus select into a single
cttz/ctlz with flag 'is_zero_undef' cleared.

However, the new rule added at r227197 would have produced wrong results in the
case where a cttz/ctlz with flag 'is_zero_undef' cleared was follwed by a
zero-extend or truncate. In that case, the folded instruction would have
been inserted in a wrong location thus leaving the CFG in an inconsistent
state.

This patch fixes the problem and add two reproducible test cases to
existing test 'InstCombine/select-cmp-cttz-ctlz.ll'.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/trunk/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp?rev=229124&r1=229123&r2=229124&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineSelect.cpp Fri Feb 13 10:33:34 2015
@@ -482,16 +482,12 @@ static Value *foldSelectCttzCtlz(ICmpIns
       match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS)))) {
     IntrinsicInst *II = cast<IntrinsicInst>(Count);
     IRBuilder<> Builder(II);
-    if (cast<ConstantInt>(II->getArgOperand(1))->isOne()) {
-      // Explicitly clear the 'undef_on_zero' flag.
-      IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone());
-      Type *Ty = NewI->getArgOperand(1)->getType();
-      NewI->setArgOperand(1, Constant::getNullValue(Ty));
-      Builder.Insert(NewI);
-      Count = NewI;
-    }
-
-    return Builder.CreateZExtOrTrunc(Count, ValueOnZero->getType());
+    // Explicitly clear the 'undef_on_zero' flag.
+    IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone());
+    Type *Ty = NewI->getArgOperand(1)->getType();
+    NewI->setArgOperand(1, Constant::getNullValue(Ty));
+    Builder.Insert(NewI);
+    return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType());
   }
 
   return nullptr;

Modified: llvm/trunk/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll?rev=229124&r1=229123&r2=229124&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll Fri Feb 13 10:33:34 2015
@@ -292,6 +292,33 @@ entry:
   ret i16 %cond
 }
 
+define i64 @select_bug1(i32 %x) {
+; CHECK-LABEL: @select_bug1(
+; CHECK: [[VAR1:%[a-zA-Z0-9]+]] = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
+; CHECK-NEXT: [[VAR2:%[a-zA-Z0-9]+]] = zext i32 [[VAR1]] to i64
+; CHECK-NEXT: ret i64 [[VAR2]]
+entry:
+  %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
+  %conv = zext i32 %0 to i64
+  %tobool = icmp ne i32 %x, 0
+  %cond = select i1 %tobool, i64 %conv, i64 32
+  ret i64 %cond
+}
+
+define i16 @select_bug2(i32 %x) {
+; CHECK-LABEL: @select_bug2(
+; CHECK: [[VAR1:%[a-zA-Z0-9]+]] = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
+; CHECK-NEXT: [[VAR2:%[a-zA-Z0-9]+]] = trunc i32 [[VAR1]] to i16
+; CHECK-NEXT: ret i16 [[VAR2]]
+entry:
+  %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
+  %conv = trunc i32 %0 to i16
+  %tobool = icmp ne i32 %x, 0
+  %cond = select i1 %tobool, i16 %conv, i16 32
+  ret i16 %cond
+}
+
+
 declare i16 @llvm.ctlz.i16(i16, i1)
 declare i32 @llvm.ctlz.i32(i32, i1)
 declare i64 @llvm.ctlz.i64(i64, i1)





More information about the llvm-commits mailing list