summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp2
-rw-r--r--llvm/test/Transforms/InstCombine/abs-1.ll30
2 files changed, 31 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index e70078a73c5..c88270fa0ea 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -818,7 +818,7 @@ static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp,
// Create the canonical compare.
Cmp.setPredicate(ICmpInst::ICMP_SLT);
- Cmp.setOperand(1, ConstantInt::getNullValue(LHS->getType()));
+ Cmp.setOperand(1, ConstantInt::getNullValue(Cmp.getOperand(0)->getType()));
// If the select operands do not change, we're done.
Value *TVal = Sel.getTrueValue();
diff --git a/llvm/test/Transforms/InstCombine/abs-1.ll b/llvm/test/Transforms/InstCombine/abs-1.ll
index ee6b6674c0b..92a9de821aa 100644
--- a/llvm/test/Transforms/InstCombine/abs-1.ll
+++ b/llvm/test/Transforms/InstCombine/abs-1.ll
@@ -101,6 +101,21 @@ define i8 @abs_canonical_4(i8 %x) {
ret i8 %abs
}
+define i32 @abs_canonical_5(i8 %x) {
+; CHECK-LABEL: @abs_canonical_5(
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[CONV]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i32 [[NEG]], i32 [[CONV]]
+; CHECK-NEXT: ret i32 [[ABS]]
+;
+ %cmp = icmp sgt i8 %x, 0
+ %conv = sext i8 %x to i32
+ %neg = sub i32 0, %conv
+ %abs = select i1 %cmp, i32 %conv, i32 %neg
+ ret i32 %abs
+}
+
; We have a canonical form of nabs to make CSE easier.
define i8 @nabs_canonical_1(i8 %x) {
@@ -159,6 +174,21 @@ define i8 @nabs_canonical_4(i8 %x) {
ret i8 %abs
}
+define i32 @nabs_canonical_5(i8 %x) {
+; CHECK-LABEL: @nabs_canonical_5(
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
+; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[CONV]]
+; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i32 [[CONV]], i32 [[NEG]]
+; CHECK-NEXT: ret i32 [[ABS]]
+;
+ %cmp = icmp sgt i8 %x, 0
+ %conv = sext i8 %x to i32
+ %neg = sub i32 0, %conv
+ %abs = select i1 %cmp, i32 %neg, i32 %conv
+ ret i32 %abs
+}
+
; The following 5 tests use a shift+add+xor to implement abs():
; B = ashr i8 A, 7 -- smear the sign bit.
; xor (add A, B), B -- add -1 and flip bits if negative
OpenPOWER on IntegriCloud