Skip to content

Commit 90833a4

Browse files
mmhaMorris Hafner
and
Morris Hafner
authored
[CIR] Always zero-extend shift amounts (#1568)
Negative shift amounts are undefined behavior in C and C++. Because of that we can always zero-extend the shift amount which is slightly faster on certain architectures (e. g. x86). This also matches the behavior of the original clang Codegen. Backported from llvm/llvm-project#133405 Co-authored-by: Morris Hafner <mhafner@nvidia.com>
1 parent 204c03e commit 90833a4

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -2967,10 +2967,11 @@ mlir::LogicalResult CIRToLLVMShiftOpLowering::matchAndRewrite(
29672967
// behavior might occur in the casts below as per [C99 6.5.7.3].
29682968
// Vector type shift amount needs no cast as type consistency is expected to
29692969
// be already be enforced at CIRGen.
2970+
// Negative shift amounts are undefined behavior so we can always zero extend
2971+
// the integer here.
29702972
if (cirAmtTy)
29712973
amt = getLLVMIntCast(rewriter, amt, mlir::cast<mlir::IntegerType>(llvmTy),
2972-
!cirAmtTy.isSigned(), cirAmtTy.getWidth(),
2973-
cirValTy.getWidth());
2974+
true, cirAmtTy.getWidth(), cirValTy.getWidth());
29742975

29752976
// Lower to the proper LLVM shift operation.
29762977
if (op.getIsShiftleft())

clang/test/CIR/Lowering/shift.cir

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module {
1616

1717
// Should allow shift with signed smaller amount type.
1818
%2 = cir.shift(left, %arg1 : !s32i, %arg0 : !s16i) -> !s32i
19-
// CHECK: %[[#CAST:]] = llvm.sext %{{.+}} : i16 to i32
19+
// CHECK: %[[#CAST:]] = llvm.zext %{{.+}} : i16 to i32
2020
// CHECK: llvm.shl %{{.+}}, %[[#CAST]] : i32
2121

2222
// Should allow shift with unsigned smaller amount type.

0 commit comments

Comments
 (0)