blob: cb85bd30e678be57519f93c385b864174f82a59b [file] [log] [blame]
Chris Lattnera5a91b12005-08-17 19:33:03 +00001//===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC,
11// converting from a legalized dag to a PPC dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PowerPC.h"
16#include "PPC32TargetMachine.h"
17#include "PPC32ISelLowering.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/CodeGen/SelectionDAGISel.h"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/MathExtras.h"
24using namespace llvm;
25
26namespace {
27 Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
28 Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
29 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
30
31 //===--------------------------------------------------------------------===//
32 /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine
33 /// instructions for SelectionDAG operations.
34 ///
35 class PPC32DAGToDAGISel : public SelectionDAGISel {
36 PPC32TargetLowering PPC32Lowering;
37
38 unsigned GlobalBaseReg;
39 bool GlobalBaseInitialized;
40 public:
41 PPC32DAGToDAGISel(TargetMachine &TM)
42 : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {}
43
44 /// runOnFunction - Override this function in order to reset our
45 /// per-function variables.
46 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
48 GlobalBaseInitialized = false;
49 return SelectionDAGISel::runOnFunction(Fn);
50 }
51
52 /// getI32Imm - Return a target constant with the specified value, of type
53 /// i32.
54 inline SDOperand getI32Imm(unsigned Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i32);
56 }
57
58 // Select - Convert the specified operand from a target-independent to a
59 // target-specific node if it hasn't already been changed.
60 SDOperand Select(SDOperand Op);
61
62 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
63 unsigned OCHi, unsigned OCLo,
64 bool IsArithmetic = false,
65 bool Negate = false);
66
67 /// InstructionSelectBasicBlock - This callback is invoked by
68 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
69 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
70 DEBUG(BB->dump());
71 // Codegen the basic block.
72 Select(DAG.getRoot());
73 DAG.RemoveDeadNodes();
74 DAG.viewGraph();
75 }
76
77 virtual const char *getPassName() const {
78 return "PowerPC DAG->DAG Pattern Instruction Selection";
79 }
80 };
81}
82
83// Immediate constant composers.
84// Lo16 - grabs the lo 16 bits from a 32 bit constant.
85// Hi16 - grabs the hi 16 bits from a 32 bit constant.
86// HA16 - computes the hi bits required if the lo bits are add/subtracted in
87// arithmethically.
88static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
89static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
90static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
91
92// isIntImmediate - This method tests to see if a constant operand.
93// If so Imm will receive the 32 bit value.
94static bool isIntImmediate(SDOperand N, unsigned& Imm) {
95 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
96 Imm = (unsigned)CN->getSignExtended();
97 return true;
98 }
99 return false;
100}
101
102// SelectIntImmediateExpr - Choose code for integer operations with an immediate
103// operand.
104SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
105 unsigned OCHi, unsigned OCLo,
106 bool IsArithmetic,
107 bool Negate) {
108 // Check to make sure this is a constant.
109 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS);
110 // Exit if not a constant.
111 if (!CN) return 0;
112 // Extract immediate.
113 unsigned C = (unsigned)CN->getValue();
114 // Negate if required (ISD::SUB).
115 if (Negate) C = -C;
116 // Get the hi and lo portions of constant.
117 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
118 unsigned Lo = Lo16(C);
119
120 // If two instructions are needed and usage indicates it would be better to
121 // load immediate into a register, bail out.
122 if (Hi && Lo && CN->use_size() > 2) return false;
123
124 // Select the first operand.
125 SDOperand Opr0 = Select(LHS);
126
127 if (Lo) // Add in the lo-part.
128 Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo));
129 if (Hi) // Add in the hi-part.
130 Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi));
131 return Opr0.Val;
132}
133
134
135// Select - Convert the specified operand from a target-independent to a
136// target-specific node if it hasn't already been changed.
137SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) {
138 SDNode *N = Op.Val;
139 if (N->getOpcode() >= ISD::BUILTIN_OP_END)
140 return Op; // Already selected.
141
142 switch (N->getOpcode()) {
143 default:
144 std::cerr << "Cannot yet select: ";
145 N->dump();
146 std::cerr << "\n";
147 abort();
148 case ISD::EntryToken: // These leaves remain the same.
149 case ISD::UNDEF:
150 return Op;
151 case ISD::TokenFactor: {
152 SDOperand New;
153 if (N->getNumOperands() == 2) {
154 SDOperand Op0 = Select(N->getOperand(0));
155 SDOperand Op1 = Select(N->getOperand(1));
156 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
157 } else {
158 std::vector<SDOperand> Ops;
159 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
160 Ops.push_back(Select(N->getOperand(0)));
161 New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);
162 }
163
164 if (New.Val != N) {
165 CurDAG->ReplaceAllUsesWith(N, New.Val);
166 N = New.Val;
167 }
168 break;
169 }
170 case ISD::CopyFromReg: {
171 SDOperand Chain = Select(N->getOperand(0));
172 if (Chain == N->getOperand(0)) return Op; // No change
173 SDOperand New = CurDAG->getCopyFromReg(Chain,
174 cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0));
175 return New.getValue(Op.ResNo);
176 }
177 case ISD::CopyToReg: {
178 SDOperand Chain = Select(N->getOperand(0));
179 SDOperand Reg = N->getOperand(1);
180 SDOperand Val = Select(N->getOperand(2));
181 if (Chain != N->getOperand(0) || Val != N->getOperand(2)) {
182 SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other,
183 Chain, Reg, Val);
184 CurDAG->ReplaceAllUsesWith(N, New.Val);
185 N = New.Val;
186 }
187 break;
188 }
189 case ISD::Constant: {
190 assert(N->getValueType(0) == MVT::i32);
191 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
192 if ((unsigned)(short)v == v) {
193 CurDAG->SelectNodeTo(N, MVT::i32, PPC::LI, getI32Imm(v));
194 break;
195 } else {
196 SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32,
197 getI32Imm(unsigned(v) >> 16));
198 CurDAG->SelectNodeTo(N, MVT::i32, PPC::ORI, Top, getI32Imm(v & 0xFFFF));
199 break;
200 }
201 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000202 case ISD::SIGN_EXTEND_INREG:
203 switch(cast<VTSDNode>(N->getOperand(1))->getVT()) {
204 default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break;
205 case MVT::i16:
206 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSH, Select(N->getOperand(0)));
207 break;
208 case MVT::i8:
209 CurDAG->SelectNodeTo(N, MVT::i32, PPC::EXTSB, Select(N->getOperand(0)));
210 break;
211 case MVT::i1:
212 CurDAG->SelectNodeTo(N, MVT::i32, PPC::SUBFIC, Select(N->getOperand(0)),
213 getI32Imm(0));
214 break;
215 }
216 break;
217 case ISD::CTLZ:
218 assert(N->getValueType(0) == MVT::i32);
219 CurDAG->SelectNodeTo(N, MVT::i32, PPC::CNTLZW, Select(N->getOperand(0)));
220 break;
Chris Lattnera5a91b12005-08-17 19:33:03 +0000221 case ISD::ADD: {
222 MVT::ValueType Ty = N->getValueType(0);
223 if (Ty == MVT::i32) {
224 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
225 PPC::ADDIS, PPC::ADDI, true)) {
226 CurDAG->ReplaceAllUsesWith(N, I);
227 N = I;
228 } else {
229 CurDAG->SelectNodeTo(N, Ty, PPC::ADD, Select(N->getOperand(0)),
230 Select(N->getOperand(1)));
231 }
232 break;
233 }
234
235 if (!NoExcessFPPrecision) { // Match FMA ops
236 if (N->getOperand(0).getOpcode() == ISD::MUL &&
237 N->getOperand(0).Val->hasOneUse()) {
238 ++FusedFP; // Statistic
239 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
240 Select(N->getOperand(0).getOperand(0)),
241 Select(N->getOperand(0).getOperand(1)),
242 Select(N->getOperand(1)));
243 break;
244 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
245 N->getOperand(1).hasOneUse()) {
246 ++FusedFP; // Statistic
247 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS,
248 Select(N->getOperand(1).getOperand(0)),
249 Select(N->getOperand(1).getOperand(1)),
250 Select(N->getOperand(0)));
251 break;
252 }
253 }
254
255 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS,
256 Select(N->getOperand(0)), Select(N->getOperand(1)));
257 break;
258 }
259 case ISD::SUB: {
260 MVT::ValueType Ty = N->getValueType(0);
261 if (Ty == MVT::i32) {
262 unsigned Imm;
263 if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) {
264 CurDAG->SelectNodeTo(N, Ty, PPC::SUBFIC, Select(N->getOperand(1)),
265 getI32Imm(Lo16(Imm)));
266 break;
267 }
268 if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1),
269 PPC::ADDIS, PPC::ADDI, true, true)) {
270 CurDAG->ReplaceAllUsesWith(N, I);
271 N = I;
272 } else {
273 CurDAG->SelectNodeTo(N, Ty, PPC::SUBF, Select(N->getOperand(1)),
274 Select(N->getOperand(0)));
275 }
276 break;
277 }
278
279 if (!NoExcessFPPrecision) { // Match FMA ops
280 if (N->getOperand(0).getOpcode() == ISD::MUL &&
281 N->getOperand(0).Val->hasOneUse()) {
282 ++FusedFP; // Statistic
283 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS,
284 Select(N->getOperand(0).getOperand(0)),
285 Select(N->getOperand(0).getOperand(1)),
286 Select(N->getOperand(1)));
287 break;
288 } else if (N->getOperand(1).getOpcode() == ISD::MUL &&
289 N->getOperand(1).Val->hasOneUse()) {
290 ++FusedFP; // Statistic
291 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS,
292 Select(N->getOperand(1).getOperand(0)),
293 Select(N->getOperand(1).getOperand(1)),
294 Select(N->getOperand(0)));
295 break;
296 }
297 }
298 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS,
299 Select(N->getOperand(0)),
300 Select(N->getOperand(1)));
301 break;
Nate Begeman26653502005-08-17 23:46:35 +0000302 }
Nate Begemanb5a06682005-08-18 00:21:41 +0000303 case ISD::MUL: {
304 unsigned Imm, Opc;
305 if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) {
306 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::MULLI,
307 Select(N->getOperand(0)), getI32Imm(Lo16(Imm)));
308 break;
309 }
310 switch (N->getValueType(0)) {
311 default: assert(0 && "Unhandled multiply type!");
312 case MVT::i32: Opc = PPC::MULLW; break;
313 case MVT::f32: Opc = PPC::FMULS; break;
314 case MVT::f64: Opc = PPC::FMUL; break;
315 }
316 CurDAG->SelectNodeTo(N, N->getValueType(0), Opc, Select(N->getOperand(0)),
317 Select(N->getOperand(1)));
318 break;
319 }
Nate Begeman305a1c72005-08-18 03:04:18 +0000320 case ISD::MULHS:
Nate Begemanb5a06682005-08-18 00:21:41 +0000321 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000322 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHW, Select(N->getOperand(0)),
323 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000324 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000325 case ISD::MULHU:
Nate Begemanb5a06682005-08-18 00:21:41 +0000326 assert(N->getValueType(0) == MVT::i32);
Nate Begeman305a1c72005-08-18 03:04:18 +0000327 CurDAG->SelectNodeTo(N, MVT::i32, PPC::MULHWU, Select(N->getOperand(0)),
328 Select(N->getOperand(1)));
Nate Begemanb5a06682005-08-18 00:21:41 +0000329 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000330 case ISD::FABS:
Nate Begeman6a7d6112005-08-18 00:53:47 +0000331 CurDAG->SelectNodeTo(N, N->getValueType(0), PPC::FABS,
332 Select(N->getOperand(0)));
333 break;
Nate Begeman305a1c72005-08-18 03:04:18 +0000334 case ISD::FP_EXTEND:
335 assert(MVT::f64 == N->getValueType(0) &&
336 MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND");
337 CurDAG->SelectNodeTo(N, MVT::f64, PPC::FMR, Select(N->getOperand(0)));
338 break;
339 case ISD::FP_ROUND:
340 assert(MVT::f32 == N->getValueType(0) &&
341 MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND");
342 CurDAG->SelectNodeTo(N, MVT::f32, PPC::FRSP, Select(N->getOperand(0)));
343 break;
Nate Begeman26653502005-08-17 23:46:35 +0000344 case ISD::FNEG: {
345 SDOperand Val = Select(N->getOperand(0));
346 MVT::ValueType Ty = N->getValueType(0);
347 if (Val.Val->hasOneUse()) {
348 unsigned Opc;
349 switch (Val.getTargetOpcode()) {
350 default: Opc = 0; break;
351 case PPC::FABS: Opc = PPC::FNABS; break;
352 case PPC::FMADD: Opc = PPC::FNMADD; break;
353 case PPC::FMADDS: Opc = PPC::FNMADDS; break;
354 case PPC::FMSUB: Opc = PPC::FNMSUB; break;
355 case PPC::FMSUBS: Opc = PPC::FNMSUBS; break;
356 }
357 // If we inverted the opcode, then emit the new instruction with the
358 // inverted opcode and the original instruction's operands. Otherwise,
359 // fall through and generate a fneg instruction.
360 if (Opc) {
361 if (PPC::FNABS == Opc)
362 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0));
363 else
364 CurDAG->SelectNodeTo(N, Ty, Opc, Val.getOperand(0),
365 Val.getOperand(1), Val.getOperand(2));
366 break;
367 }
368 }
369 CurDAG->SelectNodeTo(N, Ty, PPC::FNEG, Val);
370 break;
371 }
Nate Begeman6a7d6112005-08-18 00:53:47 +0000372 case ISD::FSQRT: {
373 MVT::ValueType Ty = N->getValueType(0);
374 CurDAG->SelectNodeTo(N, Ty, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS,
375 Select(N->getOperand(0)));
376 break;
377 }
Chris Lattnera5a91b12005-08-17 19:33:03 +0000378 case ISD::RET: {
379 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
380
381 if (N->getNumOperands() > 1) {
382 SDOperand Val = Select(N->getOperand(1));
383 switch (N->getOperand(1).getValueType()) {
384 default: assert(0 && "Unknown return type!");
385 case MVT::f64:
386 case MVT::f32:
387 Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val);
388 break;
389 case MVT::i32:
390 Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val);
391 break;
392 }
393
394 if (N->getNumOperands() > 2) {
395 assert(N->getOperand(1).getValueType() == MVT::i32 &&
396 N->getOperand(2).getValueType() == MVT::i32 &&
397 N->getNumOperands() == 2 && "Unknown two-register ret value!");
398 Val = Select(N->getOperand(2));
399 Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Val);
400 }
401 }
402
403 // Finally, select this to a blr (return) instruction.
404 CurDAG->SelectNodeTo(N, MVT::Other, PPC::BLR, Chain);
405 break;
406 }
407 }
408 return SDOperand(N, 0);
409}
410
411
412/// createPPC32ISelDag - This pass converts a legalized DAG into a
413/// PowerPC-specific DAG, ready for instruction scheduling.
414///
415FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) {
416 return new PPC32DAGToDAGISel(TM);
417}
418