blob: d09d8f36b770148382db956c64b2d982c018fea3 [file] [log] [blame]
Evandro Menezes94edf022017-02-01 02:54:34 +00001//===- X86MacroFusion.cpp - X86 Macro Fusion ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// \file This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the X86 implementation of the DAG scheduling mutation to
11// pair instructions back to back.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86MacroFusion.h"
16#include "X86Subtarget.h"
17#include "llvm/Support/CommandLine.h"
18#include "llvm/Target/TargetInstrInfo.h"
19
20#define DEBUG_TYPE "misched"
21
22using namespace llvm;
23
24static cl::opt<bool> EnableMacroFusion("x86-misched-fusion", cl::Hidden,
25 cl::desc("Enable scheduling for macro fusion."), cl::init(true));
26
27namespace {
28
NAKAMURA Takumi468487d2017-02-01 07:30:46 +000029/// \brief Verify that the instruction pair, First and Second,
Evandro Menezes94edf022017-02-01 02:54:34 +000030/// should be scheduled back to back. If either instruction is unspecified,
31/// then verify that the other instruction may be part of a pair at all.
32static bool shouldScheduleAdjacent(const X86Subtarget &ST,
33 const MachineInstr *First,
34 const MachineInstr *Second) {
35 // Check if this processor supports macro-fusion. Since this is a minor
36 // heuristic, we haven't specifically reserved a feature. hasAVX is a decent
37 // proxy for SandyBridge+.
38 if (!ST.hasAVX())
39 return false;
40
41 enum {
42 FuseTest,
43 FuseCmp,
44 FuseInc
45 } FuseKind;
46
Davide Italiano16b476f2017-02-19 21:35:41 +000047 unsigned FirstOpcode = First
48 ? First->getOpcode()
49 : static_cast<unsigned>(X86::INSTRUCTION_LIST_END);
Simon Pilgrim599b8722017-02-19 00:04:30 +000050 unsigned SecondOpcode =
Davide Italiano16b476f2017-02-19 21:35:41 +000051 Second ? Second->getOpcode()
52 : static_cast<unsigned>(X86::INSTRUCTION_LIST_END);
Evandro Menezes94edf022017-02-01 02:54:34 +000053
54 switch (SecondOpcode) {
55 default:
56 return false;
57 case X86::JE_1:
58 case X86::JNE_1:
59 case X86::JL_1:
60 case X86::JLE_1:
61 case X86::JG_1:
62 case X86::JGE_1:
63 FuseKind = FuseInc;
64 break;
65 case X86::JB_1:
66 case X86::JBE_1:
67 case X86::JA_1:
68 case X86::JAE_1:
69 FuseKind = FuseCmp;
70 break;
71 case X86::JS_1:
72 case X86::JNS_1:
73 case X86::JP_1:
74 case X86::JNP_1:
75 case X86::JO_1:
76 case X86::JNO_1:
77 FuseKind = FuseTest;
78 break;
79 }
80
81 switch (FirstOpcode) {
82 default:
83 return false;
84 case X86::TEST8rr:
85 case X86::TEST16rr:
86 case X86::TEST32rr:
87 case X86::TEST64rr:
88 case X86::TEST8ri:
89 case X86::TEST16ri:
90 case X86::TEST32ri:
91 case X86::TEST32i32:
92 case X86::TEST64i32:
93 case X86::TEST64ri32:
94 case X86::TEST8rm:
95 case X86::TEST16rm:
96 case X86::TEST32rm:
97 case X86::TEST64rm:
98 case X86::TEST8ri_NOREX:
99 case X86::AND16i16:
100 case X86::AND16ri:
101 case X86::AND16ri8:
102 case X86::AND16rm:
103 case X86::AND16rr:
104 case X86::AND32i32:
105 case X86::AND32ri:
106 case X86::AND32ri8:
107 case X86::AND32rm:
108 case X86::AND32rr:
109 case X86::AND64i32:
110 case X86::AND64ri32:
111 case X86::AND64ri8:
112 case X86::AND64rm:
113 case X86::AND64rr:
114 case X86::AND8i8:
115 case X86::AND8ri:
116 case X86::AND8rm:
117 case X86::AND8rr:
118 return true;
119 case X86::CMP16i16:
120 case X86::CMP16ri:
121 case X86::CMP16ri8:
122 case X86::CMP16rm:
123 case X86::CMP16rr:
124 case X86::CMP32i32:
125 case X86::CMP32ri:
126 case X86::CMP32ri8:
127 case X86::CMP32rm:
128 case X86::CMP32rr:
129 case X86::CMP64i32:
130 case X86::CMP64ri32:
131 case X86::CMP64ri8:
132 case X86::CMP64rm:
133 case X86::CMP64rr:
134 case X86::CMP8i8:
135 case X86::CMP8ri:
136 case X86::CMP8rm:
137 case X86::CMP8rr:
138 case X86::ADD16i16:
139 case X86::ADD16ri:
140 case X86::ADD16ri8:
141 case X86::ADD16ri8_DB:
142 case X86::ADD16ri_DB:
143 case X86::ADD16rm:
144 case X86::ADD16rr:
145 case X86::ADD16rr_DB:
146 case X86::ADD32i32:
147 case X86::ADD32ri:
148 case X86::ADD32ri8:
149 case X86::ADD32ri8_DB:
150 case X86::ADD32ri_DB:
151 case X86::ADD32rm:
152 case X86::ADD32rr:
153 case X86::ADD32rr_DB:
154 case X86::ADD64i32:
155 case X86::ADD64ri32:
156 case X86::ADD64ri32_DB:
157 case X86::ADD64ri8:
158 case X86::ADD64ri8_DB:
159 case X86::ADD64rm:
160 case X86::ADD64rr:
161 case X86::ADD64rr_DB:
162 case X86::ADD8i8:
163 case X86::ADD8mi:
164 case X86::ADD8mr:
165 case X86::ADD8ri:
166 case X86::ADD8rm:
167 case X86::ADD8rr:
168 case X86::SUB16i16:
169 case X86::SUB16ri:
170 case X86::SUB16ri8:
171 case X86::SUB16rm:
172 case X86::SUB16rr:
173 case X86::SUB32i32:
174 case X86::SUB32ri:
175 case X86::SUB32ri8:
176 case X86::SUB32rm:
177 case X86::SUB32rr:
178 case X86::SUB64i32:
179 case X86::SUB64ri32:
180 case X86::SUB64ri8:
181 case X86::SUB64rm:
182 case X86::SUB64rr:
183 case X86::SUB8i8:
184 case X86::SUB8ri:
185 case X86::SUB8rm:
186 case X86::SUB8rr:
187 return FuseKind == FuseCmp || FuseKind == FuseInc;
188 case X86::INC16r:
189 case X86::INC32r:
190 case X86::INC64r:
191 case X86::INC8r:
192 case X86::DEC16r:
193 case X86::DEC32r:
194 case X86::DEC64r:
195 case X86::DEC8r:
196 return FuseKind == FuseInc;
197 case X86::INSTRUCTION_LIST_END:
198 return true;
199 }
200}
201
202/// \brief Post-process the DAG to create cluster edges between instructions
203/// that may be fused by the processor into a single operation.
204class X86MacroFusion : public ScheduleDAGMutation {
205public:
206 X86MacroFusion() {}
207
208 void apply(ScheduleDAGInstrs *DAGInstrs) override;
209};
210
211void X86MacroFusion::apply(ScheduleDAGInstrs *DAGInstrs) {
212 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
213 const X86Subtarget &ST = DAG->MF.getSubtarget<X86Subtarget>();
214
215 // For now, assume targets can only fuse with the branch.
216 SUnit &ExitSU = DAG->ExitSU;
217 MachineInstr *Branch = ExitSU.getInstr();
218 if (!shouldScheduleAdjacent(ST, nullptr, Branch))
219 return;
220
221 for (SDep &PredDep : ExitSU.Preds) {
222 if (PredDep.isWeak())
223 continue;
224 SUnit &SU = *PredDep.getSUnit();
225 MachineInstr &Pred = *SU.getInstr();
226 if (!shouldScheduleAdjacent(ST, &Pred, Branch))
227 continue;
228
229 // Create a single weak edge from SU to ExitSU. The only effect is to cause
230 // bottom-up scheduling to heavily prioritize the clustered SU. There is no
231 // need to copy predecessor edges from ExitSU to SU, since top-down
232 // scheduling cannot prioritize ExitSU anyway. To defer top-down scheduling
233 // of SU, we could create an artificial edge from the deepest root, but it
234 // hasn't been needed yet.
235 bool Success = DAG->addEdge(&ExitSU, SDep(&SU, SDep::Cluster));
236 (void)Success;
237 assert(Success && "No DAG nodes should be reachable from ExitSU");
238
239 // Adjust latency of data deps between the nodes.
240 for (SDep &PredDep : ExitSU.Preds)
241 if (PredDep.getSUnit() == &SU)
242 PredDep.setLatency(0);
243 for (SDep &SuccDep : SU.Succs)
244 if (SuccDep.getSUnit() == &ExitSU)
245 SuccDep.setLatency(0);
246
247 DEBUG(dbgs() << "Macro fuse ";
248 SU.print(dbgs(), DAG);
249 dbgs() << " - ExitSU" << '\n');
250
251 break;
252 }
253}
254
255} // end namespace
256
257namespace llvm {
258
259std::unique_ptr<ScheduleDAGMutation>
260createX86MacroFusionDAGMutation () {
261 return EnableMacroFusion ? make_unique<X86MacroFusion>() : nullptr;
262}
263
264} // end namespace llvm