LLVM: lib/Target/NVPTX/NVPTXIRPeephole.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
30
31#define DEBUG_TYPE "nvptx-ir-peephole"
32
33using namespace llvm;
34
38
41
43 Value *OtherOperand = nullptr;
44 bool IsFirstOperand = false;
45
46
47 if (FMul0 && FMul0->getOpcode() == Instruction::FMul && FMul0->hasOneUse() &&
48 FMul0->hasAllowContract()) {
50 OtherOperand = Op1;
51 IsFirstOperand = true;
52 } else if (FMul1 && FMul1->getOpcode() == Instruction::FMul &&
53 FMul1->hasOneUse() && FMul1->hasAllowContract()) {
55 OtherOperand = Op0;
56 IsFirstOperand = false;
57 } else {
58 return false;
59 }
60
61 bool IsFSub = BI->getOpcode() == Instruction::FSub;
63 const char *OpName = IsFSub ? "FSub" : "FAdd";
64 dbgs() << "Found " << OpName << " with FMul (single use) as "
65 << (IsFirstOperand ? "first" : "second") << " operand: " << *BI
66 << "\n";
67 });
68
69 Value *MulOp0 = FMul->getOperand(0);
70 Value *MulOp1 = FMul->getOperand(1);
72 Value *FMA = nullptr;
73
74 if (!IsFSub) {
75
76
77 FMA = Builder.CreateIntrinsic(Intrinsic::fma, {BI->getType()},
78 {MulOp0, MulOp1, OtherOperand});
79 } else {
80 if (IsFirstOperand) {
81
82 Value *NegOtherOp =
84 FMA = Builder.CreateIntrinsic(Intrinsic::fma, {BI->getType()},
85 {MulOp0, MulOp1, NegOtherOp});
86 } else {
87
88 Value *NegMulOp0 =
89 Builder.CreateFNegFMF(MulOp0, FMul->getFastMathFlags());
90 FMA = Builder.CreateIntrinsic(Intrinsic::fma, {BI->getType()},
91 {NegMulOp0, MulOp1, OtherOperand});
92 }
93 }
94
95
101 FMAInst->setFastMathFlags(NewFMF);
102
104 const char *OpName = IsFSub ? "FSub" : "FAdd";
105 dbgs() << "Replacing " << OpName << " with FMA: " << *FMA << "\n";
106 });
109 FMul->eraseFromParent();
110 return true;
111}
112
115
116
119
120 if (BI->getOpcode() != Instruction::FAdd &&
121 BI->getOpcode() != Instruction::FSub)
122 continue;
123
124
125 if (!BI->hasAllowContract())
126 continue;
127
128
129 if (!BI->getType()->isFloatTy() && !BI->getType()->isDoubleTy())
130 continue;
131
134 }
135 }
137}
138
139namespace {
140
141struct NVPTXIRPeephole : public FunctionPass {
142 static char ID;
143 NVPTXIRPeephole() : FunctionPass(ID) {}
145};
146
147}
148
149char NVPTXIRPeephole::ID = 0;
150INITIALIZE_PASS(NVPTXIRPeephole, "nvptx-ir-peephole", "NVPTX IR Peephole",
151 false, false)
152
154
156 return new NVPTXIRPeephole();
157}
158
Expand Atomic instructions
static bool runOnFunction(Function &F, bool PostInlining)
static bool tryFoldBinaryFMul(BinaryOperator *BI)
Definition NVPTXIRPeephole.cpp:35
static bool foldFMA(Function &F)
Definition NVPTXIRPeephole.cpp:113
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
BinaryOps getOpcode() const
Represents analyses that only rely on functions' control flow.
Convenience struct for specifying and reasoning about fast-math flags.
static FastMathFlags intersectRewrite(FastMathFlags LHS, FastMathFlags RHS)
Intersect rewrite-based flags.
static FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS)
Union value flags.
FunctionPass class - This class is used to implement most global optimizations.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createNVPTXIRPeepholePass()
Definition NVPTXIRPeephole.cpp:155
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition NVPTXIRPeephole.cpp:159