Skip to content

Commit 7d93231

Browse files
committed
fix an issue where we didn't have a type for immediated if ther were the first argument
1 parent 679ac35 commit 7d93231

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ static bool fixI8TruncUseChain(Instruction &I,
3333
std::stack<Instruction *> &ToRemove,
3434
std::map<Value *, Value *> &ReplacedValues) {
3535

36+
auto *Cmp = dyn_cast<CmpInst>(&I);
37+
3638
if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3739
if (Trunc->getDestTy()->isIntegerTy(8)) {
3840
ReplacedValues[Trunc] = Trunc->getOperand(0);
3941
ToRemove.push(Trunc);
4042
}
41-
} else if (I.getType()->isIntegerTy(8)) {
43+
} else if (I.getType()->isIntegerTy(8) ||
44+
(Cmp && Cmp->getOperand(0)->getType()->isIntegerTy(8))) {
4245
IRBuilder<> Builder(&I);
4346

4447
std::vector<Value *> NewOperands;
45-
Type *InstrType = nullptr;
48+
Type *InstrType = IntegerType::get(I.getContext(), 32);
4649
for (unsigned OpIdx = 0; OpIdx < I.getNumOperands(); ++OpIdx) {
4750
Value *Op = I.getOperand(OpIdx);
4851
if (ReplacedValues.count(Op)) {
@@ -66,23 +69,21 @@ static bool fixI8TruncUseChain(Instruction &I,
6669
if (auto *BO = dyn_cast<BinaryOperator>(&I))
6770
NewInst =
6871
Builder.CreateBinOp(BO->getOpcode(), NewOperands[0], NewOperands[1]);
69-
else if (auto *Cmp = dyn_cast<CmpInst>(&I))
72+
else if (Cmp) {
7073
NewInst = Builder.CreateCmp(Cmp->getPredicate(), NewOperands[0],
7174
NewOperands[1]);
72-
else if (auto *Cast = dyn_cast<CastInst>(&I))
73-
NewInst = Builder.CreateCast(Cast->getOpcode(), NewOperands[0],
74-
Cast->getDestTy());
75-
else if (auto *UnaryOp = dyn_cast<UnaryOperator>(&I))
75+
Cmp->replaceAllUsesWith(NewInst);
76+
} else if (auto *UnaryOp = dyn_cast<UnaryOperator>(&I))
7677
NewInst = Builder.CreateUnOp(UnaryOp->getOpcode(), NewOperands[0]);
7778

7879
if (NewInst) {
7980
ReplacedValues[&I] = NewInst;
8081
ToRemove.push(&I);
8182
}
82-
} else if (auto *Sext = dyn_cast<SExtInst>(&I)) {
83-
if (Sext->getSrcTy()->isIntegerTy(8)) {
84-
ToRemove.push(Sext);
85-
Sext->replaceAllUsesWith(ReplacedValues[Sext->getOperand(0)]);
83+
} else if (auto *Cast = dyn_cast<CastInst>(&I)) {
84+
if (Cast->getSrcTy()->isIntegerTy(8)) {
85+
ToRemove.push(Cast);
86+
Cast->replaceAllUsesWith(ReplacedValues[Cast->getOperand(0)]);
8687
}
8788
}
8889

llvm/test/CodeGen/DirectX/legalize-i8.ll

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
22

3+
define i32 @removal_only_test(i32 %a) {
4+
; CHECK-LABEL: define i32 @removal_only_test(
5+
; CHECK-SAME: i32 [[A:%.*]]) {
6+
; CHECK: ret i32 [[A]]
7+
%1 = trunc nsw i32 %a to i8
8+
%3 = sext i8 %1 to i32
9+
ret i32 %3
10+
}
11+
312
define i32 @i8trunc(float %0) #0 {
13+
; CHECK-LABEL: define i32 @i8trunc(
414
; CHECK-NOT: %4 = trunc nsw i32 %3 to i8
515
; CHECK: add i32
616
; CHECK-NEXT: srem i32
@@ -36,3 +46,42 @@ define i32 @i8trunc(float %0) #0 {
3646
%18 = sext i8 %17 to i32
3747
ret i32 %18
3848
}
49+
50+
define i32 @cast_removal_test(i32 %a) {
51+
; CHECK-LABEL: define i32 @cast_removal_test(
52+
; CHECK-SAME: i32 [[A:%.*]]) {
53+
; CHECK-NOT: trunc
54+
; CHECK-NOT: zext i8
55+
; CHECK-NOT: sext i8
56+
; CHECK: add i32 [[A]], [[A]]
57+
%1 = trunc nsw i32 %a to i8
58+
%2 = zext i8 %1 to i32
59+
%3 = sext i8 %1 to i32
60+
%4 = add i32 %2, %3
61+
ret i32 %4
62+
}
63+
64+
define i1 @trunc_cmp_test(i32 %a, i32 %b) {
65+
; CHECK-LABEL: define i1 @trunc_cmp_test(
66+
; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
67+
; CHECK: icmp slt i32 [[A]], [[B]]
68+
; CHECK: icmp sgt i32 [[A]], [[B]]
69+
%1 = trunc nsw i32 %a to i8
70+
%2 = trunc nsw i32 %b to i8
71+
%3 = icmp slt i8 %1, %2
72+
%4 = icmp sgt i8 %1, %2
73+
%5 = and i1 %3, %4
74+
ret i1 %5
75+
}
76+
77+
define i32 @first_operand_imm_test(i32 %a) {
78+
; CHECK-LABEL: define i32 @first_operand_imm_test(
79+
; CHECK-SAME: i32 [[A:%.*]]) {
80+
; CHECK-NOT: trunc
81+
; CHECK: sub i32 0, [[A]]
82+
; CHECK-NOT: sext i8
83+
%1 = trunc nsw i32 %a to i8
84+
%2 = sub i8 0, %1
85+
%3 = sext i8 %2 to i32
86+
ret i32 %3
87+
}

0 commit comments

Comments
 (0)