Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -1275,6 +1275,22 @@ if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1)))) return X; + // If the operation is extracting a member from a small structure type, + // such as std::pair of C++, we can bypass creating the structure; e.g. + // def make_struct(X,Y) := (X << ShiftCnt) | Y + // assuming sizeof(Y) <= ShiftCnt and sizeof(X) + ShiftCnt <= OpWidth + // make_struct(X,Y).getFirstElem = ((X << ShiftCnt) | Y) >> ShiftCnt -> X + Value *Y; + if (isa(Op1) && + match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_Specific(Op1)), m_Value(Y)))) { + KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); + unsigned ShiftCnt = cast(Op1)->getZExtValue(); + unsigned Width = Op0->getType()->getScalarSizeInBits(); + unsigned WidthY = Width - YKnown.countMinLeadingZeros(); + if (WidthY <= ShiftCnt) + return X; + } + return nullptr; } @@ -1796,6 +1812,36 @@ MaxRecurse)) return V; + // If this operation is extracting a boolean member from a small structure, + // such as std::pair of C++, we can bypass creating the structure; e.g. + // def make_struct(X,Y) := (X << ShiftCnt) | Y + // assuming sizeof(Y) <= ShiftCnt and sizeof(X) + ShiftCnt <= OpWidth + // if Y is bool, make_struct(X,Y) & 1 -> Y + // if X is bool, make_struct(X,Y) & (1 << ShiftCnt) -> X << ShiftCnt + Value *Y; + if (isa(Op1) && + match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShAmt)), m_Value(Y)))) { + APInt Mask = cast(Op1)->getValue(); + unsigned ShiftCnt = ShAmt->getZExtValue(); + if (Mask == 1 || Mask == (1uLL << ShiftCnt)) { + KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); + unsigned Width = Op0->getType()->getScalarSizeInBits(); + unsigned WidthY = Width - YKnown.countMinLeadingZeros(); + if (WidthY <= ShiftCnt) { + if (WidthY == 1 && Mask == 1) + return Y; + KnownBits XKnown = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); + unsigned WidthX = Width - XKnown.countMinLeadingZeros(); + if (WidthX == 1 && Mask == (1uLL << ShiftCnt)) { + if (cast(Op0)->getOperand(0) == Y) + return cast(Op0)->getOperand(1); + else + return cast(Op0)->getOperand(0); + } + } + } + } + return nullptr; } Index: test/Transforms/InstSimplify/pair.ll =================================================================== --- /dev/null +++ test/Transforms/InstSimplify/pair.ll @@ -0,0 +1,39 @@ +; RUN: opt < %s -instsimplify -S | FileCheck %s + +define i32 @func1(i32 %a, i32 %b) { +; CHECK-LABEL: @func1( +; CHECK-NEXT: ret i32 %a + %1 = zext i32 %a to i64 + %2 = zext i32 %b to i64 + %3 = shl nuw i64 %1, 32 + %4 = or i64 %2, %3 + %5 = lshr i64 %4, 32 + %6 = trunc i64 %5 to i32 + ret i32 %6 +} + +define i64 @func2(i32 %a, i1 %b) { +; CHECK-LABEL: @func2( +; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 %b to i64 +; CHECK-NEXT: ret i64 [[ZEXT]] + %1 = zext i32 %a to i64 + %2 = zext i1 %b to i64 + %3 = shl nuw i64 %1, 32 + %4 = or i64 %2, %3 + %5 = and i64 %4, 1 + ret i64 %5 +} + + +define i64 @func3(i32 %a, i1 %b) { +; CHECK-LABEL: @func3( +; CHECK-NEXT: [[ZEXT:%.*]] = zext i1 %b to i64 +; CHECK-NEXT: [[SHL:%.*]] = shl nuw i64 [[ZEXT]], 32 +; CHECK-NEXT: ret i64 [[SHL]] + %1 = zext i1 %b to i64 + %2 = zext i32 %a to i64 + %3 = shl nuw i64 %1, 32 + %4 = or i64 %2, %3 + %5 = and i64 %4, 4294967296 + ret i64 %5 +} Index: test/Transforms/NewGVN/pair_jumpthread.ll =================================================================== --- /dev/null +++ test/Transforms/NewGVN/pair_jumpthread.ll @@ -0,0 +1,117 @@ +; RUN: opt < %s -newgvn -S | FileCheck %s +; RUN: opt < %s -newgvn -jump-threading -S | FileCheck --check-prefix=CHECK-JT %s + +define signext i32 @testBI(i32 signext %v) { +; Test with std::pair +; based on the following C++ code +; std::pair callee(int v) { +; int a = dummy(v); +; if (a) return std::make_pair(true, dummy(a)); +; else return std::make_pair(v < 0, v); +; } +; int func(int v) { +; std::pair rc = callee(v); +; if (rc.first) dummy(0); +; return rc.second; +; } + +; CHECK-LABEL: @testBI +; CHECK-LABEL: _ZL6calleei.exit +; CHECK: [[PHIOFOPS:%.*]] = phi i64 [ 1, %if.then.i ], [ {{%.*}}, %if.else.i ] +; CHECK: %tobool = icmp eq i64 [[PHIOFOPS]], 0 +; CHECK-JT-LABEL: @testBI +; CHECK-JT-LABEL: _ZL6calleei.exit.thread + +entry: + %call.i = call signext i32 @dummy(i32 signext %v) + %tobool.i = icmp eq i32 %call.i, 0 + br i1 %tobool.i, label %if.else.i, label %if.then.i + +if.then.i: ; preds = %entry + %call2.i = call signext i32 @dummy(i32 signext %call.i) + %retval.sroa.22.0.insert.ext.i.i = zext i32 %call2.i to i64 + %retval.sroa.22.0.insert.shift.i.i = shl nuw i64 %retval.sroa.22.0.insert.ext.i.i, 32 + %retval.sroa.0.0.insert.insert.i.i = or i64 %retval.sroa.22.0.insert.shift.i.i, 1 + br label %_ZL6calleei.exit + +if.else.i: ; preds = %entry + %.lobit.i = lshr i32 %v, 31 + %0 = zext i32 %.lobit.i to i64 + %retval.sroa.22.0.insert.ext.i8.i = zext i32 %v to i64 + %retval.sroa.22.0.insert.shift.i9.i = shl nuw i64 %retval.sroa.22.0.insert.ext.i8.i, 32 + %retval.sroa.0.0.insert.insert.i11.i = or i64 %retval.sroa.22.0.insert.shift.i9.i, %0 + br label %_ZL6calleei.exit + +_ZL6calleei.exit: ; preds = %if.then.i, %if.else.i + %retval.sroa.0.0.i = phi i64 [ %retval.sroa.0.0.insert.insert.i.i, %if.then.i ], [ %retval.sroa.0.0.insert.insert.i11.i, %if.else.i ] + %rc.sroa.43.0.extract.shift = lshr i64 %retval.sroa.0.0.i, 32 + %rc.sroa.43.0.extract.trunc = trunc i64 %rc.sroa.43.0.extract.shift to i32 + %1 = and i64 %retval.sroa.0.0.i, 1 + %tobool = icmp eq i64 %1, 0 + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %_ZL6calleei.exit + %call1 = call signext i32 @dummy(i32 signext 0) + br label %if.end + +if.end: ; preds = %_ZL6calleei.exit, %if.then + ret i32 %rc.sroa.43.0.extract.trunc +} + + +define signext i32 @testIB(i32 signext %v) { +; Test with std::pair +; based on the following C++ code +; std::pair callee(int v) { +; int a = dummy(v); +; if (a) return std::make_pair(dummy(v), true); +; else return std::make_pair(v, v < 0); +; } +; int func(int v) { +; std::pair rc = callee(v); +; if (rc.second) dummy(0); +; return rc.first; +; } + +; CHECK-LABEL: @testIB +; CHECK-LABEL: _ZL6calleei.exit +; CHECK: [[PHIOFOPS:%.*]] = phi i64 [ 4294967296, %if.then.i ], [ {{%.*}}, %if.else.i ] +; CHECK: %tobool = icmp eq i64 [[PHIOFOPS]], 0 +; CHECK-JT-LABEL: @testIB +; CHECK-JT-LABEL: _ZL6calleei.exit.thread + +entry: + %call.i = call signext i32 @dummy(i32 signext %v) + %tobool.i = icmp eq i32 %call.i, 0 + br i1 %tobool.i, label %if.else.i, label %if.then.i + +if.then.i: ; preds = %entry + %call1.i = call signext i32 @dummy(i32 signext %v) + %retval.sroa.0.0.insert.ext.i.i = zext i32 %call1.i to i64 + %retval.sroa.0.0.insert.insert.i.i = or i64 %retval.sroa.0.0.insert.ext.i.i, 4294967296 + br label %_ZL6calleei.exit + +if.else.i: ; preds = %entry + %.lobit.i = lshr i32 %v, 31 + %0 = zext i32 %.lobit.i to i64 + %retval.sroa.2.0.insert.shift.i8.i = shl nuw nsw i64 %0, 32 + %retval.sroa.0.0.insert.ext.i9.i = zext i32 %v to i64 + %retval.sroa.0.0.insert.insert.i10.i = or i64 %retval.sroa.2.0.insert.shift.i8.i, %retval.sroa.0.0.insert.ext.i9.i + br label %_ZL6calleei.exit + +_ZL6calleei.exit: ; preds = %if.then.i, %if.else.i + %retval.sroa.0.0.i = phi i64 [ %retval.sroa.0.0.insert.insert.i.i, %if.then.i ], [ %retval.sroa.0.0.insert.insert.i10.i, %if.else.i ] + %rc.sroa.0.0.extract.trunc = trunc i64 %retval.sroa.0.0.i to i32 + %1 = and i64 %retval.sroa.0.0.i, 4294967296 + %tobool = icmp eq i64 %1, 0 + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %_ZL6calleei.exit + %call1 = call signext i32 @dummy(i32 signext 0) + br label %if.end + +if.end: ; preds = %_ZL6calleei.exit, %if.then + ret i32 %rc.sroa.0.0.extract.trunc +} + +declare signext i32 @dummy(i32 signext %v)