blob: 50b10e41f14a7b29a855699ddd319b4528a07ec1 [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemana9795f82005-03-24 04:41:43 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000020#include "PPC32ISelLowering.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000021#include "llvm/Constants.h"
Nate Begemana9795f82005-03-24 04:41:43 +000022#include "llvm/Function.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begemana9795f82005-03-24 04:41:43 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/Target/TargetData.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
Nate Begemana9795f82005-03-24 04:41:43 +000038namespace {
Chris Lattner6d9aed42005-08-17 01:25:14 +000039Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
40Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
41Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Chris Lattner3c304a32005-08-05 22:05:03 +000042
Nate Begemana9795f82005-03-24 04:41:43 +000043//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000044// ISel - PPC32 specific code to select PPC32 machine instructions for
45// SelectionDAG operations.
Nate Begemana9795f82005-03-24 04:41:43 +000046//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000047
Nate Begemana9795f82005-03-24 04:41:43 +000048class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +000049 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +000050 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
51 // for sdiv and udiv until it is put into the future
52 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000053
Nate Begemana9795f82005-03-24 04:41:43 +000054 /// ExprMap - As shared expressions are codegen'd, we keep track of which
55 /// vreg the value is produced in, so we only emit one copy of each compiled
56 /// tree.
57 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +000058
59 unsigned GlobalBaseReg;
60 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +000061 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +000062public:
Nate Begeman815d6da2005-04-06 00:25:27 +000063 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
64 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000065
Nate Begemanc7b09f12005-03-25 08:34:25 +000066 /// runOnFunction - Override this function in order to reset our per-function
67 /// variables.
68 virtual bool runOnFunction(Function &Fn) {
69 // Make sure we re-emit a set of the global base reg if necessary
70 GlobalBaseInitialized = false;
71 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000072 }
73
Nate Begemana9795f82005-03-24 04:41:43 +000074 /// InstructionSelectBasicBlock - This callback is invoked by
75 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
76 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
77 DEBUG(BB->dump());
78 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +000079 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +000080 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +000081
Nate Begemana9795f82005-03-24 04:41:43 +000082 // Clear state used for selection.
83 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +000084 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +000085 }
Nate Begeman815d6da2005-04-06 00:25:27 +000086
Chris Lattner54abfc52005-08-11 17:15:31 +000087 // convenience functions for virtual register creation
88 inline unsigned MakeIntReg() {
89 return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
90 }
91 inline unsigned MakeFPReg() {
92 return RegMap->createVirtualRegister(PPC32::FPRCRegisterClass);
93 }
94
Nate Begeman815d6da2005-04-06 00:25:27 +000095 // dag -> dag expanders for integer divide by constant
96 SDOperand BuildSDIVSequence(SDOperand N);
97 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000098
Nate Begemandffcfcc2005-04-01 00:32:34 +000099 unsigned getGlobalBaseReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000100 void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000101 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000102 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begemanc24d4842005-08-10 20:52:09 +0000103 unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
Chris Lattnerb4138c42005-08-10 18:11:33 +0000104 bool SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000105 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000106 bool IsArithmetic = false, bool Negate = false);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000107 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000108 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000109
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000110 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000111 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000112
113 virtual const char *getPassName() const {
114 return "PowerPC Pattern Instruction Selection";
115 }
Nate Begemana9795f82005-03-24 04:41:43 +0000116};
117
Chris Lattner02efa6c2005-08-08 21:08:09 +0000118// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
119// any number of 0s on either side. The 1s are allowed to wrap from LSB to
120// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
121// not, since all 1s are not contiguous.
122static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
123 if (isShiftedMask_32(Val)) {
124 // look for the first non-zero bit
125 MB = CountLeadingZeros_32(Val);
126 // look for the first zero bit after the run of ones
127 ME = CountLeadingZeros_32((Val - 1) ^ Val);
128 return true;
129 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
130 // effectively look for the first zero bit
131 ME = CountLeadingZeros_32(Val) - 1;
132 // effectively look for the first one bit after the run of zeros
133 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
134 return true;
135 }
136 // no run present
137 return false;
138}
139
Chris Lattnercf1cf182005-08-08 21:10:27 +0000140// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
141// and mask opcode and mask operation.
142static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
143 bool IsShiftMask,
144 unsigned &SH, unsigned &MB, unsigned &ME) {
145 if (Shift > 31) return false;
146 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
147
148 if (Opcode == ISD::SHL) { // shift left
149 // apply shift to mask if it comes first
150 if (IsShiftMask) Mask = Mask << Shift;
151 // determine which bits are made indeterminant by shift
152 Indeterminant = ~(0xFFFFFFFFu << Shift);
153 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
154 // apply shift to mask if it comes first
155 if (IsShiftMask) Mask = Mask >> Shift;
156 // determine which bits are made indeterminant by shift
157 Indeterminant = ~(0xFFFFFFFFu >> Shift);
158 // adjust for the left rotate
159 Shift = 32 - Shift;
160 }
161
162 // if the mask doesn't intersect any Indeterminant bits
Jim Laskeycf083e32005-08-12 23:52:46 +0000163 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000164 SH = Shift;
165 // make sure the mask is still a mask (wrap arounds may not be)
166 return isRunOfOnes(Mask, MB, ME);
167 }
168
169 // can't do it
170 return false;
171}
172
Chris Lattner59b21c22005-08-09 18:29:55 +0000173// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000174// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000175static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000176 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000177 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000178 // retrieve value
Jim Laskeyb454cfd2005-08-18 00:15:15 +0000179 Imm = (unsigned)CN->getValue();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000180 // passes muster
181 return true;
182 }
183 // not a constant
184 return false;
185}
186
Jim Laskey191cf942005-08-11 21:59:23 +0000187// isOpcWithIntImmediate - This method tests to see if the node is a specific
188// opcode and that it has a immediate integer right operand.
189// If so Imm will receive the 32 bit value.
190static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) {
191 return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm);
192}
193
Chris Lattnercf1cf182005-08-08 21:10:27 +0000194// isOprShiftImm - Returns true if the specified operand is a shift opcode with
195// a immediate shift count less than 32.
196static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
197 Opc = N.getOpcode();
198 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000199 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000200}
201
202// isOprNot - Returns true if the specified operand is an xor with immediate -1.
203static bool isOprNot(SDOperand N) {
204 unsigned Imm;
Jim Laskey191cf942005-08-11 21:59:23 +0000205 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000206}
207
208// Immediate constant composers.
209// Lo16 - grabs the lo 16 bits from a 32 bit constant.
210// Hi16 - grabs the hi 16 bits from a 32 bit constant.
211// HA16 - computes the hi bits required if the lo bits are add/subtracted in
212// arithmethically.
213static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
214static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
215static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
216
Nate Begemanc7bd4822005-04-11 06:34:10 +0000217/// NodeHasRecordingVariant - If SelectExpr can always produce code for
218/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
219/// return false.
220static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
221 switch(NodeOpcode) {
222 default: return false;
223 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000225 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000226 }
227}
228
Nate Begeman3e897162005-03-31 23:55:40 +0000229/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
Nate Begemanc24d4842005-08-10 20:52:09 +0000230/// to Condition.
231static unsigned getBCCForSetCC(ISD::CondCode CC) {
232 switch (CC) {
Nate Begeman3e897162005-03-31 23:55:40 +0000233 default: assert(0 && "Unknown condition!"); abort();
234 case ISD::SETEQ: return PPC::BEQ;
235 case ISD::SETNE: return PPC::BNE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000236 case ISD::SETULT:
Nate Begeman3e897162005-03-31 23:55:40 +0000237 case ISD::SETLT: return PPC::BLT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000238 case ISD::SETULE:
Nate Begeman3e897162005-03-31 23:55:40 +0000239 case ISD::SETLE: return PPC::BLE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000240 case ISD::SETUGT:
Nate Begeman3e897162005-03-31 23:55:40 +0000241 case ISD::SETGT: return PPC::BGT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000242 case ISD::SETUGE:
Nate Begeman3e897162005-03-31 23:55:40 +0000243 case ISD::SETGE: return PPC::BGE;
244 }
Nate Begeman04730362005-04-01 04:45:11 +0000245 return 0;
246}
247
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000248/// getCRIdxForSetCC - Return the index of the condition register field
249/// associated with the SetCC condition, and whether or not the field is
250/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000251static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
252 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000253 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000254 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000255 case ISD::SETLT: Inv = false; return 0;
256 case ISD::SETUGE:
257 case ISD::SETGE: Inv = true; return 0;
258 case ISD::SETUGT:
259 case ISD::SETGT: Inv = false; return 1;
260 case ISD::SETULE:
261 case ISD::SETLE: Inv = true; return 1;
262 case ISD::SETEQ: Inv = false; return 2;
263 case ISD::SETNE: Inv = true; return 2;
264 }
265 return 0;
266}
267
Nate Begeman04730362005-04-01 04:45:11 +0000268/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
269/// and store immediate instructions.
270static unsigned IndexedOpForOp(unsigned Opcode) {
271 switch(Opcode) {
272 default: assert(0 && "Unknown opcode!"); abort();
273 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
274 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
275 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
276 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
277 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
278 case PPC::LFD: return PPC::LFDX;
279 }
280 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000281}
Nate Begeman815d6da2005-04-06 00:25:27 +0000282
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000283// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000284// a multiply.
285struct ms {
286 int m; // magic number
287 int s; // shift amount
288};
289
290struct mu {
291 unsigned int m; // magic number
292 int a; // add indicator
293 int s; // shift amount
294};
295
296/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000297/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000298/// or -1.
299static struct ms magic(int d) {
300 int p;
301 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000302 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000303 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000304
Nate Begeman815d6da2005-04-06 00:25:27 +0000305 ad = abs(d);
306 t = two31 + ((unsigned int)d >> 31);
307 anc = t - 1 - t%ad; // absolute value of nc
308 p = 31; // initialize p
309 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
310 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
311 q2 = two31/ad; // initialize q2 = 2p/abs(d)
312 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
313 do {
314 p = p + 1;
315 q1 = 2*q1; // update q1 = 2p/abs(nc)
316 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
317 if (r1 >= anc) { // must be unsigned comparison
318 q1 = q1 + 1;
319 r1 = r1 - anc;
320 }
321 q2 = 2*q2; // update q2 = 2p/abs(d)
322 r2 = 2*r2; // update r2 = rem(2p/abs(d))
323 if (r2 >= ad) { // must be unsigned comparison
324 q2 = q2 + 1;
325 r2 = r2 - ad;
326 }
327 delta = ad - r2;
328 } while (q1 < delta || (q1 == delta && r1 == 0));
329
330 mag.m = q2 + 1;
331 if (d < 0) mag.m = -mag.m; // resulting magic number
332 mag.s = p - 32; // resulting shift
333 return mag;
334}
335
336/// magicu - calculate the magic numbers required to codegen an integer udiv as
337/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
338static struct mu magicu(unsigned d)
339{
340 int p;
341 unsigned int nc, delta, q1, r1, q2, r2;
342 struct mu magu;
343 magu.a = 0; // initialize "add" indicator
344 nc = - 1 - (-d)%d;
345 p = 31; // initialize p
346 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
347 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
348 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
349 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
350 do {
351 p = p + 1;
352 if (r1 >= nc - r1 ) {
353 q1 = 2*q1 + 1; // update q1
354 r1 = 2*r1 - nc; // update r1
355 }
356 else {
357 q1 = 2*q1; // update q1
358 r1 = 2*r1; // update r1
359 }
360 if (r2 + 1 >= d - r2) {
361 if (q2 >= 0x7FFFFFFF) magu.a = 1;
362 q2 = 2*q2 + 1; // update q2
363 r2 = 2*r2 + 1 - d; // update r2
364 }
365 else {
366 if (q2 >= 0x80000000) magu.a = 1;
367 q2 = 2*q2; // update q2
368 r2 = 2*r2 + 1; // update r2
369 }
370 delta = d - 1 - r2;
371 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
372 magu.m = q2 + 1; // resulting magic number
373 magu.s = p - 32; // resulting shift
374 return magu;
375}
376}
377
378/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
379/// return a DAG expression to select that will generate the same value by
380/// multiplying by a magic number. See:
381/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
382SDOperand ISel::BuildSDIVSequence(SDOperand N) {
383 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
384 ms magics = magic(d);
385 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000386 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000387 ISelDAG->getConstant(magics.m, MVT::i32));
388 // If d > 0 and m < 0, add the numerator
389 if (d > 0 && magics.m < 0)
390 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
391 // If d < 0 and m > 0, subtract the numerator.
392 if (d < 0 && magics.m > 0)
393 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
394 // Shift right algebraic if shift value is nonzero
395 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000396 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000397 ISelDAG->getConstant(magics.s, MVT::i32));
398 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000399 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000400 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000401 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000402}
403
404/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
405/// return a DAG expression to select that will generate the same value by
406/// multiplying by a magic number. See:
407/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
408SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000409 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000410 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
411 mu magics = magicu(d);
412 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000413 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000414 ISelDAG->getConstant(magics.m, MVT::i32));
415 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000416 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000417 ISelDAG->getConstant(magics.s, MVT::i32));
418 } else {
419 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000420 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000421 ISelDAG->getConstant(1, MVT::i32));
422 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000423 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000424 ISelDAG->getConstant(magics.s-1, MVT::i32));
425 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000426 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000427}
428
Nate Begemanc7b09f12005-03-25 08:34:25 +0000429/// getGlobalBaseReg - Output the instructions required to put the
430/// base address to use for accessing globals into a register.
431///
432unsigned ISel::getGlobalBaseReg() {
433 if (!GlobalBaseInitialized) {
434 // Insert the set of GlobalBaseReg into the first MBB of the function
435 MachineBasicBlock &FirstMBB = BB->getParent()->front();
436 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000437 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000438 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
Chris Lattner3f852b42005-08-18 23:24:50 +0000439 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000440 GlobalBaseInitialized = true;
441 }
442 return GlobalBaseReg;
443}
444
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000445/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000446/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000447void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
448 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000449 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000450 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000451 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000452 bool GPOpt =
453 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
Nate Begeman27d53ba2005-08-19 03:42:28 +0000454 if (GPOpt)
455 BuildMI(BB, PPC::MFOCRF, 1, IntCR).addReg(PPC::CR7);
456 else
457 BuildMI(BB, PPC::MFCR, 0, IntCR);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000458 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000459 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000460 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
461 .addImm(31).addImm(31);
462 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
463 } else {
464 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
465 .addImm(31).addImm(31);
466 }
467}
468
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000469/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000470/// the rotate left word immediate then mask insert (rlwimi) instruction.
471/// Returns true on success, false if the caller still needs to select OR.
472///
473/// Patterns matched:
474/// 1. or shl, and 5. or and, and
475/// 2. or and, shl 6. or shl, shr
476/// 3. or shr, and 7. or shr, shl
477/// 4. or and, shr
478bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000479 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000480 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000481 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000482
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000483 SDOperand Op0 = OR.getOperand(0);
484 SDOperand Op1 = OR.getOperand(1);
485
486 unsigned Op0Opc = Op0.getOpcode();
487 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000488
Nate Begeman7ddecb42005-04-06 23:51:40 +0000489 // Verify that we have the correct opcodes
490 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
491 return false;
492 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
493 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000494
Nate Begeman7ddecb42005-04-06 23:51:40 +0000495 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +0000496 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000497 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000498 case ISD::SHL: TgtMask <<= Value; break;
499 case ISD::SRL: TgtMask >>= Value; break;
500 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000501 }
502 } else {
503 return false;
504 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000505
Nate Begeman7ddecb42005-04-06 23:51:40 +0000506 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +0000507 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000508 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000509 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000510 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000511 InsMask <<= Amount;
512 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000513 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000514 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000515 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000516 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000517 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000518 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000519 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000520 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000521 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000522 break;
523 }
524 } else {
525 return false;
526 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000527
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000528 unsigned Tmp3 = 0;
529
530 // If both of the inputs are ANDs and one of them has a logical shift by
531 // constant as its input, make that the inserted value so that we can combine
532 // the shift into the rotate part of the rlwimi instruction
533 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000534 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000535 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000536 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000537 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000538 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000539 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
540 }
541 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
542 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000543 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000544 std::swap(Op0, Op1);
545 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +0000546 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000547 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000548 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
549 }
550 }
551 }
552
Nate Begeman7ddecb42005-04-06 23:51:40 +0000553 // Verify that the Target mask and Insert mask together form a full word mask
554 // and that the Insert mask is a run of set bits (which implies both are runs
555 // of set bits). Given that, Select the arguments and generate the rlwimi
556 // instruction.
557 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +0000558 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000559 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000560 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000561 // Check for rotlwi / rotrwi here, a special case of bitfield insert
562 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000563 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +0000564 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000565 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
566 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
567 .addImm(0).addImm(31);
568 return true;
569 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000570 if (Op0Opc == ISD::AND && fullMask)
571 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000572 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000573 Tmp1 = SelectExpr(Op0);
574 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000575 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
576 .addImm(Amount).addImm(MB).addImm(ME);
577 return true;
578 }
579 return false;
580}
581
Nate Begeman3664cef2005-04-13 22:14:14 +0000582/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
583/// low six bits. If the shift amount is an ISD::AND node with a mask that is
584/// wider than the implicit mask, then we can get rid of the AND and let the
585/// shift do the mask.
586unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Jim Laskey191cf942005-08-11 21:59:23 +0000587 unsigned C;
588 if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63)
Nate Begeman3664cef2005-04-13 22:14:14 +0000589 return SelectExpr(N.getOperand(0));
590 else
591 return SelectExpr(N);
592}
593
Nate Begemanc24d4842005-08-10 20:52:09 +0000594unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000595 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000596 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000597 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +0000598 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000599
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000600 // Allocate a condition register for this expression
601 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000602
Nate Begemanc24d4842005-08-10 20:52:09 +0000603 // Use U to determine whether the SETCC immediate range is signed or not.
604 bool U = ISD::isUnsignedIntSetCC(CC);
605 if (isIntImmediate(RHS, Tmp2) &&
606 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
607 Tmp2 = Lo16(Tmp2);
608 // For comparisons against zero, we can implicity set CR0 if a recording
609 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
610 // operand zero of the SetCC node is available.
611 if (Tmp2 == 0 &&
612 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
613 RecordSuccess = false;
614 Tmp1 = SelectExpr(LHS, true);
615 if (RecordSuccess) {
616 ++Recorded;
617 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
618 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000619 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000620 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000621 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000622 // If we could not implicitly set CR0, then emit a compare immediate
623 // instead.
624 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
625 if (U)
626 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
627 else
628 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000629 } else {
Nate Begemanc24d4842005-08-10 20:52:09 +0000630 bool IsInteger = MVT::isInteger(LHS.getValueType());
631 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
632 Tmp1 = SelectExpr(LHS);
633 Tmp2 = SelectExpr(RHS);
634 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000635 }
636 return Result;
637}
638
Nate Begemand3ded2d2005-08-08 22:22:56 +0000639/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000640unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000641{
Nate Begeman96fc6812005-03-31 02:05:53 +0000642 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000643 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000644 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +0000645 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +0000646 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000647 if (isFrame) {
648 ++FrameOff;
649 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
650 return 1;
651 } else {
652 Reg = SelectExpr(N.getOperand(0));
653 return 0;
654 }
655 } else {
656 Reg = SelectExpr(N.getOperand(0));
657 offset = SelectExpr(N.getOperand(1));
658 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000659 }
Nate Begeman04730362005-04-01 04:45:11 +0000660 }
Nate Begemand3ded2d2005-08-08 22:22:56 +0000661 // Now check if we're dealing with a global, and whether or not we should emit
662 // an optimized load or store for statics.
663 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
664 GlobalValue *GV = GN->getGlobal();
665 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000666 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +0000667 if (PICEnabled)
668 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
669 .addGlobalAddress(GV);
670 else
671 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
672 Reg = GlobalHi;
673 offset = 0;
674 return 3;
675 }
676 }
Nate Begemana9795f82005-03-24 04:41:43 +0000677 Reg = SelectExpr(N);
678 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000679 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000680}
681
682void ISel::SelectBranchCC(SDOperand N)
683{
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000684 MachineBasicBlock *Dest =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000685 cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000686
Nate Begemana9795f82005-03-24 04:41:43 +0000687 Select(N.getOperand(0)); //chain
Nate Begeman7cbd5252005-08-16 19:49:35 +0000688 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get();
689 unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC);
Nate Begemanc24d4842005-08-10 20:52:09 +0000690 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000691
Nate Begemancd08e4c2005-04-09 20:09:12 +0000692 // If this is a two way branch, then grab the fallthrough basic block argument
693 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
694 // if necessary by the branch selection pass. Otherwise, emit a standard
695 // conditional branch.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000696 if (N.getOpcode() == ISD::BRTWOWAY_CC) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000697 MachineBasicBlock *Fallthrough =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000698 cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock();
Chris Lattnerf913d3f2005-08-21 19:03:28 +0000699 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
700 .addMBB(Dest).addMBB(Fallthrough);
701 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000702 } else {
Chris Lattnerf913d3f2005-08-21 19:03:28 +0000703 // Iterate to the next basic block
704 ilist<MachineBasicBlock>::iterator It = BB;
705 ++It;
706
Nate Begeman439009c2005-06-15 18:22:43 +0000707 // If the fallthrough path is off the end of the function, which would be
708 // undefined behavior, set it to be the same as the current block because
709 // we have nothing better to set it to, and leaving it alone will cause the
710 // PowerPC Branch Selection pass to crash.
711 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000712 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +0000713 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000714 }
Nate Begemana9795f82005-03-24 04:41:43 +0000715 return;
716}
717
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000718// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +0000719bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000720 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000721 bool IsArithmetic, bool Negate) {
722 // check constant
723 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
724 // exit if not a constant
725 if (!CN) return false;
726 // extract immediate
Chris Lattner6d9aed42005-08-17 01:25:14 +0000727 unsigned C = (unsigned)CN->getValue();
Chris Lattnerb4138c42005-08-10 18:11:33 +0000728 // negate if required (ISD::SUB)
729 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000730 // get the hi and lo portions of constant
731 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
732 unsigned Lo = Lo16(C);
733 // assume no intermediate result from lo instruction (same as final result)
734 unsigned Tmp = Result;
735 // check if two instructions are needed
736 if (Hi && Lo) {
737 // exit if usage indicates it would be better to load immediate into a
738 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +0000739 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000740 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +0000741 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000742 }
743 // get first operand
744 unsigned Opr0 = SelectExpr(N.getOperand(0));
745 // is a lo instruction needed
746 if (Lo) {
Chris Lattner6d9aed42005-08-17 01:25:14 +0000747 // generate instruction for lo portion
748 BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000749 // need to switch out first operand for hi instruction
750 Opr0 = Tmp;
751 }
Chris Lattner6d9aed42005-08-17 01:25:14 +0000752 // is a hi instruction needed
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000753 if (Hi) {
754 // generate instruction for hi portion
Chris Lattner6d9aed42005-08-17 01:25:14 +0000755 BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000756 }
757 return true;
758}
759
Nate Begemanc7bd4822005-04-11 06:34:10 +0000760unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +0000761 unsigned Result;
762 unsigned Tmp1, Tmp2, Tmp3;
763 unsigned Opc = 0;
764 unsigned opcode = N.getOpcode();
765
766 SDNode *Node = N.Val;
767 MVT::ValueType DestType = N.getValueType();
768
Chris Lattnera8cd0152005-08-16 21:58:15 +0000769 if (Node->getOpcode() == ISD::CopyFromReg) {
770 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana43b1762005-06-14 03:55:23 +0000771 // Just use the specified register as our input.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000772 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1)
773 return Reg;
774 }
Nate Begemana43b1762005-06-14 03:55:23 +0000775
Nate Begemana9795f82005-03-24 04:41:43 +0000776 unsigned &Reg = ExprMap[N];
777 if (Reg) return Reg;
778
Nate Begeman27eeb002005-04-02 05:59:34 +0000779 switch (N.getOpcode()) {
780 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000781 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000782 MakeReg(N.getValueType()) : 1;
783 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000784 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +0000785 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000786 // If this is a call instruction, make sure to prepare ALL of the result
787 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000788 if (Node->getNumValues() == 1)
789 Reg = Result = 1; // Void call, just a chain.
790 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000791 Result = MakeReg(Node->getValueType(0));
792 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000793 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000794 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000795 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000796 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000797 break;
798 case ISD::ADD_PARTS:
799 case ISD::SUB_PARTS:
800 case ISD::SHL_PARTS:
801 case ISD::SRL_PARTS:
802 case ISD::SRA_PARTS:
803 Result = MakeReg(Node->getValueType(0));
804 ExprMap[N.getValue(0)] = Result;
805 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
806 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
807 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000808 }
809
Nate Begemana9795f82005-03-24 04:41:43 +0000810 switch (opcode) {
811 default:
Nate Begeman5a014812005-08-14 01:17:16 +0000812 Node->dump(); std::cerr << '\n';
813 assert(0 && "Node not handled!\n");
Chris Lattner0bbea952005-08-26 20:25:03 +0000814 case PPCISD::FSEL:
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000815 Tmp1 = SelectExpr(N.getOperand(0));
816 Tmp2 = SelectExpr(N.getOperand(1));
817 Tmp3 = SelectExpr(N.getOperand(2));
818 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
819 return Result;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000820 case ISD::UNDEF:
Chris Lattner2b544002005-08-24 23:08:16 +0000821 if (Node->getValueType(0) == MVT::i32)
822 BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Result);
823 else
824 BuildMI(BB, PPC::IMPLICIT_DEF_FP, 0, Result);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000825 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000826 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000827 // Generate both result values. FIXME: Need a better commment here?
828 if (Result != 1)
829 ExprMap[N.getValue(1)] = 1;
830 else
831 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
832
833 // FIXME: We are currently ignoring the requested alignment for handling
834 // greater than the stack alignment. This will need to be revisited at some
835 // point. Align = N.getOperand(2);
836 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
837 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
838 std::cerr << "Cannot allocate stack object with greater alignment than"
839 << " the stack alignment yet!";
840 abort();
841 }
842 Select(N.getOperand(0));
843 Tmp1 = SelectExpr(N.getOperand(1));
844 // Subtract size from stack pointer, thereby allocating some space.
845 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
846 // Put a pointer to the space into the result register by copying the SP
847 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
848 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000849
850 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000851 Tmp1 = BB->getParent()->getConstantPool()->
852 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner54abfc52005-08-11 17:15:31 +0000853 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000854 if (PICEnabled)
855 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
856 .addConstantPoolIndex(Tmp1);
857 else
858 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000859 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
860 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000861
862 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000863 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000864 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000865 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000866
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000867 case ISD::GlobalAddress: {
868 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +0000869 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000870 if (PICEnabled)
871 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
872 .addGlobalAddress(GV);
873 else
Chris Lattner4015ea82005-07-28 04:42:11 +0000874 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000875 if (GV->hasWeakLinkage() || GV->isExternal()) {
876 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
877 } else {
878 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
879 }
880 return Result;
881 }
882
Nate Begeman5e966612005-03-24 06:28:42 +0000883 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000884 case ISD::EXTLOAD:
885 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000886 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000887 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000888 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +0000889 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000890
Nate Begeman5e966612005-03-24 06:28:42 +0000891 // Make sure we generate both values.
892 if (Result != 1)
893 ExprMap[N.getValue(1)] = 1; // Generate the token
894 else
895 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
896
897 SDOperand Chain = N.getOperand(0);
898 SDOperand Address = N.getOperand(1);
899 Select(Chain);
900
Nate Begeman9db505c2005-03-28 19:36:43 +0000901 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000902 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000903 case MVT::i1: Opc = PPC::LBZ; break;
904 case MVT::i8: Opc = PPC::LBZ; break;
905 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
906 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000907 case MVT::f32: Opc = PPC::LFS; break;
908 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000909 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000910
Nate Begeman74d73452005-03-31 00:15:26 +0000911 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000912 Tmp1 = MakeIntReg();
Chris Lattner5839bf22005-08-26 17:15:30 +0000913 unsigned CPI = BB->getParent()->getConstantPool()->
914 getConstantPoolIndex(CP->get());
Nate Begeman2497e632005-07-21 20:44:43 +0000915 if (PICEnabled)
916 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
917 .addConstantPoolIndex(CPI);
918 else
919 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +0000920 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +0000921 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000922 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
923 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000924 } else {
925 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000926 switch(SelectAddr(Address, Tmp1, offset)) {
927 default: assert(0 && "Unhandled return value from SelectAddr");
928 case 0: // imm offset, no frame, no index
929 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
930 break;
931 case 1: // imm offset + frame index
932 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
933 break;
934 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +0000935 Opc = IndexedOpForOp(Opc);
936 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000937 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +0000938 case 3: {
939 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
940 GlobalValue *GV = GN->getGlobal();
941 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
942 }
Nate Begeman04730362005-04-01 04:45:11 +0000943 }
Nate Begeman5e966612005-03-24 06:28:42 +0000944 }
945 return Result;
946 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000947
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000948 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000949 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000950 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000951 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000952 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
953 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
954 };
955 static const unsigned FPR[] = {
956 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
957 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
958 };
959
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000960 // Lower the chain for this call.
961 Select(N.getOperand(0));
962 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000963
Nate Begemand860aa62005-04-04 22:17:48 +0000964 MachineInstr *CallMI;
965 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000966 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +0000967 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000968 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +0000969 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000970 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +0000971 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000972 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +0000973 true);
974 } else {
975 Tmp1 = SelectExpr(N.getOperand(1));
Chris Lattner86fac6b2005-08-24 22:21:47 +0000976 BuildMI(BB, PPC::MTCTR, 1).addReg(Tmp1);
Nate Begemand860aa62005-04-04 22:17:48 +0000977 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
Nate Begemand860aa62005-04-04 22:17:48 +0000978 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
979 .addReg(PPC::R12);
980 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000981
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000982 // Load the register args to virtual regs
983 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000984 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000985 ArgVR.push_back(SelectExpr(N.getOperand(i)));
986
987 // Copy the virtual registers into the appropriate argument register
988 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
989 switch(N.getOperand(i+2).getValueType()) {
990 default: Node->dump(); assert(0 && "Unknown value type for call");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000991 case MVT::i32:
992 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +0000993 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000994 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +0000995 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
996 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000997 ++GPR_idx;
998 break;
999 case MVT::f64:
1000 case MVT::f32:
1001 assert(FPR_idx < 13 && "Too many fp args");
1002 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001003 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001004 ++FPR_idx;
1005 break;
1006 }
1007 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001008
Nate Begemand860aa62005-04-04 22:17:48 +00001009 // Put the call instruction in the correct place in the MachineBasicBlock
1010 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001011
1012 switch (Node->getValueType(0)) {
1013 default: assert(0 && "Unknown value type for call result!");
1014 case MVT::Other: return 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001015 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001016 if (Node->getValueType(1) == MVT::i32) {
1017 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1018 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1019 } else {
1020 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1021 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001022 break;
1023 case MVT::f32:
1024 case MVT::f64:
1025 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1026 break;
1027 }
1028 return Result+N.ResNo;
1029 }
Nate Begemana9795f82005-03-24 04:41:43 +00001030
1031 case ISD::SIGN_EXTEND:
1032 case ISD::SIGN_EXTEND_INREG:
1033 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001034 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001035 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001036 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001037 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001038 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001039 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001040 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001041 break;
1042 }
Nate Begemana9795f82005-03-24 04:41:43 +00001043 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001044
Nate Begemana9795f82005-03-24 04:41:43 +00001045 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001046 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001047 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001048 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Chris Lattnera8cd0152005-08-16 21:58:15 +00001049 Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001050 if (MVT::isInteger(DestType))
1051 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1052 else
1053 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001054 return Result;
1055
1056 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001057 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001058 unsigned SH, MB, ME;
1059 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1060 isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) {
1061 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1062 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1063 .addImm(MB).addImm(ME);
1064 return Result;
1065 }
1066 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001067 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001068 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001069 .addImm(31-Tmp2);
1070 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001071 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001072 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001073 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1074 }
1075 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001076
Nate Begeman5e966612005-03-24 06:28:42 +00001077 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001078 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001079 unsigned SH, MB, ME;
1080 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1081 isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) {
1082 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1083 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1084 .addImm(MB).addImm(ME);
1085 return Result;
1086 }
1087 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001088 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001089 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001090 .addImm(Tmp2).addImm(31);
1091 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001092 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001093 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001094 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1095 }
1096 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001097
Nate Begeman5e966612005-03-24 06:28:42 +00001098 case ISD::SRA:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001099 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001100 unsigned SH, MB, ME;
1101 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1102 isRotateAndMask(ISD::SRA, Tmp2, Tmp3, true, SH, MB, ME)) {
1103 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1104 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1105 .addImm(MB).addImm(ME);
1106 return Result;
1107 }
1108 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001109 Tmp2 &= 0x1F;
Nate Begeman5e966612005-03-24 06:28:42 +00001110 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1111 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001112 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001113 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001114 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1115 }
1116 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001117
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001118 case ISD::CTLZ:
1119 Tmp1 = SelectExpr(N.getOperand(0));
1120 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1121 return Result;
1122
Nate Begemana9795f82005-03-24 04:41:43 +00001123 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001124 if (!MVT::isInteger(DestType)) {
1125 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1126 N.getOperand(0).Val->hasOneUse()) {
1127 ++FusedFP; // Statistic
1128 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1129 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1130 Tmp3 = SelectExpr(N.getOperand(1));
1131 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1132 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1133 return Result;
1134 }
1135 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1136 N.getOperand(1).Val->hasOneUse()) {
1137 ++FusedFP; // Statistic
1138 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1139 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1140 Tmp3 = SelectExpr(N.getOperand(0));
1141 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1142 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1143 return Result;
1144 }
1145 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1146 Tmp1 = SelectExpr(N.getOperand(0));
1147 Tmp2 = SelectExpr(N.getOperand(1));
1148 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1149 return Result;
1150 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001151 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
1152 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001153 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001154 Tmp2 = SelectExpr(N.getOperand(1));
1155 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001156 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001157
Nate Begemana9795f82005-03-24 04:41:43 +00001158 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001159 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001160 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1161 unsigned SH, MB, ME;
1162 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1163 unsigned OprOpc;
1164 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1165 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001166 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001167 } else {
1168 Tmp1 = SelectExpr(N.getOperand(0));
1169 isRunOfOnes(Tmp2, MB, ME);
1170 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001171 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001172 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1173 .addImm(MB).addImm(ME);
1174 RecordSuccess = true;
1175 return Result;
1176 } else if (isUInt16(Tmp2)) {
1177 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001178 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001179 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001180 RecordSuccess = true;
1181 return Result;
1182 } else if (isUInt16(Tmp2)) {
1183 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001184 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001185 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001186 RecordSuccess = true;
1187 return Result;
1188 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001189 }
Jim Laskey847c3a92005-08-12 23:38:02 +00001190 if (isOprNot(N.getOperand(1))) {
1191 Tmp1 = SelectExpr(N.getOperand(0));
1192 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1193 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1194 RecordSuccess = false;
1195 return Result;
1196 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001197 if (isOprNot(N.getOperand(0))) {
Jim Laskey847c3a92005-08-12 23:38:02 +00001198 Tmp1 = SelectExpr(N.getOperand(1));
1199 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1200 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001201 RecordSuccess = false;
1202 return Result;
1203 }
1204 // emit a regular and
1205 Tmp1 = SelectExpr(N.getOperand(0));
1206 Tmp2 = SelectExpr(N.getOperand(1));
1207 Opc = Recording ? PPC::ANDo : PPC::AND;
1208 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001209 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001210 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001211
Nate Begemana9795f82005-03-24 04:41:43 +00001212 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001213 if (SelectBitfieldInsert(N, Result))
1214 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001215 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1216 return Result;
Jim Laskey847c3a92005-08-12 23:38:02 +00001217 if (isOprNot(N.getOperand(1))) {
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1220 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1221 RecordSuccess = false;
1222 return Result;
1223 }
1224 if (isOprNot(N.getOperand(0))) {
1225 Tmp1 = SelectExpr(N.getOperand(1));
1226 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1227 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1228 RecordSuccess = false;
1229 return Result;
1230 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001231 // emit regular or
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = SelectExpr(N.getOperand(1));
1234 Opc = Recording ? PPC::ORo : PPC::OR;
1235 RecordSuccess = true;
1236 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001237 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001238
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001239 case ISD::XOR: {
1240 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001241 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001242 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1243 Tmp2 = SelectExpr(N.getOperand(1));
1244 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1245 return Result;
1246 }
Chris Lattner837a5212005-04-21 21:09:11 +00001247 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001248 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001249 switch(N.getOperand(0).getOpcode()) {
1250 case ISD::OR:
1251 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1252 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1253 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1254 break;
1255 case ISD::AND:
1256 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1257 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1258 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1259 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001260 case ISD::XOR:
1261 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1262 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1263 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1264 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001265 default:
1266 Tmp1 = SelectExpr(N.getOperand(0));
1267 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1268 break;
1269 }
1270 return Result;
1271 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001272 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1273 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001274 // emit regular xor
1275 Tmp1 = SelectExpr(N.getOperand(0));
1276 Tmp2 = SelectExpr(N.getOperand(1));
1277 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001278 return Result;
1279 }
1280
Chris Lattner5b909172005-08-08 21:30:29 +00001281 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001282 if (!MVT::isInteger(DestType)) {
1283 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1284 N.getOperand(0).Val->hasOneUse()) {
1285 ++FusedFP; // Statistic
1286 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1288 Tmp3 = SelectExpr(N.getOperand(1));
1289 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1290 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1291 return Result;
1292 }
1293 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1294 N.getOperand(1).Val->hasOneUse()) {
1295 ++FusedFP; // Statistic
1296 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1297 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1298 Tmp3 = SelectExpr(N.getOperand(0));
1299 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1300 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1301 return Result;
1302 }
1303 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1304 Tmp1 = SelectExpr(N.getOperand(0));
1305 Tmp2 = SelectExpr(N.getOperand(1));
1306 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1307 return Result;
1308 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001309 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001310 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001311 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman4b46fc02005-08-24 04:59:21 +00001312 if (0 == Tmp1)
1313 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp2);
1314 else
1315 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001316 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001317 }
1318 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001319 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001320 Tmp1 = SelectExpr(N.getOperand(0));
1321 Tmp2 = SelectExpr(N.getOperand(1));
1322 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001323 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001324
Nate Begeman5e966612005-03-24 06:28:42 +00001325 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001326 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001327 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001328 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001329 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001330 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001331 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001332 switch (DestType) {
1333 default: assert(0 && "Unknown type to ISD::MUL"); break;
1334 case MVT::i32: Opc = PPC::MULLW; break;
1335 case MVT::f32: Opc = PPC::FMULS; break;
1336 case MVT::f64: Opc = PPC::FMUL; break;
1337 }
1338 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001339 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001340 return Result;
1341
Nate Begeman815d6da2005-04-06 00:25:27 +00001342 case ISD::MULHS:
1343 case ISD::MULHU:
1344 Tmp1 = SelectExpr(N.getOperand(0));
1345 Tmp2 = SelectExpr(N.getOperand(1));
1346 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1347 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1348 return Result;
1349
Nate Begemanf3d08f32005-03-29 00:03:27 +00001350 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001351 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001352 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1353 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001354 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001355 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001356 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1357 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001358 return Result;
1359 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1360 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001361 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001362 Tmp1 = MakeIntReg();
1363 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001364 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1365 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1366 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1367 return Result;
Chris Lattnerc70b4af2005-08-25 22:03:50 +00001368 } else if (Tmp3) {
1369 ExprMap.erase(N);
1370 return SelectExpr(BuildSDIVSequence(N));
Nate Begeman9f833d32005-04-12 00:10:02 +00001371 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001372 }
1373 // fall thru
1374 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001375 // If this is a divide by constant, we can emit code using some magic
1376 // constants to implement it as a multiply instead.
Chris Lattner801d5f52005-08-25 23:19:58 +00001377 if (isIntImmediate(N.getOperand(1), Tmp3) && Tmp3) {
Chris Lattnerc70b4af2005-08-25 22:03:50 +00001378 ExprMap.erase(N);
1379 return SelectExpr(BuildUDIVSequence(N));
Jeff Cohen00b168892005-07-27 06:12:32 +00001380 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001381 Tmp1 = SelectExpr(N.getOperand(0));
1382 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001383 switch (DestType) {
Chris Lattnerc70b4af2005-08-25 22:03:50 +00001384 default: assert(0 && "Unknown type to ISD::DIV"); break;
Nate Begemana3fd4002005-07-19 16:51:05 +00001385 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1386 case MVT::f32: Opc = PPC::FDIVS; break;
1387 case MVT::f64: Opc = PPC::FDIV; break;
1388 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001389 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1390 return Result;
1391
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001392 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001393 case ISD::SUB_PARTS: {
1394 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1395 "Not an i64 add/sub!");
Nate Begeman456f1e82005-08-17 00:20:08 +00001396 unsigned Tmp4 = 0;
Nate Begeman456f1e82005-08-17 00:20:08 +00001397 Tmp1 = SelectExpr(N.getOperand(0));
1398 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman456f1e82005-08-17 00:20:08 +00001399
Nate Begemanca12a2b2005-03-28 22:28:37 +00001400 if (N.getOpcode() == ISD::ADD_PARTS) {
Chris Lattner95e06822005-08-26 16:38:51 +00001401 bool ME = false, ZE = false;
Chris Lattner801d5f52005-08-25 23:19:58 +00001402 if (isIntImmediate(N.getOperand(3), Tmp3)) {
1403 ME = (signed)Tmp3 == -1;
1404 ZE = Tmp3 == 0;
1405 }
1406
1407 if (!ZE && !ME)
1408 Tmp4 = SelectExpr(N.getOperand(3));
1409
1410 if (isIntImmediate(N.getOperand(2), Tmp3) &&
1411 ((signed)Tmp3 >= -32768 || (signed)Tmp3 < 32768)) {
1412 // Codegen the low 32 bits of the add. Interestingly, there is no
1413 // shifted form of add immediate carrying.
Nate Begeman456f1e82005-08-17 00:20:08 +00001414 BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3);
Chris Lattner801d5f52005-08-25 23:19:58 +00001415 } else {
1416 Tmp3 = SelectExpr(N.getOperand(2));
Nate Begeman456f1e82005-08-17 00:20:08 +00001417 BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3);
Chris Lattner801d5f52005-08-25 23:19:58 +00001418 }
1419
Nate Begeman456f1e82005-08-17 00:20:08 +00001420 // Codegen the high 32 bits, adding zero, minus one, or the full value
1421 // along with the carry flag produced by addc/addic to tmp2.
Chris Lattner801d5f52005-08-25 23:19:58 +00001422 if (ZE) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001423 BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2);
Chris Lattner801d5f52005-08-25 23:19:58 +00001424 } else if (ME) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001425 BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2);
Chris Lattner801d5f52005-08-25 23:19:58 +00001426 } else {
Nate Begeman456f1e82005-08-17 00:20:08 +00001427 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4);
Chris Lattner801d5f52005-08-25 23:19:58 +00001428 }
Nate Begemanca12a2b2005-03-28 22:28:37 +00001429 } else {
Chris Lattner801d5f52005-08-25 23:19:58 +00001430 Tmp3 = SelectExpr(N.getOperand(2));
1431 Tmp4 = SelectExpr(N.getOperand(3));
Nate Begeman456f1e82005-08-17 00:20:08 +00001432 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1);
1433 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2);
Nate Begeman27eeb002005-04-02 05:59:34 +00001434 }
1435 return Result+N.ResNo;
1436 }
1437
1438 case ISD::SHL_PARTS:
1439 case ISD::SRA_PARTS:
1440 case ISD::SRL_PARTS: {
1441 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1442 "Not an i64 shift!");
1443 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1444 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001445 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
Chris Lattner54abfc52005-08-11 17:15:31 +00001446 Tmp1 = MakeIntReg();
1447 Tmp2 = MakeIntReg();
1448 Tmp3 = MakeIntReg();
1449 unsigned Tmp4 = MakeIntReg();
1450 unsigned Tmp5 = MakeIntReg();
1451 unsigned Tmp6 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001452 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1453 if (ISD::SHL_PARTS == opcode) {
1454 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1455 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1456 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1457 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001458 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001459 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1460 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1461 } else if (ISD::SRL_PARTS == opcode) {
1462 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1463 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1464 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1465 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1466 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1467 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1468 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1469 } else {
1470 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1471 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1472 MachineBasicBlock *OldMBB = BB;
1473 MachineFunction *F = BB->getParent();
1474 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1475 F->getBasicBlockList().insert(It, TmpMBB);
1476 F->getBasicBlockList().insert(It, PhiMBB);
1477 BB->addSuccessor(TmpMBB);
1478 BB->addSuccessor(PhiMBB);
1479 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1480 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1481 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1482 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1483 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1484 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1485 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1486 // Select correct least significant half if the shift amount > 32
1487 BB = TmpMBB;
Chris Lattner54abfc52005-08-11 17:15:31 +00001488 unsigned Tmp7 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001489 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1490 TmpMBB->addSuccessor(PhiMBB);
1491 BB = PhiMBB;
1492 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1493 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001494 }
1495 return Result+N.ResNo;
1496 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001497
Nate Begeman6b559972005-04-01 02:59:27 +00001498 case ISD::FP_TO_SINT: {
Nate Begeman6b559972005-04-01 02:59:27 +00001499 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman5a014812005-08-14 01:17:16 +00001500 Tmp2 = MakeFPReg();
1501 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1502 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1503 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1504 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1505 return Result;
Nate Begeman6b559972005-04-01 02:59:27 +00001506 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001507
Chris Lattner88ac32c2005-08-09 20:21:10 +00001508 case ISD::SETCC: {
1509 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1510 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
1511 // We can codegen setcc op, imm very efficiently compared to a brcond.
1512 // Check for those cases here.
1513 // setcc op, 0
1514 if (Tmp3 == 0) {
1515 Tmp1 = SelectExpr(Node->getOperand(0));
1516 switch (CC) {
Chris Lattneree84f112005-08-25 17:49:31 +00001517 default: Node->dump(); assert(0 && "Unhandled SetCC condition");abort();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001518 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001519 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001520 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1521 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1522 .addImm(5).addImm(31);
1523 break;
1524 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001525 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001526 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1527 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1528 break;
1529 case ISD::SETLT:
1530 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1531 .addImm(31).addImm(31);
1532 break;
1533 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001534 Tmp2 = MakeIntReg();
1535 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001536 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1537 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1538 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1539 .addImm(31).addImm(31);
1540 break;
Nate Begeman9765c252005-04-12 21:22:28 +00001541 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001542 return Result;
1543 } else if (Tmp3 == ~0U) { // setcc op, -1
1544 Tmp1 = SelectExpr(Node->getOperand(0));
1545 switch (CC) {
1546 default: assert(0 && "Unhandled SetCC condition"); abort();
1547 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001548 Tmp2 = MakeIntReg();
1549 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001550 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
1551 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
1552 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
1553 break;
1554 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001555 Tmp2 = MakeIntReg();
1556 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001557 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1558 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
1559 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
1560 break;
1561 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001562 Tmp2 = MakeIntReg();
1563 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001564 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
1565 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1566 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1567 .addImm(31).addImm(31);
1568 break;
1569 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001570 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001571 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1572 .addImm(31).addImm(31);
1573 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1574 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001575 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001576 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001577 }
Nate Begeman33162522005-03-29 21:54:38 +00001578 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001579
Nate Begemanc24d4842005-08-10 20:52:09 +00001580 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1581 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001582 return Result;
1583 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001584
1585 case ISD::SELECT_CC: {
1586 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
Nate Begemana3fd4002005-07-19 16:51:05 +00001587
Nate Begeman4b46fc02005-08-24 04:59:21 +00001588 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1589 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
1590 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N.getOperand(2));
1591 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N.getOperand(3));
1592 if (N1C && N2C && N3C && N1C->isNullValue() && N3C->isNullValue() &&
Nate Begeman6ef49492005-08-24 05:06:48 +00001593 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Nate Begeman4b46fc02005-08-24 04:59:21 +00001594 Tmp1 = SelectExpr(Node->getOperand(0));
1595 Tmp2 = MakeIntReg();
Nate Begeman6ef49492005-08-24 05:06:48 +00001596 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1597 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman4b46fc02005-08-24 04:59:21 +00001598 return Result;
1599 }
1600
Nate Begeman5a014812005-08-14 01:17:16 +00001601 // If the False value only has one use, we can generate better code by
1602 // selecting it in the fallthrough basic block rather than here, which
1603 // increases register pressure.
Nate Begeman5a014812005-08-14 01:17:16 +00001604 unsigned TrueValue = SelectExpr(N.getOperand(2));
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001605 unsigned FalseValue;
1606
1607 // If the false value is simple enough, evaluate it inline in the false
1608 // block.
Chris Lattnerb30ee6a2005-08-22 00:47:28 +00001609 if (N.getOperand(3).Val->hasOneUse() &&
1610 (isa<ConstantSDNode>(N.getOperand(3)) ||
Chris Lattnerb30ee6a2005-08-22 00:47:28 +00001611 isa<GlobalAddressSDNode>(N.getOperand(3))))
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001612 FalseValue = 0;
1613 else
1614 FalseValue = SelectExpr(N.getOperand(3));
Nate Begemanc24d4842005-08-10 20:52:09 +00001615 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1616 Opc = getBCCForSetCC(CC);
1617
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001618 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00001619 // value and the MBB to hold the PHI instruction for this SetCC.
1620 MachineBasicBlock *thisMBB = BB;
1621 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1622 ilist<MachineBasicBlock>::iterator It = BB;
1623 ++It;
1624
1625 // thisMBB:
1626 // ...
1627 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001628 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00001629 // bCC copy1MBB
1630 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001631 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1632 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001633 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001634 MachineFunction *F = BB->getParent();
1635 F->getBasicBlockList().insert(It, copy0MBB);
1636 F->getBasicBlockList().insert(It, sinkMBB);
1637 // Update machine-CFG edges
1638 BB->addSuccessor(copy0MBB);
1639 BB->addSuccessor(sinkMBB);
1640
1641 // copy0MBB:
1642 // %FalseValue = ...
1643 // # fallthrough to sinkMBB
1644 BB = copy0MBB;
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001645
1646 // If the false value is simple enough, evaluate it here, to avoid it being
1647 // evaluated on the true edge.
1648 if (FalseValue == 0)
1649 FalseValue = SelectExpr(N.getOperand(3));
1650
Nate Begeman74747862005-03-29 22:24:51 +00001651 // Update machine-CFG edges
1652 BB->addSuccessor(sinkMBB);
1653
1654 // sinkMBB:
1655 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1656 // ...
1657 BB = sinkMBB;
1658 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1659 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001660 return Result;
1661 }
Nate Begemana9795f82005-03-24 04:41:43 +00001662
Chris Lattner0c09a412005-08-18 17:16:52 +00001663 case ISD::Constant: {
1664 assert(N.getValueType() == MVT::i32 &&
1665 "Only i32 constants are legal on this target!");
Nate Begeman58dfb082005-08-18 18:14:49 +00001666 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001667 if (isInt16(v)) {
1668 BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo16(v));
Chris Lattner0c09a412005-08-18 17:16:52 +00001669 } else {
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001670 unsigned Hi = Hi16(v);
1671 unsigned Lo = Lo16(v);
1672 if (Lo) {
1673 Tmp1 = MakeIntReg();
1674 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi);
1675 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo);
1676 } else {
1677 BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi);
1678 }
Nate Begemana9795f82005-03-24 04:41:43 +00001679 }
1680 return Result;
Chris Lattner0c09a412005-08-18 17:16:52 +00001681 }
Nate Begemana3fd4002005-07-19 16:51:05 +00001682
Nate Begemana3fd4002005-07-19 16:51:05 +00001683 case ISD::FNEG:
1684 if (!NoExcessFPPrecision &&
1685 ISD::ADD == N.getOperand(0).getOpcode() &&
1686 N.getOperand(0).Val->hasOneUse() &&
1687 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1688 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
1689 ++FusedFP; // Statistic
1690 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1691 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1692 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1693 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1694 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1695 } else if (!NoExcessFPPrecision &&
1696 ISD::ADD == N.getOperand(0).getOpcode() &&
1697 N.getOperand(0).Val->hasOneUse() &&
1698 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1699 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
1700 ++FusedFP; // Statistic
1701 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1702 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1703 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1704 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1705 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1706 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
1707 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1708 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1709 } else {
1710 Tmp1 = SelectExpr(N.getOperand(0));
1711 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1712 }
1713 return Result;
1714
1715 case ISD::FABS:
1716 Tmp1 = SelectExpr(N.getOperand(0));
1717 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1718 return Result;
1719
Nate Begemanadeb43d2005-07-20 22:42:00 +00001720 case ISD::FSQRT:
1721 Tmp1 = SelectExpr(N.getOperand(0));
1722 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
1723 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1724 return Result;
1725
Nate Begemana3fd4002005-07-19 16:51:05 +00001726 case ISD::FP_ROUND:
1727 assert (DestType == MVT::f32 &&
1728 N.getOperand(0).getValueType() == MVT::f64 &&
1729 "only f64 to f32 conversion supported here");
1730 Tmp1 = SelectExpr(N.getOperand(0));
1731 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1732 return Result;
1733
1734 case ISD::FP_EXTEND:
1735 assert (DestType == MVT::f64 &&
1736 N.getOperand(0).getValueType() == MVT::f32 &&
1737 "only f32 to f64 conversion supported here");
1738 Tmp1 = SelectExpr(N.getOperand(0));
1739 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1740 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00001741 }
Nate Begemana9795f82005-03-24 04:41:43 +00001742 return 0;
1743}
1744
1745void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00001746 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00001747 unsigned opcode = N.getOpcode();
1748
1749 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1750 return; // Already selected.
1751
1752 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001753
Nate Begemana9795f82005-03-24 04:41:43 +00001754 switch (Node->getOpcode()) {
1755 default:
1756 Node->dump(); std::cerr << "\n";
1757 assert(0 && "Node not handled yet!");
1758 case ISD::EntryToken: return; // Noop
1759 case ISD::TokenFactor:
1760 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1761 Select(Node->getOperand(i));
1762 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001763 case ISD::CALLSEQ_START:
1764 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00001765 Select(N.getOperand(0));
1766 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001767 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00001768 PPC::ADJCALLSTACKUP;
1769 BuildMI(BB, Opc, 1).addImm(Tmp1);
1770 return;
1771 case ISD::BR: {
1772 MachineBasicBlock *Dest =
1773 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001774 Select(N.getOperand(0));
1775 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1776 return;
1777 }
Nate Begeman7cbd5252005-08-16 19:49:35 +00001778 case ISD::BR_CC:
1779 case ISD::BRTWOWAY_CC:
Nate Begemana9795f82005-03-24 04:41:43 +00001780 SelectBranchCC(N);
1781 return;
1782 case ISD::CopyToReg:
1783 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001784 Tmp1 = SelectExpr(N.getOperand(2));
1785 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001786
Nate Begemana9795f82005-03-24 04:41:43 +00001787 if (Tmp1 != Tmp2) {
Chris Lattnera8cd0152005-08-16 21:58:15 +00001788 if (N.getOperand(2).getValueType() == MVT::f64 ||
1789 N.getOperand(2).getValueType() == MVT::f32)
Nate Begemana9795f82005-03-24 04:41:43 +00001790 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1791 else
1792 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1793 }
1794 return;
1795 case ISD::ImplicitDef:
1796 Select(N.getOperand(0));
Chris Lattner2b544002005-08-24 23:08:16 +00001797 Tmp1 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1798 if (N.getOperand(1).getValueType() == MVT::i32)
1799 BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Tmp1);
1800 else
1801 BuildMI(BB, PPC::IMPLICIT_DEF_FP, 0, Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001802 return;
1803 case ISD::RET:
1804 switch (N.getNumOperands()) {
1805 default:
1806 assert(0 && "Unknown return instruction!");
1807 case 3:
1808 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1809 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001810 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00001811 Select(N.getOperand(0));
1812 Tmp1 = SelectExpr(N.getOperand(1));
1813 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001814 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1815 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001816 break;
1817 case 2:
1818 Select(N.getOperand(0));
1819 Tmp1 = SelectExpr(N.getOperand(1));
1820 switch (N.getOperand(1).getValueType()) {
1821 default:
1822 assert(0 && "Unknown return type!");
1823 case MVT::f64:
1824 case MVT::f32:
1825 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1826 break;
1827 case MVT::i32:
1828 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1829 break;
1830 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001831 case 1:
1832 Select(N.getOperand(0));
1833 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001834 }
1835 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1836 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001837 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00001838 case ISD::STORE: {
1839 SDOperand Chain = N.getOperand(0);
1840 SDOperand Value = N.getOperand(1);
1841 SDOperand Address = N.getOperand(2);
1842 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00001843
Nate Begeman2497e632005-07-21 20:44:43 +00001844 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00001845
Nate Begeman2497e632005-07-21 20:44:43 +00001846 if (opcode == ISD::STORE) {
1847 switch(Value.getValueType()) {
1848 default: assert(0 && "unknown Type in store");
1849 case MVT::i32: Opc = PPC::STW; break;
1850 case MVT::f64: Opc = PPC::STFD; break;
1851 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001852 }
Nate Begeman2497e632005-07-21 20:44:43 +00001853 } else { //ISD::TRUNCSTORE
1854 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1855 default: assert(0 && "unknown Type in store");
1856 case MVT::i1:
1857 case MVT::i8: Opc = PPC::STB; break;
1858 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001859 }
Nate Begemana9795f82005-03-24 04:41:43 +00001860 }
Nate Begeman2497e632005-07-21 20:44:43 +00001861
1862 if(Address.getOpcode() == ISD::FrameIndex) {
1863 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1864 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00001865 } else {
1866 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001867 switch(SelectAddr(Address, Tmp2, offset)) {
1868 default: assert(0 && "Unhandled return value from SelectAddr");
1869 case 0: // imm offset, no frame, no index
1870 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
1871 break;
1872 case 1: // imm offset + frame index
1873 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
1874 break;
1875 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00001876 Opc = IndexedOpForOp(Opc);
1877 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001878 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001879 case 3: {
1880 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1881 GlobalValue *GV = GN->getGlobal();
1882 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
1883 }
Nate Begeman2497e632005-07-21 20:44:43 +00001884 }
1885 }
1886 return;
1887 }
Nate Begemana9795f82005-03-24 04:41:43 +00001888 case ISD::EXTLOAD:
1889 case ISD::SEXTLOAD:
1890 case ISD::ZEXTLOAD:
1891 case ISD::LOAD:
1892 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001893 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00001894 case ISD::CALL:
1895 case ISD::DYNAMIC_STACKALLOC:
1896 ExprMap.erase(N);
1897 SelectExpr(N);
1898 return;
1899 }
1900 assert(0 && "Should not be reached!");
1901}
1902
1903
1904/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1905/// into a machine code representation using pattern matching and a machine
1906/// description file.
1907///
1908FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001909 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001910}
1911