blob: 4362a1d26cf14d88c668edf371f4498e8fc056e5 [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemana9795f82005-03-24 04:41:43 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000020#include "PPC32ISelLowering.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000021#include "llvm/Constants.h"
Nate Begemana9795f82005-03-24 04:41:43 +000022#include "llvm/Function.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begemana9795f82005-03-24 04:41:43 +000024#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
27#include "llvm/CodeGen/SelectionDAGISel.h"
28#include "llvm/CodeGen/SSARegMap.h"
29#include "llvm/Target/TargetData.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
Nate Begemana9795f82005-03-24 04:41:43 +000038namespace {
Chris Lattner6d9aed42005-08-17 01:25:14 +000039Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
40Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
41Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Chris Lattner3c304a32005-08-05 22:05:03 +000042
Nate Begemana9795f82005-03-24 04:41:43 +000043//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000044// ISel - PPC32 specific code to select PPC32 machine instructions for
45// SelectionDAG operations.
Nate Begemana9795f82005-03-24 04:41:43 +000046//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000047
Nate Begemana9795f82005-03-24 04:41:43 +000048class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +000049 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +000050 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
51 // for sdiv and udiv until it is put into the future
52 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000053
Nate Begemana9795f82005-03-24 04:41:43 +000054 /// ExprMap - As shared expressions are codegen'd, we keep track of which
55 /// vreg the value is produced in, so we only emit one copy of each compiled
56 /// tree.
57 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +000058
59 unsigned GlobalBaseReg;
60 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +000061 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +000062public:
Nate Begeman815d6da2005-04-06 00:25:27 +000063 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
64 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000065
Nate Begemanc7b09f12005-03-25 08:34:25 +000066 /// runOnFunction - Override this function in order to reset our per-function
67 /// variables.
68 virtual bool runOnFunction(Function &Fn) {
69 // Make sure we re-emit a set of the global base reg if necessary
70 GlobalBaseInitialized = false;
71 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000072 }
73
Nate Begemana9795f82005-03-24 04:41:43 +000074 /// InstructionSelectBasicBlock - This callback is invoked by
75 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
76 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
77 DEBUG(BB->dump());
78 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +000079 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +000080 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +000081
Nate Begemana9795f82005-03-24 04:41:43 +000082 // Clear state used for selection.
83 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +000084 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +000085 }
Nate Begeman815d6da2005-04-06 00:25:27 +000086
Chris Lattner54abfc52005-08-11 17:15:31 +000087 // convenience functions for virtual register creation
88 inline unsigned MakeIntReg() {
89 return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
90 }
91 inline unsigned MakeFPReg() {
92 return RegMap->createVirtualRegister(PPC32::FPRCRegisterClass);
93 }
94
Nate Begeman815d6da2005-04-06 00:25:27 +000095 // dag -> dag expanders for integer divide by constant
96 SDOperand BuildSDIVSequence(SDOperand N);
97 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000098
Nate Begemandffcfcc2005-04-01 00:32:34 +000099 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000100 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemanc24d4842005-08-10 20:52:09 +0000101 void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000102 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000103 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begemanc24d4842005-08-10 20:52:09 +0000104 unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
Chris Lattnerb4138c42005-08-10 18:11:33 +0000105 bool SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000106 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000107 bool IsArithmetic = false, bool Negate = false);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000108 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000109 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000110
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000111 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000112 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000113
114 virtual const char *getPassName() const {
115 return "PowerPC Pattern Instruction Selection";
116 }
Nate Begemana9795f82005-03-24 04:41:43 +0000117};
118
Chris Lattner02efa6c2005-08-08 21:08:09 +0000119// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
120// any number of 0s on either side. The 1s are allowed to wrap from LSB to
121// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
122// not, since all 1s are not contiguous.
123static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
124 if (isShiftedMask_32(Val)) {
125 // look for the first non-zero bit
126 MB = CountLeadingZeros_32(Val);
127 // look for the first zero bit after the run of ones
128 ME = CountLeadingZeros_32((Val - 1) ^ Val);
129 return true;
130 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
131 // effectively look for the first zero bit
132 ME = CountLeadingZeros_32(Val) - 1;
133 // effectively look for the first one bit after the run of zeros
134 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
135 return true;
136 }
137 // no run present
138 return false;
139}
140
Chris Lattnercf1cf182005-08-08 21:10:27 +0000141// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
142// and mask opcode and mask operation.
143static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
144 bool IsShiftMask,
145 unsigned &SH, unsigned &MB, unsigned &ME) {
146 if (Shift > 31) return false;
147 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
148
149 if (Opcode == ISD::SHL) { // shift left
150 // apply shift to mask if it comes first
151 if (IsShiftMask) Mask = Mask << Shift;
152 // determine which bits are made indeterminant by shift
153 Indeterminant = ~(0xFFFFFFFFu << Shift);
154 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
155 // apply shift to mask if it comes first
156 if (IsShiftMask) Mask = Mask >> Shift;
157 // determine which bits are made indeterminant by shift
158 Indeterminant = ~(0xFFFFFFFFu >> Shift);
159 // adjust for the left rotate
160 Shift = 32 - Shift;
161 }
162
163 // if the mask doesn't intersect any Indeterminant bits
Jim Laskeycf083e32005-08-12 23:52:46 +0000164 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000165 SH = Shift;
166 // make sure the mask is still a mask (wrap arounds may not be)
167 return isRunOfOnes(Mask, MB, ME);
168 }
169
170 // can't do it
171 return false;
172}
173
Chris Lattner59b21c22005-08-09 18:29:55 +0000174// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000175// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000176static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000177 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000178 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000179 // retrieve value
Jim Laskeyb454cfd2005-08-18 00:15:15 +0000180 Imm = (unsigned)CN->getValue();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000181 // passes muster
182 return true;
183 }
184 // not a constant
185 return false;
186}
187
Jim Laskey191cf942005-08-11 21:59:23 +0000188// isOpcWithIntImmediate - This method tests to see if the node is a specific
189// opcode and that it has a immediate integer right operand.
190// If so Imm will receive the 32 bit value.
191static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) {
192 return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm);
193}
194
Chris Lattnercf1cf182005-08-08 21:10:27 +0000195// isOprShiftImm - Returns true if the specified operand is a shift opcode with
196// a immediate shift count less than 32.
197static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
198 Opc = N.getOpcode();
199 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000200 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000201}
202
203// isOprNot - Returns true if the specified operand is an xor with immediate -1.
204static bool isOprNot(SDOperand N) {
205 unsigned Imm;
Jim Laskey191cf942005-08-11 21:59:23 +0000206 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000207}
208
209// Immediate constant composers.
210// Lo16 - grabs the lo 16 bits from a 32 bit constant.
211// Hi16 - grabs the hi 16 bits from a 32 bit constant.
212// HA16 - computes the hi bits required if the lo bits are add/subtracted in
213// arithmethically.
214static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
215static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
216static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
217
Nate Begemanc7bd4822005-04-11 06:34:10 +0000218/// NodeHasRecordingVariant - If SelectExpr can always produce code for
219/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
220/// return false.
221static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
222 switch(NodeOpcode) {
223 default: return false;
224 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000225 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000226 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000227 }
228}
229
Nate Begeman3e897162005-03-31 23:55:40 +0000230/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
Nate Begemanc24d4842005-08-10 20:52:09 +0000231/// to Condition.
232static unsigned getBCCForSetCC(ISD::CondCode CC) {
233 switch (CC) {
Nate Begeman3e897162005-03-31 23:55:40 +0000234 default: assert(0 && "Unknown condition!"); abort();
235 case ISD::SETEQ: return PPC::BEQ;
236 case ISD::SETNE: return PPC::BNE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000237 case ISD::SETULT:
Nate Begeman3e897162005-03-31 23:55:40 +0000238 case ISD::SETLT: return PPC::BLT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000239 case ISD::SETULE:
Nate Begeman3e897162005-03-31 23:55:40 +0000240 case ISD::SETLE: return PPC::BLE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000241 case ISD::SETUGT:
Nate Begeman3e897162005-03-31 23:55:40 +0000242 case ISD::SETGT: return PPC::BGT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000243 case ISD::SETUGE:
Nate Begeman3e897162005-03-31 23:55:40 +0000244 case ISD::SETGE: return PPC::BGE;
245 }
Nate Begeman04730362005-04-01 04:45:11 +0000246 return 0;
247}
248
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000249/// getCRIdxForSetCC - Return the index of the condition register field
250/// associated with the SetCC condition, and whether or not the field is
251/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000252static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
253 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000254 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000255 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000256 case ISD::SETLT: Inv = false; return 0;
257 case ISD::SETUGE:
258 case ISD::SETGE: Inv = true; return 0;
259 case ISD::SETUGT:
260 case ISD::SETGT: Inv = false; return 1;
261 case ISD::SETULE:
262 case ISD::SETLE: Inv = true; return 1;
263 case ISD::SETEQ: Inv = false; return 2;
264 case ISD::SETNE: Inv = true; return 2;
265 }
266 return 0;
267}
268
Nate Begeman04730362005-04-01 04:45:11 +0000269/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
270/// and store immediate instructions.
271static unsigned IndexedOpForOp(unsigned Opcode) {
272 switch(Opcode) {
273 default: assert(0 && "Unknown opcode!"); abort();
274 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
275 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
276 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
277 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
278 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
279 case PPC::LFD: return PPC::LFDX;
280 }
281 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000282}
Nate Begeman815d6da2005-04-06 00:25:27 +0000283
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000284// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000285// a multiply.
286struct ms {
287 int m; // magic number
288 int s; // shift amount
289};
290
291struct mu {
292 unsigned int m; // magic number
293 int a; // add indicator
294 int s; // shift amount
295};
296
297/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000298/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000299/// or -1.
300static struct ms magic(int d) {
301 int p;
302 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000303 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000304 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000305
Nate Begeman815d6da2005-04-06 00:25:27 +0000306 ad = abs(d);
307 t = two31 + ((unsigned int)d >> 31);
308 anc = t - 1 - t%ad; // absolute value of nc
309 p = 31; // initialize p
310 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
311 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
312 q2 = two31/ad; // initialize q2 = 2p/abs(d)
313 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
314 do {
315 p = p + 1;
316 q1 = 2*q1; // update q1 = 2p/abs(nc)
317 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
318 if (r1 >= anc) { // must be unsigned comparison
319 q1 = q1 + 1;
320 r1 = r1 - anc;
321 }
322 q2 = 2*q2; // update q2 = 2p/abs(d)
323 r2 = 2*r2; // update r2 = rem(2p/abs(d))
324 if (r2 >= ad) { // must be unsigned comparison
325 q2 = q2 + 1;
326 r2 = r2 - ad;
327 }
328 delta = ad - r2;
329 } while (q1 < delta || (q1 == delta && r1 == 0));
330
331 mag.m = q2 + 1;
332 if (d < 0) mag.m = -mag.m; // resulting magic number
333 mag.s = p - 32; // resulting shift
334 return mag;
335}
336
337/// magicu - calculate the magic numbers required to codegen an integer udiv as
338/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
339static struct mu magicu(unsigned d)
340{
341 int p;
342 unsigned int nc, delta, q1, r1, q2, r2;
343 struct mu magu;
344 magu.a = 0; // initialize "add" indicator
345 nc = - 1 - (-d)%d;
346 p = 31; // initialize p
347 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
348 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
349 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
350 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
351 do {
352 p = p + 1;
353 if (r1 >= nc - r1 ) {
354 q1 = 2*q1 + 1; // update q1
355 r1 = 2*r1 - nc; // update r1
356 }
357 else {
358 q1 = 2*q1; // update q1
359 r1 = 2*r1; // update r1
360 }
361 if (r2 + 1 >= d - r2) {
362 if (q2 >= 0x7FFFFFFF) magu.a = 1;
363 q2 = 2*q2 + 1; // update q2
364 r2 = 2*r2 + 1 - d; // update r2
365 }
366 else {
367 if (q2 >= 0x80000000) magu.a = 1;
368 q2 = 2*q2; // update q2
369 r2 = 2*r2 + 1; // update r2
370 }
371 delta = d - 1 - r2;
372 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
373 magu.m = q2 + 1; // resulting magic number
374 magu.s = p - 32; // resulting shift
375 return magu;
376}
377}
378
379/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
380/// return a DAG expression to select that will generate the same value by
381/// multiplying by a magic number. See:
382/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
383SDOperand ISel::BuildSDIVSequence(SDOperand N) {
384 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
385 ms magics = magic(d);
386 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000387 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000388 ISelDAG->getConstant(magics.m, MVT::i32));
389 // If d > 0 and m < 0, add the numerator
390 if (d > 0 && magics.m < 0)
391 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
392 // If d < 0 and m > 0, subtract the numerator.
393 if (d < 0 && magics.m > 0)
394 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
395 // Shift right algebraic if shift value is nonzero
396 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000397 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000398 ISelDAG->getConstant(magics.s, MVT::i32));
399 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000400 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000401 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000402 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000403}
404
405/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
406/// return a DAG expression to select that will generate the same value by
407/// multiplying by a magic number. See:
408/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
409SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000410 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000411 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
412 mu magics = magicu(d);
413 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000414 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000415 ISelDAG->getConstant(magics.m, MVT::i32));
416 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000417 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000418 ISelDAG->getConstant(magics.s, MVT::i32));
419 } else {
420 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000421 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000422 ISelDAG->getConstant(1, MVT::i32));
423 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000424 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000425 ISelDAG->getConstant(magics.s-1, MVT::i32));
426 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000427 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000428}
429
Nate Begemanc7b09f12005-03-25 08:34:25 +0000430/// getGlobalBaseReg - Output the instructions required to put the
431/// base address to use for accessing globals into a register.
432///
433unsigned ISel::getGlobalBaseReg() {
434 if (!GlobalBaseInitialized) {
435 // Insert the set of GlobalBaseReg into the first MBB of the function
436 MachineBasicBlock &FirstMBB = BB->getParent()->front();
437 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000438 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000439 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
Chris Lattner3f852b42005-08-18 23:24:50 +0000440 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000441 GlobalBaseInitialized = true;
442 }
443 return GlobalBaseReg;
444}
445
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000446/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000447/// Constant Pool. Optionally takes a register in which to load the value.
448unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000449 unsigned Tmp1 = MakeIntReg();
450 if (0 == Result) Result = MakeFPReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000451 MachineConstantPool *CP = BB->getParent()->getConstantPool();
452 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
453 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000454 if (PICEnabled)
455 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
456 .addConstantPoolIndex(CPI);
457 else
458 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000459 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
460 return Result;
461}
462
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000463/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000464/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000465void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
466 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000467 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000468 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000469 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000470 bool GPOpt =
471 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
Nate Begeman27d53ba2005-08-19 03:42:28 +0000472 if (GPOpt)
473 BuildMI(BB, PPC::MFOCRF, 1, IntCR).addReg(PPC::CR7);
474 else
475 BuildMI(BB, PPC::MFCR, 0, IntCR);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000476 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000477 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000478 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
479 .addImm(31).addImm(31);
480 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
481 } else {
482 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
483 .addImm(31).addImm(31);
484 }
485}
486
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000487/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000488/// the rotate left word immediate then mask insert (rlwimi) instruction.
489/// Returns true on success, false if the caller still needs to select OR.
490///
491/// Patterns matched:
492/// 1. or shl, and 5. or and, and
493/// 2. or and, shl 6. or shl, shr
494/// 3. or shr, and 7. or shr, shl
495/// 4. or and, shr
496bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000497 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000498 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000499 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000500
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000501 SDOperand Op0 = OR.getOperand(0);
502 SDOperand Op1 = OR.getOperand(1);
503
504 unsigned Op0Opc = Op0.getOpcode();
505 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000506
Nate Begeman7ddecb42005-04-06 23:51:40 +0000507 // Verify that we have the correct opcodes
508 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
509 return false;
510 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
511 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000512
Nate Begeman7ddecb42005-04-06 23:51:40 +0000513 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +0000514 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000515 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000516 case ISD::SHL: TgtMask <<= Value; break;
517 case ISD::SRL: TgtMask >>= Value; break;
518 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000519 }
520 } else {
521 return false;
522 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000523
Nate Begeman7ddecb42005-04-06 23:51:40 +0000524 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +0000525 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000526 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000527 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000528 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000529 InsMask <<= Amount;
530 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000531 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000532 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000533 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000534 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000535 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000536 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000537 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000538 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000539 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000540 break;
541 }
542 } else {
543 return false;
544 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000545
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000546 unsigned Tmp3 = 0;
547
548 // If both of the inputs are ANDs and one of them has a logical shift by
549 // constant as its input, make that the inserted value so that we can combine
550 // the shift into the rotate part of the rlwimi instruction
551 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000552 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000553 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000554 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000555 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000556 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000557 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
558 }
559 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
560 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000561 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000562 std::swap(Op0, Op1);
563 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +0000564 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000565 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000566 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
567 }
568 }
569 }
570
Nate Begeman7ddecb42005-04-06 23:51:40 +0000571 // Verify that the Target mask and Insert mask together form a full word mask
572 // and that the Insert mask is a run of set bits (which implies both are runs
573 // of set bits). Given that, Select the arguments and generate the rlwimi
574 // instruction.
575 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +0000576 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000577 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000578 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000579 // Check for rotlwi / rotrwi here, a special case of bitfield insert
580 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000581 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +0000582 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000583 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
584 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
585 .addImm(0).addImm(31);
586 return true;
587 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000588 if (Op0Opc == ISD::AND && fullMask)
589 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000590 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000591 Tmp1 = SelectExpr(Op0);
592 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000593 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
594 .addImm(Amount).addImm(MB).addImm(ME);
595 return true;
596 }
597 return false;
598}
599
Nate Begeman3664cef2005-04-13 22:14:14 +0000600/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
601/// low six bits. If the shift amount is an ISD::AND node with a mask that is
602/// wider than the implicit mask, then we can get rid of the AND and let the
603/// shift do the mask.
604unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Jim Laskey191cf942005-08-11 21:59:23 +0000605 unsigned C;
606 if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63)
Nate Begeman3664cef2005-04-13 22:14:14 +0000607 return SelectExpr(N.getOperand(0));
608 else
609 return SelectExpr(N);
610}
611
Nate Begemanc24d4842005-08-10 20:52:09 +0000612unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000613 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000614 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000615 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +0000616 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000617
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000618 // Allocate a condition register for this expression
619 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000620
Nate Begemanc24d4842005-08-10 20:52:09 +0000621 // Use U to determine whether the SETCC immediate range is signed or not.
622 bool U = ISD::isUnsignedIntSetCC(CC);
623 if (isIntImmediate(RHS, Tmp2) &&
624 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
625 Tmp2 = Lo16(Tmp2);
626 // For comparisons against zero, we can implicity set CR0 if a recording
627 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
628 // operand zero of the SetCC node is available.
629 if (Tmp2 == 0 &&
630 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
631 RecordSuccess = false;
632 Tmp1 = SelectExpr(LHS, true);
633 if (RecordSuccess) {
634 ++Recorded;
635 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
636 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000637 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000638 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000639 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000640 // If we could not implicitly set CR0, then emit a compare immediate
641 // instead.
642 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
643 if (U)
644 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
645 else
646 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000647 } else {
Nate Begemanc24d4842005-08-10 20:52:09 +0000648 bool IsInteger = MVT::isInteger(LHS.getValueType());
649 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
650 Tmp1 = SelectExpr(LHS);
651 Tmp2 = SelectExpr(RHS);
652 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000653 }
654 return Result;
655}
656
Nate Begemand3ded2d2005-08-08 22:22:56 +0000657/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000658unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000659{
Nate Begeman96fc6812005-03-31 02:05:53 +0000660 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000661 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000662 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +0000663 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +0000664 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000665 if (isFrame) {
666 ++FrameOff;
667 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
668 return 1;
669 } else {
670 Reg = SelectExpr(N.getOperand(0));
671 return 0;
672 }
673 } else {
674 Reg = SelectExpr(N.getOperand(0));
675 offset = SelectExpr(N.getOperand(1));
676 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000677 }
Nate Begeman04730362005-04-01 04:45:11 +0000678 }
Nate Begemand3ded2d2005-08-08 22:22:56 +0000679 // Now check if we're dealing with a global, and whether or not we should emit
680 // an optimized load or store for statics.
681 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
682 GlobalValue *GV = GN->getGlobal();
683 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000684 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +0000685 if (PICEnabled)
686 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
687 .addGlobalAddress(GV);
688 else
689 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
690 Reg = GlobalHi;
691 offset = 0;
692 return 3;
693 }
694 }
Nate Begemana9795f82005-03-24 04:41:43 +0000695 Reg = SelectExpr(N);
696 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000697 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000698}
699
700void ISel::SelectBranchCC(SDOperand N)
701{
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000702 MachineBasicBlock *Dest =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000703 cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000704
Nate Begemana9795f82005-03-24 04:41:43 +0000705 Select(N.getOperand(0)); //chain
Nate Begeman7cbd5252005-08-16 19:49:35 +0000706 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get();
707 unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC);
Nate Begemanc24d4842005-08-10 20:52:09 +0000708 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000709
Nate Begeman439009c2005-06-15 18:22:43 +0000710 // Iterate to the next basic block
711 ilist<MachineBasicBlock>::iterator It = BB;
712 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000713
714 // If this is a two way branch, then grab the fallthrough basic block argument
715 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
716 // if necessary by the branch selection pass. Otherwise, emit a standard
717 // conditional branch.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000718 if (N.getOpcode() == ISD::BRTWOWAY_CC) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000719 MachineBasicBlock *Fallthrough =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000720 cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000721 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000722 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +0000723 .addMBB(Dest).addMBB(Fallthrough);
724 if (Fallthrough != It)
725 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
726 } else {
727 if (Fallthrough != It) {
728 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000729 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +0000730 .addMBB(Fallthrough).addMBB(Dest);
731 }
732 }
733 } else {
Nate Begeman439009c2005-06-15 18:22:43 +0000734 // If the fallthrough path is off the end of the function, which would be
735 // undefined behavior, set it to be the same as the current block because
736 // we have nothing better to set it to, and leaving it alone will cause the
737 // PowerPC Branch Selection pass to crash.
738 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000739 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +0000740 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000741 }
Nate Begemana9795f82005-03-24 04:41:43 +0000742 return;
743}
744
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000745// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +0000746bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000747 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000748 bool IsArithmetic, bool Negate) {
749 // check constant
750 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
751 // exit if not a constant
752 if (!CN) return false;
753 // extract immediate
Chris Lattner6d9aed42005-08-17 01:25:14 +0000754 unsigned C = (unsigned)CN->getValue();
Chris Lattnerb4138c42005-08-10 18:11:33 +0000755 // negate if required (ISD::SUB)
756 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000757 // get the hi and lo portions of constant
758 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
759 unsigned Lo = Lo16(C);
760 // assume no intermediate result from lo instruction (same as final result)
761 unsigned Tmp = Result;
762 // check if two instructions are needed
763 if (Hi && Lo) {
764 // exit if usage indicates it would be better to load immediate into a
765 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +0000766 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000767 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +0000768 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000769 }
770 // get first operand
771 unsigned Opr0 = SelectExpr(N.getOperand(0));
772 // is a lo instruction needed
773 if (Lo) {
Chris Lattner6d9aed42005-08-17 01:25:14 +0000774 // generate instruction for lo portion
775 BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000776 // need to switch out first operand for hi instruction
777 Opr0 = Tmp;
778 }
Chris Lattner6d9aed42005-08-17 01:25:14 +0000779 // is a hi instruction needed
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000780 if (Hi) {
781 // generate instruction for hi portion
Chris Lattner6d9aed42005-08-17 01:25:14 +0000782 BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000783 }
784 return true;
785}
786
Nate Begemanc7bd4822005-04-11 06:34:10 +0000787unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +0000788 unsigned Result;
789 unsigned Tmp1, Tmp2, Tmp3;
790 unsigned Opc = 0;
791 unsigned opcode = N.getOpcode();
792
793 SDNode *Node = N.Val;
794 MVT::ValueType DestType = N.getValueType();
795
Chris Lattnera8cd0152005-08-16 21:58:15 +0000796 if (Node->getOpcode() == ISD::CopyFromReg) {
797 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana43b1762005-06-14 03:55:23 +0000798 // Just use the specified register as our input.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000799 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1)
800 return Reg;
801 }
Nate Begemana43b1762005-06-14 03:55:23 +0000802
Nate Begemana9795f82005-03-24 04:41:43 +0000803 unsigned &Reg = ExprMap[N];
804 if (Reg) return Reg;
805
Nate Begeman27eeb002005-04-02 05:59:34 +0000806 switch (N.getOpcode()) {
807 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000808 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000809 MakeReg(N.getValueType()) : 1;
810 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000811 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +0000812 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000813 // If this is a call instruction, make sure to prepare ALL of the result
814 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000815 if (Node->getNumValues() == 1)
816 Reg = Result = 1; // Void call, just a chain.
817 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000818 Result = MakeReg(Node->getValueType(0));
819 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000820 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000821 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000822 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000823 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000824 break;
825 case ISD::ADD_PARTS:
826 case ISD::SUB_PARTS:
827 case ISD::SHL_PARTS:
828 case ISD::SRL_PARTS:
829 case ISD::SRA_PARTS:
830 Result = MakeReg(Node->getValueType(0));
831 ExprMap[N.getValue(0)] = Result;
832 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
833 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
834 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000835 }
836
Nate Begemana9795f82005-03-24 04:41:43 +0000837 switch (opcode) {
838 default:
Nate Begeman5a014812005-08-14 01:17:16 +0000839 Node->dump(); std::cerr << '\n';
840 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000841 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000842 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
843 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000844 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000845 // Generate both result values. FIXME: Need a better commment here?
846 if (Result != 1)
847 ExprMap[N.getValue(1)] = 1;
848 else
849 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
850
851 // FIXME: We are currently ignoring the requested alignment for handling
852 // greater than the stack alignment. This will need to be revisited at some
853 // point. Align = N.getOperand(2);
854 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
855 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
856 std::cerr << "Cannot allocate stack object with greater alignment than"
857 << " the stack alignment yet!";
858 abort();
859 }
860 Select(N.getOperand(0));
861 Tmp1 = SelectExpr(N.getOperand(1));
862 // Subtract size from stack pointer, thereby allocating some space.
863 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
864 // Put a pointer to the space into the result register by copying the SP
865 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
866 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000867
868 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000869 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
Chris Lattner54abfc52005-08-11 17:15:31 +0000870 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000871 if (PICEnabled)
872 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
873 .addConstantPoolIndex(Tmp1);
874 else
875 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000876 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
877 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000878
879 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000880 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000881 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000882 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000883
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000884 case ISD::GlobalAddress: {
885 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +0000886 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000887 if (PICEnabled)
888 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
889 .addGlobalAddress(GV);
890 else
Chris Lattner4015ea82005-07-28 04:42:11 +0000891 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000892 if (GV->hasWeakLinkage() || GV->isExternal()) {
893 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
894 } else {
895 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
896 }
897 return Result;
898 }
899
Nate Begeman5e966612005-03-24 06:28:42 +0000900 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000901 case ISD::EXTLOAD:
902 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000903 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000904 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000905 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +0000906 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000907
Nate Begeman5e966612005-03-24 06:28:42 +0000908 // Make sure we generate both values.
909 if (Result != 1)
910 ExprMap[N.getValue(1)] = 1; // Generate the token
911 else
912 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
913
914 SDOperand Chain = N.getOperand(0);
915 SDOperand Address = N.getOperand(1);
916 Select(Chain);
917
Nate Begeman9db505c2005-03-28 19:36:43 +0000918 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000919 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000920 case MVT::i1: Opc = PPC::LBZ; break;
921 case MVT::i8: Opc = PPC::LBZ; break;
922 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
923 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000924 case MVT::f32: Opc = PPC::LFS; break;
925 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000926 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000927
Nate Begeman74d73452005-03-31 00:15:26 +0000928 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000929 Tmp1 = MakeIntReg();
Nate Begeman74d73452005-03-31 00:15:26 +0000930 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +0000931 if (PICEnabled)
932 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
933 .addConstantPoolIndex(CPI);
934 else
935 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +0000936 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +0000937 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000938 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
939 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000940 } else {
941 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000942 switch(SelectAddr(Address, Tmp1, offset)) {
943 default: assert(0 && "Unhandled return value from SelectAddr");
944 case 0: // imm offset, no frame, no index
945 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
946 break;
947 case 1: // imm offset + frame index
948 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
949 break;
950 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +0000951 Opc = IndexedOpForOp(Opc);
952 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000953 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +0000954 case 3: {
955 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
956 GlobalValue *GV = GN->getGlobal();
957 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
958 }
Nate Begeman04730362005-04-01 04:45:11 +0000959 }
Nate Begeman5e966612005-03-24 06:28:42 +0000960 }
961 return Result;
962 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000963
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000964 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000965 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000966 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000967 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000968 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
969 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
970 };
971 static const unsigned FPR[] = {
972 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
973 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
974 };
975
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000976 // Lower the chain for this call.
977 Select(N.getOperand(0));
978 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000979
Nate Begemand860aa62005-04-04 22:17:48 +0000980 MachineInstr *CallMI;
981 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000982 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +0000983 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000984 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +0000985 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000986 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +0000987 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000988 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +0000989 true);
990 } else {
991 Tmp1 = SelectExpr(N.getOperand(1));
992 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
993 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
994 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
995 .addReg(PPC::R12);
996 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000997
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000998 // Load the register args to virtual regs
999 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001000 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001001 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1002
1003 // Copy the virtual registers into the appropriate argument register
1004 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1005 switch(N.getOperand(i+2).getValueType()) {
1006 default: Node->dump(); assert(0 && "Unknown value type for call");
1007 case MVT::i1:
1008 case MVT::i8:
1009 case MVT::i16:
1010 case MVT::i32:
1011 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001012 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001013 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001014 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1015 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001016 ++GPR_idx;
1017 break;
1018 case MVT::f64:
1019 case MVT::f32:
1020 assert(FPR_idx < 13 && "Too many fp args");
1021 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001022 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001023 ++FPR_idx;
1024 break;
1025 }
1026 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001027
Nate Begemand860aa62005-04-04 22:17:48 +00001028 // Put the call instruction in the correct place in the MachineBasicBlock
1029 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001030
1031 switch (Node->getValueType(0)) {
1032 default: assert(0 && "Unknown value type for call result!");
1033 case MVT::Other: return 1;
1034 case MVT::i1:
1035 case MVT::i8:
1036 case MVT::i16:
1037 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001038 if (Node->getValueType(1) == MVT::i32) {
1039 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1040 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1041 } else {
1042 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1043 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001044 break;
1045 case MVT::f32:
1046 case MVT::f64:
1047 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1048 break;
1049 }
1050 return Result+N.ResNo;
1051 }
Nate Begemana9795f82005-03-24 04:41:43 +00001052
1053 case ISD::SIGN_EXTEND:
1054 case ISD::SIGN_EXTEND_INREG:
1055 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001056 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001057 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001058 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001059 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001060 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001061 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001062 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001063 break;
Nate Begeman74747862005-03-29 22:24:51 +00001064 case MVT::i1:
1065 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1066 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001067 }
Nate Begemana9795f82005-03-24 04:41:43 +00001068 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001069
Nate Begemana9795f82005-03-24 04:41:43 +00001070 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001071 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001072 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001073 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Chris Lattnera8cd0152005-08-16 21:58:15 +00001074 Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001075 if (MVT::isInteger(DestType))
1076 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1077 else
1078 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001079 return Result;
1080
1081 case ISD::SHL:
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::SHL, 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(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001094 .addImm(31-Tmp2);
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::SLW, 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::SRL:
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::SRL, 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 Begeman33162522005-03-29 21:54:38 +00001114 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001115 .addImm(Tmp2).addImm(31);
1116 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001117 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001118 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001119 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1120 }
1121 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001122
Nate Begeman5e966612005-03-24 06:28:42 +00001123 case ISD::SRA:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001124 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001125 unsigned SH, MB, ME;
1126 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1127 isRotateAndMask(ISD::SRA, Tmp2, Tmp3, true, SH, MB, ME)) {
1128 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1129 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1130 .addImm(MB).addImm(ME);
1131 return Result;
1132 }
1133 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001134 Tmp2 &= 0x1F;
Nate Begeman5e966612005-03-24 06:28:42 +00001135 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1136 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001137 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001138 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001139 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1140 }
1141 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001142
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001143 case ISD::CTLZ:
1144 Tmp1 = SelectExpr(N.getOperand(0));
1145 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1146 return Result;
1147
Nate Begemana9795f82005-03-24 04:41:43 +00001148 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001149 if (!MVT::isInteger(DestType)) {
1150 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1151 N.getOperand(0).Val->hasOneUse()) {
1152 ++FusedFP; // Statistic
1153 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1154 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1155 Tmp3 = SelectExpr(N.getOperand(1));
1156 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1157 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1158 return Result;
1159 }
1160 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1161 N.getOperand(1).Val->hasOneUse()) {
1162 ++FusedFP; // Statistic
1163 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1164 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1165 Tmp3 = SelectExpr(N.getOperand(0));
1166 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1167 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1168 return Result;
1169 }
1170 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1171 Tmp1 = SelectExpr(N.getOperand(0));
1172 Tmp2 = SelectExpr(N.getOperand(1));
1173 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1174 return Result;
1175 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001176 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
1177 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001178 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001179 Tmp2 = SelectExpr(N.getOperand(1));
1180 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001181 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001182
Nate Begemana9795f82005-03-24 04:41:43 +00001183 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001184 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001185 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1186 unsigned SH, MB, ME;
1187 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1188 unsigned OprOpc;
1189 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1190 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001191 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001192 } else {
1193 Tmp1 = SelectExpr(N.getOperand(0));
1194 isRunOfOnes(Tmp2, MB, ME);
1195 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001196 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001197 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1198 .addImm(MB).addImm(ME);
1199 RecordSuccess = true;
1200 return Result;
1201 } else if (isUInt16(Tmp2)) {
1202 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001203 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001204 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001205 RecordSuccess = true;
1206 return Result;
1207 } else if (isUInt16(Tmp2)) {
1208 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001209 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001210 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001211 RecordSuccess = true;
1212 return Result;
1213 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001214 }
Jim Laskey847c3a92005-08-12 23:38:02 +00001215 if (isOprNot(N.getOperand(1))) {
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1218 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1219 RecordSuccess = false;
1220 return Result;
1221 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001222 if (isOprNot(N.getOperand(0))) {
Jim Laskey847c3a92005-08-12 23:38:02 +00001223 Tmp1 = SelectExpr(N.getOperand(1));
1224 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1225 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001226 RecordSuccess = false;
1227 return Result;
1228 }
1229 // emit a regular and
1230 Tmp1 = SelectExpr(N.getOperand(0));
1231 Tmp2 = SelectExpr(N.getOperand(1));
1232 Opc = Recording ? PPC::ANDo : PPC::AND;
1233 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001234 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001235 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001236
Nate Begemana9795f82005-03-24 04:41:43 +00001237 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001238 if (SelectBitfieldInsert(N, Result))
1239 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001240 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1241 return Result;
Jim Laskey847c3a92005-08-12 23:38:02 +00001242 if (isOprNot(N.getOperand(1))) {
1243 Tmp1 = SelectExpr(N.getOperand(0));
1244 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1245 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1246 RecordSuccess = false;
1247 return Result;
1248 }
1249 if (isOprNot(N.getOperand(0))) {
1250 Tmp1 = SelectExpr(N.getOperand(1));
1251 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1252 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1253 RecordSuccess = false;
1254 return Result;
1255 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001256 // emit regular or
1257 Tmp1 = SelectExpr(N.getOperand(0));
1258 Tmp2 = SelectExpr(N.getOperand(1));
1259 Opc = Recording ? PPC::ORo : PPC::OR;
1260 RecordSuccess = true;
1261 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001262 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001263
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001264 case ISD::XOR: {
1265 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001266 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001267 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1268 Tmp2 = SelectExpr(N.getOperand(1));
1269 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1270 return Result;
1271 }
Chris Lattner837a5212005-04-21 21:09:11 +00001272 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001273 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001274 switch(N.getOperand(0).getOpcode()) {
1275 case ISD::OR:
1276 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1277 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1278 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1279 break;
1280 case ISD::AND:
1281 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1282 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1283 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1284 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001285 case ISD::XOR:
1286 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1288 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1289 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001290 default:
1291 Tmp1 = SelectExpr(N.getOperand(0));
1292 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1293 break;
1294 }
1295 return Result;
1296 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001297 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1298 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001299 // emit regular xor
1300 Tmp1 = SelectExpr(N.getOperand(0));
1301 Tmp2 = SelectExpr(N.getOperand(1));
1302 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001303 return Result;
1304 }
1305
Chris Lattner5b909172005-08-08 21:30:29 +00001306 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001307 if (!MVT::isInteger(DestType)) {
1308 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1309 N.getOperand(0).Val->hasOneUse()) {
1310 ++FusedFP; // Statistic
1311 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1312 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1313 Tmp3 = SelectExpr(N.getOperand(1));
1314 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1315 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1316 return Result;
1317 }
1318 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1319 N.getOperand(1).Val->hasOneUse()) {
1320 ++FusedFP; // Statistic
1321 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1322 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1323 Tmp3 = SelectExpr(N.getOperand(0));
1324 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1325 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1326 return Result;
1327 }
1328 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1329 Tmp1 = SelectExpr(N.getOperand(0));
1330 Tmp2 = SelectExpr(N.getOperand(1));
1331 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1332 return Result;
1333 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001334 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001335 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001336 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001337 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001338 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001339 }
1340 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001341 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001342 Tmp1 = SelectExpr(N.getOperand(0));
1343 Tmp2 = SelectExpr(N.getOperand(1));
1344 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001345 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001346
Nate Begeman5e966612005-03-24 06:28:42 +00001347 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001348 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001349 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001350 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001351 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001352 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001353 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001354 switch (DestType) {
1355 default: assert(0 && "Unknown type to ISD::MUL"); break;
1356 case MVT::i32: Opc = PPC::MULLW; break;
1357 case MVT::f32: Opc = PPC::FMULS; break;
1358 case MVT::f64: Opc = PPC::FMUL; break;
1359 }
1360 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001361 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001362 return Result;
1363
Nate Begeman815d6da2005-04-06 00:25:27 +00001364 case ISD::MULHS:
1365 case ISD::MULHU:
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 Tmp2 = SelectExpr(N.getOperand(1));
1368 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1369 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1370 return Result;
1371
Nate Begemanf3d08f32005-03-29 00:03:27 +00001372 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001373 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001374 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1375 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001376 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001377 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001378 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1379 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001380 return Result;
1381 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1382 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001383 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001384 Tmp1 = MakeIntReg();
1385 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001386 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1387 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1388 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1389 return Result;
Nate Begeman9f833d32005-04-12 00:10:02 +00001390 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001391 }
1392 // fall thru
1393 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001394 // If this is a divide by constant, we can emit code using some magic
1395 // constants to implement it as a multiply instead.
Chris Lattner59b21c22005-08-09 18:29:55 +00001396 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001397 if (opcode == ISD::SDIV) {
1398 if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) {
1399 ExprMap.erase(N);
1400 return SelectExpr(BuildSDIVSequence(N));
1401 }
1402 } else {
1403 if ((signed)Tmp3 > 1) {
1404 ExprMap.erase(N);
1405 return SelectExpr(BuildUDIVSequence(N));
1406 }
1407 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001408 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001409 Tmp1 = SelectExpr(N.getOperand(0));
1410 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001411 switch (DestType) {
1412 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1413 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1414 case MVT::f32: Opc = PPC::FDIVS; break;
1415 case MVT::f64: Opc = PPC::FDIV; break;
1416 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001417 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1418 return Result;
1419
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001420 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001421 case ISD::SUB_PARTS: {
1422 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1423 "Not an i64 add/sub!");
Nate Begeman456f1e82005-08-17 00:20:08 +00001424 unsigned Tmp4 = 0;
1425 bool ME = isIntImmediate(N.getOperand(3),Tmp3) && ((signed)Tmp3 == -1);
1426 bool ZE = isIntImmediate(N.getOperand(3),Tmp3) && (Tmp3 == 0);
1427 bool IM = isIntImmediate(N.getOperand(2),Tmp3) && ((signed)Tmp3 >= -32768 ||
1428 (signed)Tmp3 < 32768);
1429 Tmp1 = SelectExpr(N.getOperand(0));
1430 Tmp2 = SelectExpr(N.getOperand(1));
1431 if (!IM || N.getOpcode() == ISD::SUB_PARTS)
1432 Tmp3 = SelectExpr(N.getOperand(2));
1433 if ((!ME && !ZE) || N.getOpcode() == ISD::SUB_PARTS)
1434 Tmp4 = SelectExpr(N.getOperand(3));
1435
Nate Begemanca12a2b2005-03-28 22:28:37 +00001436 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001437 // Codegen the low 32 bits of the add. Interestingly, there is no shifted
1438 // form of add immediate carrying.
1439 if (IM)
1440 BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1441 else
1442 BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3);
1443 // Codegen the high 32 bits, adding zero, minus one, or the full value
1444 // along with the carry flag produced by addc/addic to tmp2.
1445 if (ZE)
1446 BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2);
1447 else if (ME)
1448 BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2);
1449 else
1450 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001451 } else {
Nate Begeman456f1e82005-08-17 00:20:08 +00001452 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1);
1453 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2);
Nate Begeman27eeb002005-04-02 05:59:34 +00001454 }
1455 return Result+N.ResNo;
1456 }
1457
1458 case ISD::SHL_PARTS:
1459 case ISD::SRA_PARTS:
1460 case ISD::SRL_PARTS: {
1461 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1462 "Not an i64 shift!");
1463 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1464 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001465 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
Chris Lattner54abfc52005-08-11 17:15:31 +00001466 Tmp1 = MakeIntReg();
1467 Tmp2 = MakeIntReg();
1468 Tmp3 = MakeIntReg();
1469 unsigned Tmp4 = MakeIntReg();
1470 unsigned Tmp5 = MakeIntReg();
1471 unsigned Tmp6 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001472 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1473 if (ISD::SHL_PARTS == opcode) {
1474 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1475 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1476 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1477 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001478 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001479 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1480 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1481 } else if (ISD::SRL_PARTS == opcode) {
1482 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1483 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1484 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1485 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1486 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1487 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1488 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1489 } else {
1490 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1491 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1492 MachineBasicBlock *OldMBB = BB;
1493 MachineFunction *F = BB->getParent();
1494 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1495 F->getBasicBlockList().insert(It, TmpMBB);
1496 F->getBasicBlockList().insert(It, PhiMBB);
1497 BB->addSuccessor(TmpMBB);
1498 BB->addSuccessor(PhiMBB);
1499 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1500 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1501 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1502 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1503 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1504 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1505 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1506 // Select correct least significant half if the shift amount > 32
1507 BB = TmpMBB;
Chris Lattner54abfc52005-08-11 17:15:31 +00001508 unsigned Tmp7 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001509 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1510 TmpMBB->addSuccessor(PhiMBB);
1511 BB = PhiMBB;
1512 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1513 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001514 }
1515 return Result+N.ResNo;
1516 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001517
Nate Begeman6b559972005-04-01 02:59:27 +00001518 case ISD::FP_TO_SINT: {
Nate Begeman6b559972005-04-01 02:59:27 +00001519 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman5a014812005-08-14 01:17:16 +00001520 Tmp2 = MakeFPReg();
1521 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1522 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1523 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1524 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1525 return Result;
Nate Begeman6b559972005-04-01 02:59:27 +00001526 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001527
Chris Lattner88ac32c2005-08-09 20:21:10 +00001528 case ISD::SETCC: {
1529 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1530 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
1531 // We can codegen setcc op, imm very efficiently compared to a brcond.
1532 // Check for those cases here.
1533 // setcc op, 0
1534 if (Tmp3 == 0) {
1535 Tmp1 = SelectExpr(Node->getOperand(0));
1536 switch (CC) {
1537 default: Node->dump(); assert(0 && "Unhandled SetCC condition"); abort();
1538 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001539 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001540 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1541 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1542 .addImm(5).addImm(31);
1543 break;
1544 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001545 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001546 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1547 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1548 break;
1549 case ISD::SETLT:
1550 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1551 .addImm(31).addImm(31);
1552 break;
1553 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001554 Tmp2 = MakeIntReg();
1555 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001556 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1557 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1558 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1559 .addImm(31).addImm(31);
1560 break;
Nate Begeman9765c252005-04-12 21:22:28 +00001561 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001562 return Result;
1563 } else if (Tmp3 == ~0U) { // setcc op, -1
1564 Tmp1 = SelectExpr(Node->getOperand(0));
1565 switch (CC) {
1566 default: assert(0 && "Unhandled SetCC condition"); abort();
1567 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001568 Tmp2 = MakeIntReg();
1569 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001570 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
1571 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
1572 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
1573 break;
1574 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001575 Tmp2 = MakeIntReg();
1576 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001577 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1578 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
1579 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
1580 break;
1581 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001582 Tmp2 = MakeIntReg();
1583 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001584 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
1585 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1586 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1587 .addImm(31).addImm(31);
1588 break;
1589 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001590 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001591 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1592 .addImm(31).addImm(31);
1593 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1594 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001595 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001596 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001597 }
Nate Begeman33162522005-03-29 21:54:38 +00001598 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001599
Nate Begemanc24d4842005-08-10 20:52:09 +00001600 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1601 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001602 return Result;
1603 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001604
1605 case ISD::SELECT_CC: {
1606 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
1607 if (!MVT::isInteger(N.getOperand(0).getValueType()) &&
1608 !MVT::isInteger(N.getOperand(2).getValueType()) &&
1609 CC != ISD::SETEQ && CC != ISD::SETNE) {
1610 MVT::ValueType VT = N.getOperand(0).getValueType();
1611 unsigned TV = SelectExpr(N.getOperand(2)); // Use if TRUE
1612 unsigned FV = SelectExpr(N.getOperand(3)); // Use if FALSE
Nate Begemana3fd4002005-07-19 16:51:05 +00001613
Nate Begemanc24d4842005-08-10 20:52:09 +00001614 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001615 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001616 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00001617 default: assert(0 && "Invalid FSEL condition"); abort();
1618 case ISD::SETULT:
1619 case ISD::SETLT:
1620 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1621 case ISD::SETUGE:
1622 case ISD::SETGE:
Nate Begemanc24d4842005-08-10 20:52:09 +00001623 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00001624 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1625 return Result;
1626 case ISD::SETUGT:
1627 case ISD::SETGT:
1628 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1629 case ISD::SETULE:
1630 case ISD::SETLE: {
Nate Begemanc24d4842005-08-10 20:52:09 +00001631 if (N.getOperand(0).getOpcode() == ISD::FNEG) {
1632 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Nate Begemana3fd4002005-07-19 16:51:05 +00001633 } else {
1634 Tmp2 = MakeReg(VT);
Nate Begemanc24d4842005-08-10 20:52:09 +00001635 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00001636 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1637 }
1638 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1639 return Result;
1640 }
1641 }
1642 } else {
1643 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanc24d4842005-08-10 20:52:09 +00001644 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
1645 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001646 Tmp3 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001647 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00001648 default: assert(0 && "Invalid FSEL condition"); abort();
1649 case ISD::SETULT:
1650 case ISD::SETLT:
1651 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1652 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1653 return Result;
1654 case ISD::SETUGE:
1655 case ISD::SETGE:
1656 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1657 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1658 return Result;
1659 case ISD::SETUGT:
1660 case ISD::SETGT:
1661 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1662 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1663 return Result;
1664 case ISD::SETULE:
1665 case ISD::SETLE:
1666 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1667 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1668 return Result;
1669 }
1670 }
1671 assert(0 && "Should never get here");
Nate Begemana3fd4002005-07-19 16:51:05 +00001672 }
1673
Nate Begeman5a014812005-08-14 01:17:16 +00001674 // If the False value only has one use, we can generate better code by
1675 // selecting it in the fallthrough basic block rather than here, which
1676 // increases register pressure.
Nate Begeman5a014812005-08-14 01:17:16 +00001677 unsigned TrueValue = SelectExpr(N.getOperand(2));
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001678 unsigned FalseValue;
1679
1680 // If the false value is simple enough, evaluate it inline in the false
1681 // block.
1682 if (isa<ConstantSDNode>(N.getOperand(3)) ||
1683 isa<ConstantFPSDNode>(N.getOperand(3)) ||
1684 isa<GlobalAddressSDNode>(N.getOperand(3)))
1685 FalseValue = 0;
1686 else
1687 FalseValue = SelectExpr(N.getOperand(3));
Nate Begemanc24d4842005-08-10 20:52:09 +00001688 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1689 Opc = getBCCForSetCC(CC);
1690
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001691 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00001692 // value and the MBB to hold the PHI instruction for this SetCC.
1693 MachineBasicBlock *thisMBB = BB;
1694 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1695 ilist<MachineBasicBlock>::iterator It = BB;
1696 ++It;
1697
1698 // thisMBB:
1699 // ...
1700 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001701 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00001702 // bCC copy1MBB
1703 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001704 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1705 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001706 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001707 MachineFunction *F = BB->getParent();
1708 F->getBasicBlockList().insert(It, copy0MBB);
1709 F->getBasicBlockList().insert(It, sinkMBB);
1710 // Update machine-CFG edges
1711 BB->addSuccessor(copy0MBB);
1712 BB->addSuccessor(sinkMBB);
1713
1714 // copy0MBB:
1715 // %FalseValue = ...
1716 // # fallthrough to sinkMBB
1717 BB = copy0MBB;
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001718
1719 // If the false value is simple enough, evaluate it here, to avoid it being
1720 // evaluated on the true edge.
1721 if (FalseValue == 0)
1722 FalseValue = SelectExpr(N.getOperand(3));
1723
Nate Begeman74747862005-03-29 22:24:51 +00001724 // Update machine-CFG edges
1725 BB->addSuccessor(sinkMBB);
1726
1727 // sinkMBB:
1728 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1729 // ...
1730 BB = sinkMBB;
1731 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1732 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001733 return Result;
1734 }
Nate Begemana9795f82005-03-24 04:41:43 +00001735
Chris Lattner0c09a412005-08-18 17:16:52 +00001736 case ISD::Constant: {
1737 assert(N.getValueType() == MVT::i32 &&
1738 "Only i32 constants are legal on this target!");
Nate Begeman58dfb082005-08-18 18:14:49 +00001739 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001740 if (isInt16(v)) {
1741 BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo16(v));
Chris Lattner0c09a412005-08-18 17:16:52 +00001742 } else {
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001743 unsigned Hi = Hi16(v);
1744 unsigned Lo = Lo16(v);
1745 if (Lo) {
1746 Tmp1 = MakeIntReg();
1747 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi);
1748 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo);
1749 } else {
1750 BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi);
1751 }
Nate Begemana9795f82005-03-24 04:41:43 +00001752 }
1753 return Result;
Chris Lattner0c09a412005-08-18 17:16:52 +00001754 }
Nate Begemana3fd4002005-07-19 16:51:05 +00001755
1756 case ISD::ConstantFP: {
1757 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
1758 Result = getConstDouble(CN->getValue(), Result);
1759 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001760 }
1761
Nate Begemana3fd4002005-07-19 16:51:05 +00001762 case ISD::FNEG:
1763 if (!NoExcessFPPrecision &&
1764 ISD::ADD == N.getOperand(0).getOpcode() &&
1765 N.getOperand(0).Val->hasOneUse() &&
1766 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1767 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
1768 ++FusedFP; // Statistic
1769 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1770 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1771 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1772 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1773 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1774 } else if (!NoExcessFPPrecision &&
1775 ISD::ADD == N.getOperand(0).getOpcode() &&
1776 N.getOperand(0).Val->hasOneUse() &&
1777 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1778 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
1779 ++FusedFP; // Statistic
1780 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1781 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1782 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1783 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1784 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1785 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
1786 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1787 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1788 } else {
1789 Tmp1 = SelectExpr(N.getOperand(0));
1790 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1791 }
1792 return Result;
1793
1794 case ISD::FABS:
1795 Tmp1 = SelectExpr(N.getOperand(0));
1796 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1797 return Result;
1798
Nate Begemanadeb43d2005-07-20 22:42:00 +00001799 case ISD::FSQRT:
1800 Tmp1 = SelectExpr(N.getOperand(0));
1801 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
1802 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1803 return Result;
1804
Nate Begemana3fd4002005-07-19 16:51:05 +00001805 case ISD::FP_ROUND:
1806 assert (DestType == MVT::f32 &&
1807 N.getOperand(0).getValueType() == MVT::f64 &&
1808 "only f64 to f32 conversion supported here");
1809 Tmp1 = SelectExpr(N.getOperand(0));
1810 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1811 return Result;
1812
1813 case ISD::FP_EXTEND:
1814 assert (DestType == MVT::f64 &&
1815 N.getOperand(0).getValueType() == MVT::f32 &&
1816 "only f32 to f64 conversion supported here");
1817 Tmp1 = SelectExpr(N.getOperand(0));
1818 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1819 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00001820 }
Nate Begemana9795f82005-03-24 04:41:43 +00001821 return 0;
1822}
1823
1824void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00001825 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00001826 unsigned opcode = N.getOpcode();
1827
1828 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1829 return; // Already selected.
1830
1831 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001832
Nate Begemana9795f82005-03-24 04:41:43 +00001833 switch (Node->getOpcode()) {
1834 default:
1835 Node->dump(); std::cerr << "\n";
1836 assert(0 && "Node not handled yet!");
1837 case ISD::EntryToken: return; // Noop
1838 case ISD::TokenFactor:
1839 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1840 Select(Node->getOperand(i));
1841 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001842 case ISD::CALLSEQ_START:
1843 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00001844 Select(N.getOperand(0));
1845 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001846 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00001847 PPC::ADJCALLSTACKUP;
1848 BuildMI(BB, Opc, 1).addImm(Tmp1);
1849 return;
1850 case ISD::BR: {
1851 MachineBasicBlock *Dest =
1852 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001853 Select(N.getOperand(0));
1854 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1855 return;
1856 }
Nate Begeman7cbd5252005-08-16 19:49:35 +00001857 case ISD::BR_CC:
1858 case ISD::BRTWOWAY_CC:
Nate Begemana9795f82005-03-24 04:41:43 +00001859 SelectBranchCC(N);
1860 return;
1861 case ISD::CopyToReg:
1862 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001863 Tmp1 = SelectExpr(N.getOperand(2));
1864 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001865
Nate Begemana9795f82005-03-24 04:41:43 +00001866 if (Tmp1 != Tmp2) {
Chris Lattnera8cd0152005-08-16 21:58:15 +00001867 if (N.getOperand(2).getValueType() == MVT::f64 ||
1868 N.getOperand(2).getValueType() == MVT::f32)
Nate Begemana9795f82005-03-24 04:41:43 +00001869 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1870 else
1871 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1872 }
1873 return;
1874 case ISD::ImplicitDef:
1875 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001876 BuildMI(BB, PPC::IMPLICIT_DEF, 0,
1877 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Nate Begemana9795f82005-03-24 04:41:43 +00001878 return;
1879 case ISD::RET:
1880 switch (N.getNumOperands()) {
1881 default:
1882 assert(0 && "Unknown return instruction!");
1883 case 3:
1884 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1885 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001886 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00001887 Select(N.getOperand(0));
1888 Tmp1 = SelectExpr(N.getOperand(1));
1889 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001890 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1891 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001892 break;
1893 case 2:
1894 Select(N.getOperand(0));
1895 Tmp1 = SelectExpr(N.getOperand(1));
1896 switch (N.getOperand(1).getValueType()) {
1897 default:
1898 assert(0 && "Unknown return type!");
1899 case MVT::f64:
1900 case MVT::f32:
1901 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1902 break;
1903 case MVT::i32:
1904 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1905 break;
1906 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001907 case 1:
1908 Select(N.getOperand(0));
1909 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001910 }
1911 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1912 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001913 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00001914 case ISD::STORE: {
1915 SDOperand Chain = N.getOperand(0);
1916 SDOperand Value = N.getOperand(1);
1917 SDOperand Address = N.getOperand(2);
1918 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00001919
Nate Begeman2497e632005-07-21 20:44:43 +00001920 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00001921
Nate Begeman2497e632005-07-21 20:44:43 +00001922 if (opcode == ISD::STORE) {
1923 switch(Value.getValueType()) {
1924 default: assert(0 && "unknown Type in store");
1925 case MVT::i32: Opc = PPC::STW; break;
1926 case MVT::f64: Opc = PPC::STFD; break;
1927 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001928 }
Nate Begeman2497e632005-07-21 20:44:43 +00001929 } else { //ISD::TRUNCSTORE
1930 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1931 default: assert(0 && "unknown Type in store");
1932 case MVT::i1:
1933 case MVT::i8: Opc = PPC::STB; break;
1934 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001935 }
Nate Begemana9795f82005-03-24 04:41:43 +00001936 }
Nate Begeman2497e632005-07-21 20:44:43 +00001937
1938 if(Address.getOpcode() == ISD::FrameIndex) {
1939 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1940 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00001941 } else {
1942 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001943 switch(SelectAddr(Address, Tmp2, offset)) {
1944 default: assert(0 && "Unhandled return value from SelectAddr");
1945 case 0: // imm offset, no frame, no index
1946 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
1947 break;
1948 case 1: // imm offset + frame index
1949 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
1950 break;
1951 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00001952 Opc = IndexedOpForOp(Opc);
1953 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001954 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001955 case 3: {
1956 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1957 GlobalValue *GV = GN->getGlobal();
1958 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
1959 }
Nate Begeman2497e632005-07-21 20:44:43 +00001960 }
1961 }
1962 return;
1963 }
Nate Begemana9795f82005-03-24 04:41:43 +00001964 case ISD::EXTLOAD:
1965 case ISD::SEXTLOAD:
1966 case ISD::ZEXTLOAD:
1967 case ISD::LOAD:
1968 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001969 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00001970 case ISD::CALL:
1971 case ISD::DYNAMIC_STACKALLOC:
1972 ExprMap.erase(N);
1973 SelectExpr(N);
1974 return;
1975 }
1976 assert(0 && "Should not be reached!");
1977}
1978
1979
1980/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1981/// into a machine code representation using pattern matching and a machine
1982/// description file.
1983///
1984FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001985 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001986}
1987