Skip to content

Commit a3fb01d

Browse files
committed
Move fneg to fsub to DXILLegalizePass
1 parent 2cdb7f3 commit a3fb01d

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

llvm/lib/Target/DirectX/DXILLegalizePass.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,20 @@ static void removeMemSet(Instruction &I,
317317
ToRemove.push_back(CI);
318318
}
319319

320+
static void updateFnegToFsub(Instruction &I,
321+
SmallVectorImpl<Instruction *> &ToRemove,
322+
DenseMap<Value *, Value *> &ReplacedValues) {
323+
const Intrinsic::ID ID = I.getOpcode();
324+
if(ID != Instruction::FNeg)
325+
return;
326+
327+
IRBuilder<> Builder(&I);
328+
Value *In = I.getOperand(0);
329+
Value *Zero = ConstantFP::get(In->getType(), -0.0);
330+
I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
331+
ToRemove.push_back(&I);
332+
}
333+
320334
namespace {
321335
class DXILLegalizationPipeline {
322336

@@ -349,6 +363,7 @@ class DXILLegalizationPipeline {
349363
LegalizationPipeline.push_back(downcastI64toI32InsertExtractElements);
350364
LegalizationPipeline.push_back(legalizeFreeze);
351365
LegalizationPipeline.push_back(removeMemSet);
366+
LegalizationPipeline.push_back(updateFnegToFsub);
352367
}
353368
};
354369

llvm/lib/Target/DirectX/DXILPrepare.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,6 @@ class DXILPrepareModule : public ModulePass {
204204

205205
I.dropUnknownNonDebugMetadata(DXILCompatibleMDs);
206206

207-
if (I.getOpcode() == Instruction::FNeg) {
208-
Builder.SetInsertPoint(&I);
209-
Value *In = I.getOperand(0);
210-
Value *Zero = ConstantFP::get(In->getType(), -0.0);
211-
I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
212-
I.eraseFromParent();
213-
continue;
214-
}
215-
216207
// Emtting NoOp bitcast instructions allows the ValueEnumerator to be
217208
// unmodified as it reserves instruction IDs during contruction.
218209
if (auto LI = dyn_cast<LoadInst>(&I)) {

llvm/test/CodeGen/DirectX/fneg-conversion.ll

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
2+
3+
define float @negateF(float %x) {
4+
; CHECK-LABEL: define float @negateF(
5+
; CHECK-SAME: float [[X:%.*]]) {
6+
; CHECK-NEXT: [[ENTRY:.*:]]
7+
; CHECK-NEXT: [[Y:%.*]] = fsub float -0.000000e+00, [[X]]
8+
; CHECK-NEXT: ret float [[Y]]
9+
entry:
10+
%y = fneg float %x
11+
ret float %y
12+
}
13+
14+
define double @negateD(double %x) {
15+
; CHECK-LABEL: define double @negateD(
16+
; CHECK-SAME: double [[X:%.*]]) {
17+
; CHECK-NEXT: [[ENTRY:.*:]]
18+
; CHECK-NEXT: [[Y:%.*]] = fsub double -0.000000e+00, [[X]]
19+
; CHECK-NEXT: ret double [[Y]]
20+
entry:
21+
%y = fneg double %x
22+
ret double %y
23+
}

0 commit comments

Comments
 (0)