blob: 8c4b2b6a9a84abefbf111dd61d5ce49608bc062d [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"
Nate Begemana9795f82005-03-24 04:41:43 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000029#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/ADT/Statistic.h"
33#include <set>
34#include <algorithm>
35using namespace llvm;
36
Nate Begemana9795f82005-03-24 04:41:43 +000037namespace {
Chris Lattner6d9aed42005-08-17 01:25:14 +000038Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
39Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
40Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Chris Lattner3c304a32005-08-05 22:05:03 +000041
Nate Begemana9795f82005-03-24 04:41:43 +000042//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000043// ISel - PPC32 specific code to select PPC32 machine instructions for
44// SelectionDAG operations.
Nate Begemana9795f82005-03-24 04:41:43 +000045//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000046
Nate Begemana9795f82005-03-24 04:41:43 +000047class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +000048 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +000049 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
50 // for sdiv and udiv until it is put into the future
51 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000052
Nate Begemana9795f82005-03-24 04:41:43 +000053 /// ExprMap - As shared expressions are codegen'd, we keep track of which
54 /// vreg the value is produced in, so we only emit one copy of each compiled
55 /// tree.
56 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +000057
58 unsigned GlobalBaseReg;
59 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +000060 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +000061public:
Nate Begeman815d6da2005-04-06 00:25:27 +000062 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
63 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000064
Nate Begemanc7b09f12005-03-25 08:34:25 +000065 /// runOnFunction - Override this function in order to reset our per-function
66 /// variables.
67 virtual bool runOnFunction(Function &Fn) {
68 // Make sure we re-emit a set of the global base reg if necessary
69 GlobalBaseInitialized = false;
70 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000071 }
72
Nate Begemana9795f82005-03-24 04:41:43 +000073 /// InstructionSelectBasicBlock - This callback is invoked by
74 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
75 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
76 DEBUG(BB->dump());
77 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +000078 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +000079 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +000080
Nate Begemana9795f82005-03-24 04:41:43 +000081 // Clear state used for selection.
82 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +000083 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +000084 }
Nate Begeman815d6da2005-04-06 00:25:27 +000085
Chris Lattner54abfc52005-08-11 17:15:31 +000086 // convenience functions for virtual register creation
87 inline unsigned MakeIntReg() {
88 return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
89 }
90 inline unsigned MakeFPReg() {
91 return RegMap->createVirtualRegister(PPC32::FPRCRegisterClass);
92 }
93
Nate Begeman815d6da2005-04-06 00:25:27 +000094 // dag -> dag expanders for integer divide by constant
95 SDOperand BuildSDIVSequence(SDOperand N);
96 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000097
Nate Begemandffcfcc2005-04-01 00:32:34 +000098 unsigned getGlobalBaseReg();
Nate Begemanc24d4842005-08-10 20:52:09 +000099 void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000100 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000101 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begemanc24d4842005-08-10 20:52:09 +0000102 unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
Chris Lattnerb4138c42005-08-10 18:11:33 +0000103 bool SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000104 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000105 bool IsArithmetic = false, bool Negate = false);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000106 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000107 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000108
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000109 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000110 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000111
112 virtual const char *getPassName() const {
113 return "PowerPC Pattern Instruction Selection";
114 }
Nate Begemana9795f82005-03-24 04:41:43 +0000115};
116
Chris Lattner02efa6c2005-08-08 21:08:09 +0000117// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
118// any number of 0s on either side. The 1s are allowed to wrap from LSB to
119// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
120// not, since all 1s are not contiguous.
121static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
122 if (isShiftedMask_32(Val)) {
123 // look for the first non-zero bit
124 MB = CountLeadingZeros_32(Val);
125 // look for the first zero bit after the run of ones
126 ME = CountLeadingZeros_32((Val - 1) ^ Val);
127 return true;
128 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
129 // effectively look for the first zero bit
130 ME = CountLeadingZeros_32(Val) - 1;
131 // effectively look for the first one bit after the run of zeros
132 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
133 return true;
134 }
135 // no run present
136 return false;
137}
138
Chris Lattnercf1cf182005-08-08 21:10:27 +0000139// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
140// and mask opcode and mask operation.
141static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
142 bool IsShiftMask,
143 unsigned &SH, unsigned &MB, unsigned &ME) {
144 if (Shift > 31) return false;
145 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
146
147 if (Opcode == ISD::SHL) { // shift left
148 // apply shift to mask if it comes first
149 if (IsShiftMask) Mask = Mask << Shift;
150 // determine which bits are made indeterminant by shift
151 Indeterminant = ~(0xFFFFFFFFu << Shift);
152 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
153 // apply shift to mask if it comes first
154 if (IsShiftMask) Mask = Mask >> Shift;
155 // determine which bits are made indeterminant by shift
156 Indeterminant = ~(0xFFFFFFFFu >> Shift);
157 // adjust for the left rotate
158 Shift = 32 - Shift;
159 }
160
161 // if the mask doesn't intersect any Indeterminant bits
Jim Laskeycf083e32005-08-12 23:52:46 +0000162 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000163 SH = Shift;
164 // make sure the mask is still a mask (wrap arounds may not be)
165 return isRunOfOnes(Mask, MB, ME);
166 }
167
168 // can't do it
169 return false;
170}
171
Chris Lattner59b21c22005-08-09 18:29:55 +0000172// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000173// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000174static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000175 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000176 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000177 // retrieve value
Jim Laskeyb454cfd2005-08-18 00:15:15 +0000178 Imm = (unsigned)CN->getValue();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000179 // passes muster
180 return true;
181 }
182 // not a constant
183 return false;
184}
185
Jim Laskey191cf942005-08-11 21:59:23 +0000186// isOpcWithIntImmediate - This method tests to see if the node is a specific
187// opcode and that it has a immediate integer right operand.
188// If so Imm will receive the 32 bit value.
189static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) {
190 return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm);
191}
192
Chris Lattnercf1cf182005-08-08 21:10:27 +0000193// isOprShiftImm - Returns true if the specified operand is a shift opcode with
194// a immediate shift count less than 32.
195static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
196 Opc = N.getOpcode();
197 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000198 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000199}
200
201// isOprNot - Returns true if the specified operand is an xor with immediate -1.
202static bool isOprNot(SDOperand N) {
203 unsigned Imm;
Jim Laskey191cf942005-08-11 21:59:23 +0000204 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000205}
206
207// Immediate constant composers.
208// Lo16 - grabs the lo 16 bits from a 32 bit constant.
209// Hi16 - grabs the hi 16 bits from a 32 bit constant.
210// HA16 - computes the hi bits required if the lo bits are add/subtracted in
211// arithmethically.
212static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
213static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
214static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
215
Nate Begemanc7bd4822005-04-11 06:34:10 +0000216/// NodeHasRecordingVariant - If SelectExpr can always produce code for
217/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
218/// return false.
219static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
220 switch(NodeOpcode) {
221 default: return false;
222 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000223 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000224 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000225 }
226}
227
Nate Begeman3e897162005-03-31 23:55:40 +0000228/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
Nate Begemanc24d4842005-08-10 20:52:09 +0000229/// to Condition.
230static unsigned getBCCForSetCC(ISD::CondCode CC) {
231 switch (CC) {
Nate Begeman3e897162005-03-31 23:55:40 +0000232 default: assert(0 && "Unknown condition!"); abort();
233 case ISD::SETEQ: return PPC::BEQ;
234 case ISD::SETNE: return PPC::BNE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000235 case ISD::SETULT:
Nate Begeman3e897162005-03-31 23:55:40 +0000236 case ISD::SETLT: return PPC::BLT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000237 case ISD::SETULE:
Nate Begeman3e897162005-03-31 23:55:40 +0000238 case ISD::SETLE: return PPC::BLE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000239 case ISD::SETUGT:
Nate Begeman3e897162005-03-31 23:55:40 +0000240 case ISD::SETGT: return PPC::BGT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000241 case ISD::SETUGE:
Nate Begeman3e897162005-03-31 23:55:40 +0000242 case ISD::SETGE: return PPC::BGE;
243 }
Nate Begeman04730362005-04-01 04:45:11 +0000244 return 0;
245}
246
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000247/// getCRIdxForSetCC - Return the index of the condition register field
248/// associated with the SetCC condition, and whether or not the field is
249/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000250static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
251 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000252 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000253 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000254 case ISD::SETLT: Inv = false; return 0;
255 case ISD::SETUGE:
256 case ISD::SETGE: Inv = true; return 0;
257 case ISD::SETUGT:
258 case ISD::SETGT: Inv = false; return 1;
259 case ISD::SETULE:
260 case ISD::SETLE: Inv = true; return 1;
261 case ISD::SETEQ: Inv = false; return 2;
262 case ISD::SETNE: Inv = true; return 2;
263 }
264 return 0;
265}
266
Nate Begeman04730362005-04-01 04:45:11 +0000267/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
268/// and store immediate instructions.
269static unsigned IndexedOpForOp(unsigned Opcode) {
270 switch(Opcode) {
271 default: assert(0 && "Unknown opcode!"); abort();
272 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
273 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
274 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
275 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
276 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
277 case PPC::LFD: return PPC::LFDX;
278 }
279 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000280}
Nate Begeman815d6da2005-04-06 00:25:27 +0000281
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000282// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000283// a multiply.
284struct ms {
285 int m; // magic number
286 int s; // shift amount
287};
288
289struct mu {
290 unsigned int m; // magic number
291 int a; // add indicator
292 int s; // shift amount
293};
294
295/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000296/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000297/// or -1.
298static struct ms magic(int d) {
299 int p;
300 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000301 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000302 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000303
Nate Begeman815d6da2005-04-06 00:25:27 +0000304 ad = abs(d);
305 t = two31 + ((unsigned int)d >> 31);
306 anc = t - 1 - t%ad; // absolute value of nc
307 p = 31; // initialize p
308 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
309 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
310 q2 = two31/ad; // initialize q2 = 2p/abs(d)
311 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
312 do {
313 p = p + 1;
314 q1 = 2*q1; // update q1 = 2p/abs(nc)
315 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
316 if (r1 >= anc) { // must be unsigned comparison
317 q1 = q1 + 1;
318 r1 = r1 - anc;
319 }
320 q2 = 2*q2; // update q2 = 2p/abs(d)
321 r2 = 2*r2; // update r2 = rem(2p/abs(d))
322 if (r2 >= ad) { // must be unsigned comparison
323 q2 = q2 + 1;
324 r2 = r2 - ad;
325 }
326 delta = ad - r2;
327 } while (q1 < delta || (q1 == delta && r1 == 0));
328
329 mag.m = q2 + 1;
330 if (d < 0) mag.m = -mag.m; // resulting magic number
331 mag.s = p - 32; // resulting shift
332 return mag;
333}
334
335/// magicu - calculate the magic numbers required to codegen an integer udiv as
336/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
337static struct mu magicu(unsigned d)
338{
339 int p;
340 unsigned int nc, delta, q1, r1, q2, r2;
341 struct mu magu;
342 magu.a = 0; // initialize "add" indicator
343 nc = - 1 - (-d)%d;
344 p = 31; // initialize p
345 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
346 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
347 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
348 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
349 do {
350 p = p + 1;
351 if (r1 >= nc - r1 ) {
352 q1 = 2*q1 + 1; // update q1
353 r1 = 2*r1 - nc; // update r1
354 }
355 else {
356 q1 = 2*q1; // update q1
357 r1 = 2*r1; // update r1
358 }
359 if (r2 + 1 >= d - r2) {
360 if (q2 >= 0x7FFFFFFF) magu.a = 1;
361 q2 = 2*q2 + 1; // update q2
362 r2 = 2*r2 + 1 - d; // update r2
363 }
364 else {
365 if (q2 >= 0x80000000) magu.a = 1;
366 q2 = 2*q2; // update q2
367 r2 = 2*r2 + 1; // update r2
368 }
369 delta = d - 1 - r2;
370 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
371 magu.m = q2 + 1; // resulting magic number
372 magu.s = p - 32; // resulting shift
373 return magu;
374}
375}
376
377/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
378/// return a DAG expression to select that will generate the same value by
379/// multiplying by a magic number. See:
380/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
381SDOperand ISel::BuildSDIVSequence(SDOperand N) {
382 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
383 ms magics = magic(d);
384 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000385 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000386 ISelDAG->getConstant(magics.m, MVT::i32));
387 // If d > 0 and m < 0, add the numerator
388 if (d > 0 && magics.m < 0)
389 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
390 // If d < 0 and m > 0, subtract the numerator.
391 if (d < 0 && magics.m > 0)
392 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
393 // Shift right algebraic if shift value is nonzero
394 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000395 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000396 ISelDAG->getConstant(magics.s, MVT::i32));
397 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000398 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000399 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000400 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000401}
402
403/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
404/// return a DAG expression to select that will generate the same value by
405/// multiplying by a magic number. See:
406/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
407SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000408 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000409 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
410 mu magics = magicu(d);
411 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000412 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000413 ISelDAG->getConstant(magics.m, MVT::i32));
414 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000415 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000416 ISelDAG->getConstant(magics.s, MVT::i32));
417 } else {
418 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000419 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000420 ISelDAG->getConstant(1, MVT::i32));
421 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000422 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000423 ISelDAG->getConstant(magics.s-1, MVT::i32));
424 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000425 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000426}
427
Nate Begemanc7b09f12005-03-25 08:34:25 +0000428/// getGlobalBaseReg - Output the instructions required to put the
429/// base address to use for accessing globals into a register.
430///
431unsigned ISel::getGlobalBaseReg() {
432 if (!GlobalBaseInitialized) {
433 // Insert the set of GlobalBaseReg into the first MBB of the function
434 MachineBasicBlock &FirstMBB = BB->getParent()->front();
435 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000436 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000437 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
Chris Lattner3f852b42005-08-18 23:24:50 +0000438 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000439 GlobalBaseInitialized = true;
440 }
441 return GlobalBaseReg;
442}
443
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000444/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000445/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000446void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
447 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000448 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000449 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000450 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000451 bool GPOpt =
452 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
Nate Begeman27d53ba2005-08-19 03:42:28 +0000453 if (GPOpt)
454 BuildMI(BB, PPC::MFOCRF, 1, IntCR).addReg(PPC::CR7);
455 else
456 BuildMI(BB, PPC::MFCR, 0, IntCR);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000457 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000458 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000459 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
460 .addImm(31).addImm(31);
461 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
462 } else {
463 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
464 .addImm(31).addImm(31);
465 }
466}
467
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000468/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000469/// the rotate left word immediate then mask insert (rlwimi) instruction.
470/// Returns true on success, false if the caller still needs to select OR.
471///
472/// Patterns matched:
473/// 1. or shl, and 5. or and, and
474/// 2. or and, shl 6. or shl, shr
475/// 3. or shr, and 7. or shr, shl
476/// 4. or and, shr
477bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000478 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000479 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000480 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000481
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000482 SDOperand Op0 = OR.getOperand(0);
483 SDOperand Op1 = OR.getOperand(1);
484
485 unsigned Op0Opc = Op0.getOpcode();
486 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000487
Nate Begeman7ddecb42005-04-06 23:51:40 +0000488 // Verify that we have the correct opcodes
489 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
490 return false;
491 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
492 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000493
Nate Begeman7ddecb42005-04-06 23:51:40 +0000494 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +0000495 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000496 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000497 case ISD::SHL: TgtMask <<= Value; break;
498 case ISD::SRL: TgtMask >>= Value; break;
499 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000500 }
501 } else {
502 return false;
503 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000504
Nate Begeman7ddecb42005-04-06 23:51:40 +0000505 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +0000506 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000507 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000508 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000509 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000510 InsMask <<= Amount;
511 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000512 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000513 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000514 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000515 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000516 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000517 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000518 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000519 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000520 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000521 break;
522 }
523 } else {
524 return false;
525 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000526
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000527 unsigned Tmp3 = 0;
528
529 // If both of the inputs are ANDs and one of them has a logical shift by
530 // constant as its input, make that the inserted value so that we can combine
531 // the shift into the rotate part of the rlwimi instruction
532 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000533 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000534 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000535 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000536 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000537 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000538 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
539 }
540 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
541 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000542 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000543 std::swap(Op0, Op1);
544 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +0000545 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000546 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000547 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
548 }
549 }
550 }
551
Nate Begeman7ddecb42005-04-06 23:51:40 +0000552 // Verify that the Target mask and Insert mask together form a full word mask
553 // and that the Insert mask is a run of set bits (which implies both are runs
554 // of set bits). Given that, Select the arguments and generate the rlwimi
555 // instruction.
556 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +0000557 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000558 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000559 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000560 // Check for rotlwi / rotrwi here, a special case of bitfield insert
561 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000562 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +0000563 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000564 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
565 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
566 .addImm(0).addImm(31);
567 return true;
568 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000569 if (Op0Opc == ISD::AND && fullMask)
570 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000571 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000572 Tmp1 = SelectExpr(Op0);
573 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000574 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
575 .addImm(Amount).addImm(MB).addImm(ME);
576 return true;
577 }
578 return false;
579}
580
Nate Begeman3664cef2005-04-13 22:14:14 +0000581/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
582/// low six bits. If the shift amount is an ISD::AND node with a mask that is
583/// wider than the implicit mask, then we can get rid of the AND and let the
584/// shift do the mask.
585unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Jim Laskey191cf942005-08-11 21:59:23 +0000586 unsigned C;
587 if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63)
Nate Begeman3664cef2005-04-13 22:14:14 +0000588 return SelectExpr(N.getOperand(0));
589 else
590 return SelectExpr(N);
591}
592
Nate Begemanc24d4842005-08-10 20:52:09 +0000593unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000594 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000595 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000596 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +0000597 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000598
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000599 // Allocate a condition register for this expression
600 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000601
Nate Begemanc24d4842005-08-10 20:52:09 +0000602 // Use U to determine whether the SETCC immediate range is signed or not.
603 bool U = ISD::isUnsignedIntSetCC(CC);
604 if (isIntImmediate(RHS, Tmp2) &&
605 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
606 Tmp2 = Lo16(Tmp2);
607 // For comparisons against zero, we can implicity set CR0 if a recording
608 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
609 // operand zero of the SetCC node is available.
610 if (Tmp2 == 0 &&
611 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
612 RecordSuccess = false;
613 Tmp1 = SelectExpr(LHS, true);
614 if (RecordSuccess) {
615 ++Recorded;
616 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
617 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000618 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000619 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000620 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000621 // If we could not implicitly set CR0, then emit a compare immediate
622 // instead.
623 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
624 if (U)
625 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
626 else
627 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000628 } else {
Nate Begemanc24d4842005-08-10 20:52:09 +0000629 bool IsInteger = MVT::isInteger(LHS.getValueType());
630 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
631 Tmp1 = SelectExpr(LHS);
632 Tmp2 = SelectExpr(RHS);
633 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000634 }
635 return Result;
636}
637
Nate Begemand3ded2d2005-08-08 22:22:56 +0000638/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000639unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000640{
Nate Begeman96fc6812005-03-31 02:05:53 +0000641 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000642 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000643 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +0000644 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +0000645 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000646 if (isFrame) {
647 ++FrameOff;
648 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
649 return 1;
650 } else {
651 Reg = SelectExpr(N.getOperand(0));
652 return 0;
653 }
654 } else {
655 Reg = SelectExpr(N.getOperand(0));
656 offset = SelectExpr(N.getOperand(1));
657 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000658 }
Nate Begeman04730362005-04-01 04:45:11 +0000659 }
Nate Begemand3ded2d2005-08-08 22:22:56 +0000660 // Now check if we're dealing with a global, and whether or not we should emit
661 // an optimized load or store for statics.
662 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
663 GlobalValue *GV = GN->getGlobal();
664 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000665 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +0000666 if (PICEnabled)
667 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
668 .addGlobalAddress(GV);
669 else
670 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
671 Reg = GlobalHi;
672 offset = 0;
673 return 3;
674 }
675 }
Nate Begemana9795f82005-03-24 04:41:43 +0000676 Reg = SelectExpr(N);
677 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000678 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000679}
680
681void ISel::SelectBranchCC(SDOperand N)
682{
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000683 MachineBasicBlock *Dest =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000684 cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000685
Nate Begemana9795f82005-03-24 04:41:43 +0000686 Select(N.getOperand(0)); //chain
Nate Begeman7cbd5252005-08-16 19:49:35 +0000687 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get();
688 unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC);
Nate Begemanc24d4842005-08-10 20:52:09 +0000689 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000690
Nate Begemancd08e4c2005-04-09 20:09:12 +0000691 // If this is a two way branch, then grab the fallthrough basic block argument
692 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
693 // if necessary by the branch selection pass. Otherwise, emit a standard
694 // conditional branch.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000695 if (N.getOpcode() == ISD::BRTWOWAY_CC) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000696 MachineBasicBlock *Fallthrough =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000697 cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock();
Chris Lattnerf913d3f2005-08-21 19:03:28 +0000698 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
699 .addMBB(Dest).addMBB(Fallthrough);
700 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000701 } else {
Chris Lattnerf913d3f2005-08-21 19:03:28 +0000702 // Iterate to the next basic block
703 ilist<MachineBasicBlock>::iterator It = BB;
704 ++It;
705
Nate Begeman439009c2005-06-15 18:22:43 +0000706 // If the fallthrough path is off the end of the function, which would be
707 // undefined behavior, set it to be the same as the current block because
708 // we have nothing better to set it to, and leaving it alone will cause the
709 // PowerPC Branch Selection pass to crash.
710 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000711 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +0000712 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000713 }
Nate Begemana9795f82005-03-24 04:41:43 +0000714 return;
715}
716
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000717// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +0000718bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000719 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000720 bool IsArithmetic, bool Negate) {
721 // check constant
722 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
723 // exit if not a constant
724 if (!CN) return false;
725 // extract immediate
Chris Lattner6d9aed42005-08-17 01:25:14 +0000726 unsigned C = (unsigned)CN->getValue();
Chris Lattnerb4138c42005-08-10 18:11:33 +0000727 // negate if required (ISD::SUB)
728 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000729 // get the hi and lo portions of constant
730 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
731 unsigned Lo = Lo16(C);
732 // assume no intermediate result from lo instruction (same as final result)
733 unsigned Tmp = Result;
734 // check if two instructions are needed
735 if (Hi && Lo) {
736 // exit if usage indicates it would be better to load immediate into a
737 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +0000738 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000739 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +0000740 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000741 }
742 // get first operand
743 unsigned Opr0 = SelectExpr(N.getOperand(0));
744 // is a lo instruction needed
745 if (Lo) {
Chris Lattner6d9aed42005-08-17 01:25:14 +0000746 // generate instruction for lo portion
747 BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000748 // need to switch out first operand for hi instruction
749 Opr0 = Tmp;
750 }
Chris Lattner6d9aed42005-08-17 01:25:14 +0000751 // is a hi instruction needed
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000752 if (Hi) {
753 // generate instruction for hi portion
Chris Lattner6d9aed42005-08-17 01:25:14 +0000754 BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000755 }
756 return true;
757}
758
Nate Begemanc7bd4822005-04-11 06:34:10 +0000759unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +0000760 unsigned Result;
761 unsigned Tmp1, Tmp2, Tmp3;
762 unsigned Opc = 0;
763 unsigned opcode = N.getOpcode();
764
765 SDNode *Node = N.Val;
766 MVT::ValueType DestType = N.getValueType();
767
Chris Lattnera8cd0152005-08-16 21:58:15 +0000768 if (Node->getOpcode() == ISD::CopyFromReg) {
769 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana43b1762005-06-14 03:55:23 +0000770 // Just use the specified register as our input.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000771 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1)
772 return Reg;
773 }
Nate Begemana43b1762005-06-14 03:55:23 +0000774
Nate Begemana9795f82005-03-24 04:41:43 +0000775 unsigned &Reg = ExprMap[N];
776 if (Reg) return Reg;
777
Nate Begeman27eeb002005-04-02 05:59:34 +0000778 switch (N.getOpcode()) {
779 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000780 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000781 MakeReg(N.getValueType()) : 1;
782 break;
Chris Lattner5dd7fea2005-08-31 17:48:04 +0000783 case ISD::AssertSext:
784 case ISD::AssertZext:
785 // Don't allocate a vreg for these nodes.
786 return Reg = SelectExpr(N.getOperand(0));
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000787 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +0000788 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000789 // If this is a call instruction, make sure to prepare ALL of the result
790 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000791 if (Node->getNumValues() == 1)
792 Reg = Result = 1; // Void call, just a chain.
793 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000794 Result = MakeReg(Node->getValueType(0));
795 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000796 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000797 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000798 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000799 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000800 break;
801 case ISD::ADD_PARTS:
802 case ISD::SUB_PARTS:
Nate Begeman27eeb002005-04-02 05:59:34 +0000803 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;
Chris Lattnerf7605322005-08-31 21:09:52 +0000820 case PPCISD::FCTIWZ:
821 Tmp1 = SelectExpr(N.getOperand(0));
822 BuildMI(BB, PPC::FCTIWZ, 1, Result).addReg(Tmp1);
823 return Result;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000824 case ISD::UNDEF:
Chris Lattner2b544002005-08-24 23:08:16 +0000825 if (Node->getValueType(0) == MVT::i32)
826 BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Result);
827 else
828 BuildMI(BB, PPC::IMPLICIT_DEF_FP, 0, Result);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000829 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000830 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000831 // Generate both result values. FIXME: Need a better commment here?
832 if (Result != 1)
833 ExprMap[N.getValue(1)] = 1;
834 else
835 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
836
837 // FIXME: We are currently ignoring the requested alignment for handling
838 // greater than the stack alignment. This will need to be revisited at some
839 // point. Align = N.getOperand(2);
840 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
841 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
842 std::cerr << "Cannot allocate stack object with greater alignment than"
843 << " the stack alignment yet!";
844 abort();
845 }
846 Select(N.getOperand(0));
847 Tmp1 = SelectExpr(N.getOperand(1));
848 // Subtract size from stack pointer, thereby allocating some space.
849 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
850 // Put a pointer to the space into the result register by copying the SP
851 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
852 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000853
854 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000855 Tmp1 = BB->getParent()->getConstantPool()->
856 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner54abfc52005-08-11 17:15:31 +0000857 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000858 if (PICEnabled)
859 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
860 .addConstantPoolIndex(Tmp1);
861 else
862 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000863 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
864 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000865
866 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000867 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000868 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000869 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000870
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000871 case ISD::GlobalAddress: {
872 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +0000873 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000874 if (PICEnabled)
875 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
876 .addGlobalAddress(GV);
877 else
Chris Lattner4015ea82005-07-28 04:42:11 +0000878 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000879 if (GV->hasWeakLinkage() || GV->isExternal()) {
880 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
881 } else {
882 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
883 }
884 return Result;
885 }
886
Nate Begeman5e966612005-03-24 06:28:42 +0000887 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000888 case ISD::EXTLOAD:
889 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000890 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000891 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000892 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +0000893 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000894
Nate Begeman5e966612005-03-24 06:28:42 +0000895 // Make sure we generate both values.
896 if (Result != 1)
897 ExprMap[N.getValue(1)] = 1; // Generate the token
898 else
899 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
900
901 SDOperand Chain = N.getOperand(0);
902 SDOperand Address = N.getOperand(1);
903 Select(Chain);
904
Nate Begeman9db505c2005-03-28 19:36:43 +0000905 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000906 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000907 case MVT::i1: Opc = PPC::LBZ; break;
908 case MVT::i8: Opc = PPC::LBZ; break;
909 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
910 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000911 case MVT::f32: Opc = PPC::LFS; break;
912 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000913 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000914
Nate Begeman74d73452005-03-31 00:15:26 +0000915 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000916 Tmp1 = MakeIntReg();
Chris Lattner5839bf22005-08-26 17:15:30 +0000917 unsigned CPI = BB->getParent()->getConstantPool()->
918 getConstantPoolIndex(CP->get());
Nate Begeman2497e632005-07-21 20:44:43 +0000919 if (PICEnabled)
920 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
921 .addConstantPoolIndex(CPI);
922 else
923 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +0000924 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +0000925 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000926 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
927 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000928 } else {
929 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000930 switch(SelectAddr(Address, Tmp1, offset)) {
931 default: assert(0 && "Unhandled return value from SelectAddr");
932 case 0: // imm offset, no frame, no index
933 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
934 break;
935 case 1: // imm offset + frame index
936 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
937 break;
938 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +0000939 Opc = IndexedOpForOp(Opc);
940 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000941 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +0000942 case 3: {
943 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
944 GlobalValue *GV = GN->getGlobal();
945 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
946 }
Nate Begeman04730362005-04-01 04:45:11 +0000947 }
Nate Begeman5e966612005-03-24 06:28:42 +0000948 }
949 return Result;
950 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000951
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000952 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000953 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000954 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000955 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000956 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
957 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
958 };
959 static const unsigned FPR[] = {
960 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
961 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
962 };
963
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000964 // Lower the chain for this call.
965 Select(N.getOperand(0));
966 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000967
Nate Begemand860aa62005-04-04 22:17:48 +0000968 MachineInstr *CallMI;
969 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000970 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +0000971 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000972 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +0000973 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000974 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +0000975 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000976 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +0000977 true);
978 } else {
979 Tmp1 = SelectExpr(N.getOperand(1));
Chris Lattner86fac6b2005-08-24 22:21:47 +0000980 BuildMI(BB, PPC::MTCTR, 1).addReg(Tmp1);
Nate Begemand860aa62005-04-04 22:17:48 +0000981 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
Nate Begemand860aa62005-04-04 22:17:48 +0000982 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
983 .addReg(PPC::R12);
984 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000985
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000986 // Load the register args to virtual regs
987 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000988 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000989 ArgVR.push_back(SelectExpr(N.getOperand(i)));
990
991 // Copy the virtual registers into the appropriate argument register
992 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
993 switch(N.getOperand(i+2).getValueType()) {
994 default: Node->dump(); assert(0 && "Unknown value type for call");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000995 case MVT::i32:
996 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +0000997 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000998 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +0000999 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1000 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001001 ++GPR_idx;
1002 break;
1003 case MVT::f64:
1004 case MVT::f32:
1005 assert(FPR_idx < 13 && "Too many fp args");
1006 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001007 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001008 ++FPR_idx;
1009 break;
1010 }
1011 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001012
Nate Begemand860aa62005-04-04 22:17:48 +00001013 // Put the call instruction in the correct place in the MachineBasicBlock
1014 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001015
1016 switch (Node->getValueType(0)) {
1017 default: assert(0 && "Unknown value type for call result!");
1018 case MVT::Other: return 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001019 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001020 if (Node->getValueType(1) == MVT::i32) {
1021 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1022 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1023 } else {
1024 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1025 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001026 break;
1027 case MVT::f32:
1028 case MVT::f64:
1029 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1030 break;
1031 }
1032 return Result+N.ResNo;
1033 }
Nate Begemana9795f82005-03-24 04:41:43 +00001034
1035 case ISD::SIGN_EXTEND:
1036 case ISD::SIGN_EXTEND_INREG:
1037 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001038 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001039 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001040 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001041 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001042 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001043 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001044 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001045 break;
1046 }
Nate Begemana9795f82005-03-24 04:41:43 +00001047 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001048
Nate Begemana9795f82005-03-24 04:41:43 +00001049 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001050 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001051 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001052 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Chris Lattnera8cd0152005-08-16 21:58:15 +00001053 Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001054 if (MVT::isInteger(DestType))
1055 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1056 else
1057 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001058 return Result;
1059
1060 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001061 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001062 unsigned SH, MB, ME;
1063 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1064 isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) {
1065 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1066 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1067 .addImm(MB).addImm(ME);
1068 return Result;
1069 }
1070 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001071 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001072 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001073 .addImm(31-Tmp2);
1074 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001075 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001076 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001077 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1078 }
1079 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001080
Nate Begeman5e966612005-03-24 06:28:42 +00001081 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001082 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001083 unsigned SH, MB, ME;
1084 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1085 isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) {
1086 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1087 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1088 .addImm(MB).addImm(ME);
1089 return Result;
1090 }
1091 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001092 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001093 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001094 .addImm(Tmp2).addImm(31);
1095 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001096 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001097 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001098 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1099 }
1100 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001101
Nate Begeman5e966612005-03-24 06:28:42 +00001102 case ISD::SRA:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001103 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001104 unsigned SH, MB, ME;
1105 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1106 isRotateAndMask(ISD::SRA, Tmp2, Tmp3, true, SH, MB, ME)) {
1107 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1108 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1109 .addImm(MB).addImm(ME);
1110 return Result;
1111 }
1112 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001113 Tmp2 &= 0x1F;
Nate Begeman5e966612005-03-24 06:28:42 +00001114 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1115 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001116 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001117 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001118 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1119 }
1120 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001121
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001122 case ISD::CTLZ:
1123 Tmp1 = SelectExpr(N.getOperand(0));
1124 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1125 return Result;
1126
Nate Begemana9795f82005-03-24 04:41:43 +00001127 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001128 if (!MVT::isInteger(DestType)) {
1129 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1130 N.getOperand(0).Val->hasOneUse()) {
1131 ++FusedFP; // Statistic
1132 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1133 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1134 Tmp3 = SelectExpr(N.getOperand(1));
1135 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1136 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1137 return Result;
1138 }
1139 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1140 N.getOperand(1).Val->hasOneUse()) {
1141 ++FusedFP; // Statistic
1142 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1143 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1144 Tmp3 = SelectExpr(N.getOperand(0));
1145 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1146 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1147 return Result;
1148 }
1149 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1150 Tmp1 = SelectExpr(N.getOperand(0));
1151 Tmp2 = SelectExpr(N.getOperand(1));
1152 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1153 return Result;
1154 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001155 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
1156 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001157 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001158 Tmp2 = SelectExpr(N.getOperand(1));
1159 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001160 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001161
Nate Begemana9795f82005-03-24 04:41:43 +00001162 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001163 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001164 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1165 unsigned SH, MB, ME;
1166 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1167 unsigned OprOpc;
1168 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1169 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001170 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001171 } else {
1172 Tmp1 = SelectExpr(N.getOperand(0));
1173 isRunOfOnes(Tmp2, MB, ME);
1174 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001175 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001176 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1177 .addImm(MB).addImm(ME);
1178 RecordSuccess = true;
1179 return Result;
1180 } else if (isUInt16(Tmp2)) {
1181 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001182 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001183 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001184 RecordSuccess = true;
1185 return Result;
1186 } else if (isUInt16(Tmp2)) {
1187 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001188 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001189 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001190 RecordSuccess = true;
1191 return Result;
1192 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001193 }
Jim Laskey847c3a92005-08-12 23:38:02 +00001194 if (isOprNot(N.getOperand(1))) {
1195 Tmp1 = SelectExpr(N.getOperand(0));
1196 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1197 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1198 RecordSuccess = false;
1199 return Result;
1200 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001201 if (isOprNot(N.getOperand(0))) {
Jim Laskey847c3a92005-08-12 23:38:02 +00001202 Tmp1 = SelectExpr(N.getOperand(1));
1203 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1204 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001205 RecordSuccess = false;
1206 return Result;
1207 }
1208 // emit a regular and
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 Tmp2 = SelectExpr(N.getOperand(1));
1211 Opc = Recording ? PPC::ANDo : PPC::AND;
1212 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001213 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001214 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001215
Nate Begemana9795f82005-03-24 04:41:43 +00001216 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001217 if (SelectBitfieldInsert(N, Result))
1218 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001219 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1220 return Result;
Jim Laskey847c3a92005-08-12 23:38:02 +00001221 if (isOprNot(N.getOperand(1))) {
1222 Tmp1 = SelectExpr(N.getOperand(0));
1223 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1224 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1225 RecordSuccess = false;
1226 return Result;
1227 }
1228 if (isOprNot(N.getOperand(0))) {
1229 Tmp1 = SelectExpr(N.getOperand(1));
1230 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1231 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1232 RecordSuccess = false;
1233 return Result;
1234 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001235 // emit regular or
1236 Tmp1 = SelectExpr(N.getOperand(0));
1237 Tmp2 = SelectExpr(N.getOperand(1));
1238 Opc = Recording ? PPC::ORo : PPC::OR;
1239 RecordSuccess = true;
1240 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001241 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001242
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001243 case ISD::XOR: {
1244 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001245 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001246 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1247 Tmp2 = SelectExpr(N.getOperand(1));
1248 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1249 return Result;
1250 }
Chris Lattner837a5212005-04-21 21:09:11 +00001251 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001252 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001253 switch(N.getOperand(0).getOpcode()) {
1254 case ISD::OR:
1255 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1256 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1257 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1258 break;
1259 case ISD::AND:
1260 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1261 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1262 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1263 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001264 case ISD::XOR:
1265 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1266 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1267 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1268 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001269 default:
1270 Tmp1 = SelectExpr(N.getOperand(0));
1271 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1272 break;
1273 }
1274 return Result;
1275 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001276 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1277 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001278 // emit regular xor
1279 Tmp1 = SelectExpr(N.getOperand(0));
1280 Tmp2 = SelectExpr(N.getOperand(1));
1281 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001282 return Result;
1283 }
1284
Chris Lattner5b909172005-08-08 21:30:29 +00001285 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001286 if (!MVT::isInteger(DestType)) {
1287 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1288 N.getOperand(0).Val->hasOneUse()) {
1289 ++FusedFP; // Statistic
1290 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1291 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1292 Tmp3 = SelectExpr(N.getOperand(1));
1293 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1294 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1295 return Result;
1296 }
1297 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1298 N.getOperand(1).Val->hasOneUse()) {
1299 ++FusedFP; // Statistic
1300 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1301 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1302 Tmp3 = SelectExpr(N.getOperand(0));
1303 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1304 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1305 return Result;
1306 }
1307 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1308 Tmp1 = SelectExpr(N.getOperand(0));
1309 Tmp2 = SelectExpr(N.getOperand(1));
1310 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1311 return Result;
1312 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001313 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001314 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001315 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman4b46fc02005-08-24 04:59:21 +00001316 if (0 == Tmp1)
1317 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp2);
1318 else
1319 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001320 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001321 }
1322 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001323 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001324 Tmp1 = SelectExpr(N.getOperand(0));
1325 Tmp2 = SelectExpr(N.getOperand(1));
1326 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001327 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001328
Nate Begeman5e966612005-03-24 06:28:42 +00001329 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001330 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001331 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001332 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001333 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001334 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001335 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001336 switch (DestType) {
1337 default: assert(0 && "Unknown type to ISD::MUL"); break;
1338 case MVT::i32: Opc = PPC::MULLW; break;
1339 case MVT::f32: Opc = PPC::FMULS; break;
1340 case MVT::f64: Opc = PPC::FMUL; break;
1341 }
1342 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001343 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001344 return Result;
1345
Nate Begeman815d6da2005-04-06 00:25:27 +00001346 case ISD::MULHS:
1347 case ISD::MULHU:
1348 Tmp1 = SelectExpr(N.getOperand(0));
1349 Tmp2 = SelectExpr(N.getOperand(1));
1350 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1351 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1352 return Result;
1353
Nate Begemanf3d08f32005-03-29 00:03:27 +00001354 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001355 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001356 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1357 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001358 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001359 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001360 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1361 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001362 return Result;
1363 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1364 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001365 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001366 Tmp1 = MakeIntReg();
1367 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001368 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1369 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1370 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1371 return Result;
Chris Lattnerc70b4af2005-08-25 22:03:50 +00001372 } else if (Tmp3) {
1373 ExprMap.erase(N);
1374 return SelectExpr(BuildSDIVSequence(N));
Nate Begeman9f833d32005-04-12 00:10:02 +00001375 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001376 }
1377 // fall thru
1378 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001379 // If this is a divide by constant, we can emit code using some magic
1380 // constants to implement it as a multiply instead.
Chris Lattner801d5f52005-08-25 23:19:58 +00001381 if (isIntImmediate(N.getOperand(1), Tmp3) && Tmp3) {
Chris Lattnerc70b4af2005-08-25 22:03:50 +00001382 ExprMap.erase(N);
1383 return SelectExpr(BuildUDIVSequence(N));
Jeff Cohen00b168892005-07-27 06:12:32 +00001384 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001385 Tmp1 = SelectExpr(N.getOperand(0));
1386 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001387 switch (DestType) {
Chris Lattnerc70b4af2005-08-25 22:03:50 +00001388 default: assert(0 && "Unknown type to ISD::DIV"); break;
Nate Begemana3fd4002005-07-19 16:51:05 +00001389 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1390 case MVT::f32: Opc = PPC::FDIVS; break;
1391 case MVT::f64: Opc = PPC::FDIV; break;
1392 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001393 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1394 return Result;
1395
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001396 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001397 case ISD::SUB_PARTS: {
1398 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1399 "Not an i64 add/sub!");
Nate Begeman456f1e82005-08-17 00:20:08 +00001400 unsigned Tmp4 = 0;
Nate Begeman456f1e82005-08-17 00:20:08 +00001401 Tmp1 = SelectExpr(N.getOperand(0));
1402 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman456f1e82005-08-17 00:20:08 +00001403
Nate Begemanca12a2b2005-03-28 22:28:37 +00001404 if (N.getOpcode() == ISD::ADD_PARTS) {
Chris Lattner95e06822005-08-26 16:38:51 +00001405 bool ME = false, ZE = false;
Chris Lattner801d5f52005-08-25 23:19:58 +00001406 if (isIntImmediate(N.getOperand(3), Tmp3)) {
1407 ME = (signed)Tmp3 == -1;
1408 ZE = Tmp3 == 0;
1409 }
1410
1411 if (!ZE && !ME)
1412 Tmp4 = SelectExpr(N.getOperand(3));
1413
1414 if (isIntImmediate(N.getOperand(2), Tmp3) &&
1415 ((signed)Tmp3 >= -32768 || (signed)Tmp3 < 32768)) {
1416 // Codegen the low 32 bits of the add. Interestingly, there is no
1417 // shifted form of add immediate carrying.
Nate Begeman456f1e82005-08-17 00:20:08 +00001418 BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3);
Chris Lattner801d5f52005-08-25 23:19:58 +00001419 } else {
1420 Tmp3 = SelectExpr(N.getOperand(2));
Nate Begeman456f1e82005-08-17 00:20:08 +00001421 BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3);
Chris Lattner801d5f52005-08-25 23:19:58 +00001422 }
1423
Nate Begeman456f1e82005-08-17 00:20:08 +00001424 // Codegen the high 32 bits, adding zero, minus one, or the full value
1425 // along with the carry flag produced by addc/addic to tmp2.
Chris Lattner801d5f52005-08-25 23:19:58 +00001426 if (ZE) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001427 BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2);
Chris Lattner801d5f52005-08-25 23:19:58 +00001428 } else if (ME) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001429 BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2);
Chris Lattner801d5f52005-08-25 23:19:58 +00001430 } else {
Nate Begeman456f1e82005-08-17 00:20:08 +00001431 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4);
Chris Lattner801d5f52005-08-25 23:19:58 +00001432 }
Nate Begemanca12a2b2005-03-28 22:28:37 +00001433 } else {
Chris Lattner801d5f52005-08-25 23:19:58 +00001434 Tmp3 = SelectExpr(N.getOperand(2));
1435 Tmp4 = SelectExpr(N.getOperand(3));
Nate Begeman456f1e82005-08-17 00:20:08 +00001436 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1);
1437 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2);
Nate Begeman27eeb002005-04-02 05:59:34 +00001438 }
1439 return Result+N.ResNo;
1440 }
1441
Chris Lattner88ac32c2005-08-09 20:21:10 +00001442 case ISD::SETCC: {
1443 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1444 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
1445 // We can codegen setcc op, imm very efficiently compared to a brcond.
1446 // Check for those cases here.
1447 // setcc op, 0
1448 if (Tmp3 == 0) {
1449 Tmp1 = SelectExpr(Node->getOperand(0));
1450 switch (CC) {
Chris Lattneree84f112005-08-25 17:49:31 +00001451 default: Node->dump(); assert(0 && "Unhandled SetCC condition");abort();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001452 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001453 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001454 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1455 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1456 .addImm(5).addImm(31);
1457 break;
1458 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001459 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001460 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1461 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1462 break;
1463 case ISD::SETLT:
1464 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1465 .addImm(31).addImm(31);
1466 break;
1467 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001468 Tmp2 = MakeIntReg();
1469 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001470 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1471 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1472 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1473 .addImm(31).addImm(31);
1474 break;
Nate Begeman9765c252005-04-12 21:22:28 +00001475 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001476 return Result;
1477 } else if (Tmp3 == ~0U) { // setcc op, -1
1478 Tmp1 = SelectExpr(Node->getOperand(0));
1479 switch (CC) {
1480 default: assert(0 && "Unhandled SetCC condition"); abort();
1481 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001482 Tmp2 = MakeIntReg();
1483 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001484 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
1485 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
1486 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
1487 break;
1488 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001489 Tmp2 = MakeIntReg();
1490 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001491 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1492 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
1493 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
1494 break;
1495 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001496 Tmp2 = MakeIntReg();
1497 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001498 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
1499 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1500 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1501 .addImm(31).addImm(31);
1502 break;
1503 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001504 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001505 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1506 .addImm(31).addImm(31);
1507 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1508 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001509 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001510 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001511 }
Nate Begeman33162522005-03-29 21:54:38 +00001512 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001513
Nate Begemanc24d4842005-08-10 20:52:09 +00001514 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1515 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001516 return Result;
1517 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001518
1519 case ISD::SELECT_CC: {
1520 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
Nate Begemana3fd4002005-07-19 16:51:05 +00001521
Nate Begeman4b46fc02005-08-24 04:59:21 +00001522 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1523 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
1524 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N.getOperand(2));
1525 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N.getOperand(3));
1526 if (N1C && N2C && N3C && N1C->isNullValue() && N3C->isNullValue() &&
Nate Begeman6ef49492005-08-24 05:06:48 +00001527 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Nate Begeman4b46fc02005-08-24 04:59:21 +00001528 Tmp1 = SelectExpr(Node->getOperand(0));
1529 Tmp2 = MakeIntReg();
Nate Begeman6ef49492005-08-24 05:06:48 +00001530 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1531 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman4b46fc02005-08-24 04:59:21 +00001532 return Result;
1533 }
1534
Nate Begeman5a014812005-08-14 01:17:16 +00001535 // If the False value only has one use, we can generate better code by
1536 // selecting it in the fallthrough basic block rather than here, which
1537 // increases register pressure.
Nate Begeman5a014812005-08-14 01:17:16 +00001538 unsigned TrueValue = SelectExpr(N.getOperand(2));
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001539 unsigned FalseValue;
1540
1541 // If the false value is simple enough, evaluate it inline in the false
1542 // block.
Chris Lattnerb30ee6a2005-08-22 00:47:28 +00001543 if (N.getOperand(3).Val->hasOneUse() &&
1544 (isa<ConstantSDNode>(N.getOperand(3)) ||
Chris Lattnerb30ee6a2005-08-22 00:47:28 +00001545 isa<GlobalAddressSDNode>(N.getOperand(3))))
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001546 FalseValue = 0;
1547 else
1548 FalseValue = SelectExpr(N.getOperand(3));
Nate Begemanc24d4842005-08-10 20:52:09 +00001549 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1550 Opc = getBCCForSetCC(CC);
1551
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001552 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00001553 // value and the MBB to hold the PHI instruction for this SetCC.
1554 MachineBasicBlock *thisMBB = BB;
1555 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1556 ilist<MachineBasicBlock>::iterator It = BB;
1557 ++It;
1558
1559 // thisMBB:
1560 // ...
1561 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001562 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00001563 // bCC copy1MBB
1564 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001565 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1566 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001567 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001568 MachineFunction *F = BB->getParent();
1569 F->getBasicBlockList().insert(It, copy0MBB);
1570 F->getBasicBlockList().insert(It, sinkMBB);
1571 // Update machine-CFG edges
1572 BB->addSuccessor(copy0MBB);
1573 BB->addSuccessor(sinkMBB);
1574
1575 // copy0MBB:
1576 // %FalseValue = ...
1577 // # fallthrough to sinkMBB
1578 BB = copy0MBB;
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001579
1580 // If the false value is simple enough, evaluate it here, to avoid it being
1581 // evaluated on the true edge.
1582 if (FalseValue == 0)
1583 FalseValue = SelectExpr(N.getOperand(3));
1584
Nate Begeman74747862005-03-29 22:24:51 +00001585 // Update machine-CFG edges
1586 BB->addSuccessor(sinkMBB);
1587
1588 // sinkMBB:
1589 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1590 // ...
1591 BB = sinkMBB;
1592 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1593 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001594 return Result;
1595 }
Nate Begemana9795f82005-03-24 04:41:43 +00001596
Chris Lattner0c09a412005-08-18 17:16:52 +00001597 case ISD::Constant: {
1598 assert(N.getValueType() == MVT::i32 &&
1599 "Only i32 constants are legal on this target!");
Nate Begeman58dfb082005-08-18 18:14:49 +00001600 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001601 if (isInt16(v)) {
1602 BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo16(v));
Chris Lattner0c09a412005-08-18 17:16:52 +00001603 } else {
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001604 unsigned Hi = Hi16(v);
1605 unsigned Lo = Lo16(v);
1606 if (Lo) {
1607 Tmp1 = MakeIntReg();
1608 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi);
1609 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo);
1610 } else {
1611 BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi);
1612 }
Nate Begemana9795f82005-03-24 04:41:43 +00001613 }
1614 return Result;
Chris Lattner0c09a412005-08-18 17:16:52 +00001615 }
Nate Begemana3fd4002005-07-19 16:51:05 +00001616
Nate Begemana3fd4002005-07-19 16:51:05 +00001617 case ISD::FNEG:
1618 if (!NoExcessFPPrecision &&
1619 ISD::ADD == N.getOperand(0).getOpcode() &&
1620 N.getOperand(0).Val->hasOneUse() &&
1621 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1622 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
1623 ++FusedFP; // Statistic
1624 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1625 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1626 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1627 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1628 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1629 } else if (!NoExcessFPPrecision &&
1630 ISD::ADD == N.getOperand(0).getOpcode() &&
1631 N.getOperand(0).Val->hasOneUse() &&
1632 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1633 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
1634 ++FusedFP; // Statistic
1635 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1636 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1637 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1638 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1639 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1640 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
1641 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1642 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1643 } else {
1644 Tmp1 = SelectExpr(N.getOperand(0));
1645 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1646 }
1647 return Result;
1648
1649 case ISD::FABS:
1650 Tmp1 = SelectExpr(N.getOperand(0));
1651 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1652 return Result;
1653
Nate Begemanadeb43d2005-07-20 22:42:00 +00001654 case ISD::FSQRT:
1655 Tmp1 = SelectExpr(N.getOperand(0));
1656 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
1657 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1658 return Result;
1659
Nate Begemana3fd4002005-07-19 16:51:05 +00001660 case ISD::FP_ROUND:
1661 assert (DestType == MVT::f32 &&
1662 N.getOperand(0).getValueType() == MVT::f64 &&
1663 "only f64 to f32 conversion supported here");
1664 Tmp1 = SelectExpr(N.getOperand(0));
1665 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1666 return Result;
1667
1668 case ISD::FP_EXTEND:
1669 assert (DestType == MVT::f64 &&
1670 N.getOperand(0).getValueType() == MVT::f32 &&
1671 "only f32 to f64 conversion supported here");
1672 Tmp1 = SelectExpr(N.getOperand(0));
1673 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1674 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00001675 }
Nate Begemana9795f82005-03-24 04:41:43 +00001676 return 0;
1677}
1678
1679void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00001680 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00001681 unsigned opcode = N.getOpcode();
1682
1683 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1684 return; // Already selected.
1685
1686 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001687
Nate Begemana9795f82005-03-24 04:41:43 +00001688 switch (Node->getOpcode()) {
1689 default:
1690 Node->dump(); std::cerr << "\n";
1691 assert(0 && "Node not handled yet!");
1692 case ISD::EntryToken: return; // Noop
1693 case ISD::TokenFactor:
1694 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1695 Select(Node->getOperand(i));
1696 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001697 case ISD::CALLSEQ_START:
1698 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00001699 Select(N.getOperand(0));
1700 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001701 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00001702 PPC::ADJCALLSTACKUP;
1703 BuildMI(BB, Opc, 1).addImm(Tmp1);
1704 return;
1705 case ISD::BR: {
1706 MachineBasicBlock *Dest =
1707 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001708 Select(N.getOperand(0));
1709 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1710 return;
1711 }
Nate Begeman7cbd5252005-08-16 19:49:35 +00001712 case ISD::BR_CC:
1713 case ISD::BRTWOWAY_CC:
Nate Begemana9795f82005-03-24 04:41:43 +00001714 SelectBranchCC(N);
1715 return;
1716 case ISD::CopyToReg:
1717 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001718 Tmp1 = SelectExpr(N.getOperand(2));
1719 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001720
Nate Begemana9795f82005-03-24 04:41:43 +00001721 if (Tmp1 != Tmp2) {
Chris Lattnera8cd0152005-08-16 21:58:15 +00001722 if (N.getOperand(2).getValueType() == MVT::f64 ||
1723 N.getOperand(2).getValueType() == MVT::f32)
Nate Begemana9795f82005-03-24 04:41:43 +00001724 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1725 else
1726 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1727 }
1728 return;
1729 case ISD::ImplicitDef:
1730 Select(N.getOperand(0));
Chris Lattner2b544002005-08-24 23:08:16 +00001731 Tmp1 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1732 if (N.getOperand(1).getValueType() == MVT::i32)
1733 BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Tmp1);
1734 else
1735 BuildMI(BB, PPC::IMPLICIT_DEF_FP, 0, Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001736 return;
1737 case ISD::RET:
1738 switch (N.getNumOperands()) {
1739 default:
1740 assert(0 && "Unknown return instruction!");
1741 case 3:
1742 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1743 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001744 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00001745 Select(N.getOperand(0));
1746 Tmp1 = SelectExpr(N.getOperand(1));
1747 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001748 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1749 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001750 break;
1751 case 2:
1752 Select(N.getOperand(0));
1753 Tmp1 = SelectExpr(N.getOperand(1));
1754 switch (N.getOperand(1).getValueType()) {
1755 default:
1756 assert(0 && "Unknown return type!");
1757 case MVT::f64:
1758 case MVT::f32:
1759 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1760 break;
1761 case MVT::i32:
1762 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1763 break;
1764 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001765 case 1:
1766 Select(N.getOperand(0));
1767 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001768 }
1769 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1770 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001771 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00001772 case ISD::STORE: {
1773 SDOperand Chain = N.getOperand(0);
1774 SDOperand Value = N.getOperand(1);
1775 SDOperand Address = N.getOperand(2);
1776 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00001777
Nate Begeman2497e632005-07-21 20:44:43 +00001778 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00001779
Nate Begeman2497e632005-07-21 20:44:43 +00001780 if (opcode == ISD::STORE) {
1781 switch(Value.getValueType()) {
1782 default: assert(0 && "unknown Type in store");
1783 case MVT::i32: Opc = PPC::STW; break;
1784 case MVT::f64: Opc = PPC::STFD; break;
1785 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001786 }
Nate Begeman2497e632005-07-21 20:44:43 +00001787 } else { //ISD::TRUNCSTORE
1788 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1789 default: assert(0 && "unknown Type in store");
1790 case MVT::i1:
1791 case MVT::i8: Opc = PPC::STB; break;
1792 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001793 }
Nate Begemana9795f82005-03-24 04:41:43 +00001794 }
Nate Begeman2497e632005-07-21 20:44:43 +00001795
1796 if(Address.getOpcode() == ISD::FrameIndex) {
1797 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1798 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00001799 } else {
1800 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001801 switch(SelectAddr(Address, Tmp2, offset)) {
1802 default: assert(0 && "Unhandled return value from SelectAddr");
1803 case 0: // imm offset, no frame, no index
1804 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
1805 break;
1806 case 1: // imm offset + frame index
1807 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
1808 break;
1809 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00001810 Opc = IndexedOpForOp(Opc);
1811 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001812 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001813 case 3: {
1814 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1815 GlobalValue *GV = GN->getGlobal();
1816 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
1817 }
Nate Begeman2497e632005-07-21 20:44:43 +00001818 }
1819 }
1820 return;
1821 }
Nate Begemana9795f82005-03-24 04:41:43 +00001822 case ISD::EXTLOAD:
1823 case ISD::SEXTLOAD:
1824 case ISD::ZEXTLOAD:
1825 case ISD::LOAD:
1826 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001827 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00001828 case ISD::CALL:
1829 case ISD::DYNAMIC_STACKALLOC:
1830 ExprMap.erase(N);
1831 SelectExpr(N);
1832 return;
1833 }
1834 assert(0 && "Should not be reached!");
1835}
1836
1837
1838/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1839/// into a machine code representation using pattern matching and a machine
1840/// description file.
1841///
1842FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001843 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001844}
1845