blob: 1ffa6dcb0e5ded49391d5b4bac51f87d346e5e14 [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/// getCROpForOp - Return the condition register opcode (or inverted opcode)
250/// associated with the SelectionDAG opcode.
251static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
252 switch (Opcode) {
253 default: assert(0 && "Unknown opcode!"); abort();
254 case ISD::AND:
255 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
256 if (!Inv1 && !Inv2) return PPC::CRAND;
257 if (Inv1 ^ Inv2) return PPC::CRANDC;
258 case ISD::OR:
259 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
260 if (!Inv1 && !Inv2) return PPC::CROR;
261 if (Inv1 ^ Inv2) return PPC::CRORC;
262 }
263 return 0;
264}
265
266/// getCRIdxForSetCC - Return the index of the condition register field
267/// associated with the SetCC condition, and whether or not the field is
268/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000269static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
270 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000271 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000272 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000273 case ISD::SETLT: Inv = false; return 0;
274 case ISD::SETUGE:
275 case ISD::SETGE: Inv = true; return 0;
276 case ISD::SETUGT:
277 case ISD::SETGT: Inv = false; return 1;
278 case ISD::SETULE:
279 case ISD::SETLE: Inv = true; return 1;
280 case ISD::SETEQ: Inv = false; return 2;
281 case ISD::SETNE: Inv = true; return 2;
282 }
283 return 0;
284}
285
Nate Begeman04730362005-04-01 04:45:11 +0000286/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
287/// and store immediate instructions.
288static unsigned IndexedOpForOp(unsigned Opcode) {
289 switch(Opcode) {
290 default: assert(0 && "Unknown opcode!"); abort();
291 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
292 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
293 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
294 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
295 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
296 case PPC::LFD: return PPC::LFDX;
297 }
298 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000299}
Nate Begeman815d6da2005-04-06 00:25:27 +0000300
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000301// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000302// a multiply.
303struct ms {
304 int m; // magic number
305 int s; // shift amount
306};
307
308struct mu {
309 unsigned int m; // magic number
310 int a; // add indicator
311 int s; // shift amount
312};
313
314/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000315/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000316/// or -1.
317static struct ms magic(int d) {
318 int p;
319 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000320 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000321 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000322
Nate Begeman815d6da2005-04-06 00:25:27 +0000323 ad = abs(d);
324 t = two31 + ((unsigned int)d >> 31);
325 anc = t - 1 - t%ad; // absolute value of nc
326 p = 31; // initialize p
327 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
328 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
329 q2 = two31/ad; // initialize q2 = 2p/abs(d)
330 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
331 do {
332 p = p + 1;
333 q1 = 2*q1; // update q1 = 2p/abs(nc)
334 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
335 if (r1 >= anc) { // must be unsigned comparison
336 q1 = q1 + 1;
337 r1 = r1 - anc;
338 }
339 q2 = 2*q2; // update q2 = 2p/abs(d)
340 r2 = 2*r2; // update r2 = rem(2p/abs(d))
341 if (r2 >= ad) { // must be unsigned comparison
342 q2 = q2 + 1;
343 r2 = r2 - ad;
344 }
345 delta = ad - r2;
346 } while (q1 < delta || (q1 == delta && r1 == 0));
347
348 mag.m = q2 + 1;
349 if (d < 0) mag.m = -mag.m; // resulting magic number
350 mag.s = p - 32; // resulting shift
351 return mag;
352}
353
354/// magicu - calculate the magic numbers required to codegen an integer udiv as
355/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
356static struct mu magicu(unsigned d)
357{
358 int p;
359 unsigned int nc, delta, q1, r1, q2, r2;
360 struct mu magu;
361 magu.a = 0; // initialize "add" indicator
362 nc = - 1 - (-d)%d;
363 p = 31; // initialize p
364 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
365 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
366 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
367 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
368 do {
369 p = p + 1;
370 if (r1 >= nc - r1 ) {
371 q1 = 2*q1 + 1; // update q1
372 r1 = 2*r1 - nc; // update r1
373 }
374 else {
375 q1 = 2*q1; // update q1
376 r1 = 2*r1; // update r1
377 }
378 if (r2 + 1 >= d - r2) {
379 if (q2 >= 0x7FFFFFFF) magu.a = 1;
380 q2 = 2*q2 + 1; // update q2
381 r2 = 2*r2 + 1 - d; // update r2
382 }
383 else {
384 if (q2 >= 0x80000000) magu.a = 1;
385 q2 = 2*q2; // update q2
386 r2 = 2*r2 + 1; // update r2
387 }
388 delta = d - 1 - r2;
389 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
390 magu.m = q2 + 1; // resulting magic number
391 magu.s = p - 32; // resulting shift
392 return magu;
393}
394}
395
396/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
397/// return a DAG expression to select that will generate the same value by
398/// multiplying by a magic number. See:
399/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
400SDOperand ISel::BuildSDIVSequence(SDOperand N) {
401 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
402 ms magics = magic(d);
403 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000404 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000405 ISelDAG->getConstant(magics.m, MVT::i32));
406 // If d > 0 and m < 0, add the numerator
407 if (d > 0 && magics.m < 0)
408 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
409 // If d < 0 and m > 0, subtract the numerator.
410 if (d < 0 && magics.m > 0)
411 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
412 // Shift right algebraic if shift value is nonzero
413 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000414 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000415 ISelDAG->getConstant(magics.s, MVT::i32));
416 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000417 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000418 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000419 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000420}
421
422/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
423/// return a DAG expression to select that will generate the same value by
424/// multiplying by a magic number. See:
425/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
426SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000427 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000428 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
429 mu magics = magicu(d);
430 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000431 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000432 ISelDAG->getConstant(magics.m, MVT::i32));
433 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000434 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000435 ISelDAG->getConstant(magics.s, MVT::i32));
436 } else {
437 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000438 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000439 ISelDAG->getConstant(1, MVT::i32));
440 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000441 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000442 ISelDAG->getConstant(magics.s-1, MVT::i32));
443 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000444 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000445}
446
Nate Begemanc7b09f12005-03-25 08:34:25 +0000447/// getGlobalBaseReg - Output the instructions required to put the
448/// base address to use for accessing globals into a register.
449///
450unsigned ISel::getGlobalBaseReg() {
451 if (!GlobalBaseInitialized) {
452 // Insert the set of GlobalBaseReg into the first MBB of the function
453 MachineBasicBlock &FirstMBB = BB->getParent()->front();
454 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000455 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000456 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
457 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
458 GlobalBaseInitialized = true;
459 }
460 return GlobalBaseReg;
461}
462
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000463/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000464/// Constant Pool. Optionally takes a register in which to load the value.
465unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000466 unsigned Tmp1 = MakeIntReg();
467 if (0 == Result) Result = MakeFPReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000468 MachineConstantPool *CP = BB->getParent()->getConstantPool();
469 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
470 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000471 if (PICEnabled)
472 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
473 .addConstantPoolIndex(CPI);
474 else
475 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000476 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
477 return Result;
478}
479
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000480/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000481/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000482void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
483 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000484 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000485 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000486 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000487 bool GPOpt =
488 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
489 BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000490 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000491 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000492 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
493 .addImm(31).addImm(31);
494 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
495 } else {
496 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
497 .addImm(31).addImm(31);
498 }
499}
500
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000501/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000502/// the rotate left word immediate then mask insert (rlwimi) instruction.
503/// Returns true on success, false if the caller still needs to select OR.
504///
505/// Patterns matched:
506/// 1. or shl, and 5. or and, and
507/// 2. or and, shl 6. or shl, shr
508/// 3. or shr, and 7. or shr, shl
509/// 4. or and, shr
510bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000511 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000512 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000513 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000514
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000515 SDOperand Op0 = OR.getOperand(0);
516 SDOperand Op1 = OR.getOperand(1);
517
518 unsigned Op0Opc = Op0.getOpcode();
519 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000520
Nate Begeman7ddecb42005-04-06 23:51:40 +0000521 // Verify that we have the correct opcodes
522 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
523 return false;
524 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
525 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000526
Nate Begeman7ddecb42005-04-06 23:51:40 +0000527 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +0000528 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000529 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000530 case ISD::SHL: TgtMask <<= Value; break;
531 case ISD::SRL: TgtMask >>= Value; break;
532 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000533 }
534 } else {
535 return false;
536 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000537
Nate Begeman7ddecb42005-04-06 23:51:40 +0000538 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +0000539 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000540 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000541 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000542 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000543 InsMask <<= Amount;
544 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000545 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000546 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000547 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000549 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000550 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000551 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000552 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000553 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000554 break;
555 }
556 } else {
557 return false;
558 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000559
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000560 unsigned Tmp3 = 0;
561
562 // If both of the inputs are ANDs and one of them has a logical shift by
563 // constant as its input, make that the inserted value so that we can combine
564 // the shift into the rotate part of the rlwimi instruction
565 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000566 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000567 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000568 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000569 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000570 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000571 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
572 }
573 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
574 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000575 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000576 std::swap(Op0, Op1);
577 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +0000578 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000579 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000580 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
581 }
582 }
583 }
584
Nate Begeman7ddecb42005-04-06 23:51:40 +0000585 // Verify that the Target mask and Insert mask together form a full word mask
586 // and that the Insert mask is a run of set bits (which implies both are runs
587 // of set bits). Given that, Select the arguments and generate the rlwimi
588 // instruction.
589 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +0000590 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000591 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000592 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000593 // Check for rotlwi / rotrwi here, a special case of bitfield insert
594 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000595 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +0000596 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000597 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
598 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
599 .addImm(0).addImm(31);
600 return true;
601 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000602 if (Op0Opc == ISD::AND && fullMask)
603 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000604 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000605 Tmp1 = SelectExpr(Op0);
606 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000607 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
608 .addImm(Amount).addImm(MB).addImm(ME);
609 return true;
610 }
611 return false;
612}
613
Nate Begeman3664cef2005-04-13 22:14:14 +0000614/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
615/// low six bits. If the shift amount is an ISD::AND node with a mask that is
616/// wider than the implicit mask, then we can get rid of the AND and let the
617/// shift do the mask.
618unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Jim Laskey191cf942005-08-11 21:59:23 +0000619 unsigned C;
620 if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63)
Nate Begeman3664cef2005-04-13 22:14:14 +0000621 return SelectExpr(N.getOperand(0));
622 else
623 return SelectExpr(N);
624}
625
Nate Begemanc24d4842005-08-10 20:52:09 +0000626unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000627 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000628 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000629 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +0000630 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000631
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000632 // Allocate a condition register for this expression
633 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000634
Nate Begemanc24d4842005-08-10 20:52:09 +0000635 // Use U to determine whether the SETCC immediate range is signed or not.
636 bool U = ISD::isUnsignedIntSetCC(CC);
637 if (isIntImmediate(RHS, Tmp2) &&
638 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
639 Tmp2 = Lo16(Tmp2);
640 // For comparisons against zero, we can implicity set CR0 if a recording
641 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
642 // operand zero of the SetCC node is available.
643 if (Tmp2 == 0 &&
644 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
645 RecordSuccess = false;
646 Tmp1 = SelectExpr(LHS, true);
647 if (RecordSuccess) {
648 ++Recorded;
649 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
650 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000651 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000652 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000653 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000654 // If we could not implicitly set CR0, then emit a compare immediate
655 // instead.
656 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
657 if (U)
658 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
659 else
660 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000661 } else {
Nate Begemanc24d4842005-08-10 20:52:09 +0000662 bool IsInteger = MVT::isInteger(LHS.getValueType());
663 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
664 Tmp1 = SelectExpr(LHS);
665 Tmp2 = SelectExpr(RHS);
666 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000667 }
668 return Result;
669}
670
Nate Begemand3ded2d2005-08-08 22:22:56 +0000671/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000672unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000673{
Nate Begeman96fc6812005-03-31 02:05:53 +0000674 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000675 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000676 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +0000677 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +0000678 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000679 if (isFrame) {
680 ++FrameOff;
681 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
682 return 1;
683 } else {
684 Reg = SelectExpr(N.getOperand(0));
685 return 0;
686 }
687 } else {
688 Reg = SelectExpr(N.getOperand(0));
689 offset = SelectExpr(N.getOperand(1));
690 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000691 }
Nate Begeman04730362005-04-01 04:45:11 +0000692 }
Nate Begemand3ded2d2005-08-08 22:22:56 +0000693 // Now check if we're dealing with a global, and whether or not we should emit
694 // an optimized load or store for statics.
695 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
696 GlobalValue *GV = GN->getGlobal();
697 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000698 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +0000699 if (PICEnabled)
700 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
701 .addGlobalAddress(GV);
702 else
703 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
704 Reg = GlobalHi;
705 offset = 0;
706 return 3;
707 }
708 }
Nate Begemana9795f82005-03-24 04:41:43 +0000709 Reg = SelectExpr(N);
710 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000711 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000712}
713
714void ISel::SelectBranchCC(SDOperand N)
715{
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000716 MachineBasicBlock *Dest =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000717 cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000718
Nate Begemana9795f82005-03-24 04:41:43 +0000719 Select(N.getOperand(0)); //chain
Nate Begeman7cbd5252005-08-16 19:49:35 +0000720 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get();
721 unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC);
Nate Begemanc24d4842005-08-10 20:52:09 +0000722 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000723
Nate Begeman439009c2005-06-15 18:22:43 +0000724 // Iterate to the next basic block
725 ilist<MachineBasicBlock>::iterator It = BB;
726 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000727
728 // If this is a two way branch, then grab the fallthrough basic block argument
729 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
730 // if necessary by the branch selection pass. Otherwise, emit a standard
731 // conditional branch.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000732 if (N.getOpcode() == ISD::BRTWOWAY_CC) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000733 MachineBasicBlock *Fallthrough =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000734 cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000735 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000736 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +0000737 .addMBB(Dest).addMBB(Fallthrough);
738 if (Fallthrough != It)
739 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
740 } else {
741 if (Fallthrough != It) {
742 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000743 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +0000744 .addMBB(Fallthrough).addMBB(Dest);
745 }
746 }
747 } else {
Nate Begeman439009c2005-06-15 18:22:43 +0000748 // If the fallthrough path is off the end of the function, which would be
749 // undefined behavior, set it to be the same as the current block because
750 // we have nothing better to set it to, and leaving it alone will cause the
751 // PowerPC Branch Selection pass to crash.
752 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000753 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +0000754 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000755 }
Nate Begemana9795f82005-03-24 04:41:43 +0000756 return;
757}
758
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000759// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +0000760bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000761 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000762 bool IsArithmetic, bool Negate) {
763 // check constant
764 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
765 // exit if not a constant
766 if (!CN) return false;
767 // extract immediate
Chris Lattner6d9aed42005-08-17 01:25:14 +0000768 unsigned C = (unsigned)CN->getValue();
Chris Lattnerb4138c42005-08-10 18:11:33 +0000769 // negate if required (ISD::SUB)
770 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000771 // get the hi and lo portions of constant
772 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
773 unsigned Lo = Lo16(C);
774 // assume no intermediate result from lo instruction (same as final result)
775 unsigned Tmp = Result;
776 // check if two instructions are needed
777 if (Hi && Lo) {
778 // exit if usage indicates it would be better to load immediate into a
779 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +0000780 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000781 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +0000782 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000783 }
784 // get first operand
785 unsigned Opr0 = SelectExpr(N.getOperand(0));
786 // is a lo instruction needed
787 if (Lo) {
Chris Lattner6d9aed42005-08-17 01:25:14 +0000788 // generate instruction for lo portion
789 BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000790 // need to switch out first operand for hi instruction
791 Opr0 = Tmp;
792 }
Chris Lattner6d9aed42005-08-17 01:25:14 +0000793 // is a hi instruction needed
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000794 if (Hi) {
795 // generate instruction for hi portion
Chris Lattner6d9aed42005-08-17 01:25:14 +0000796 BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000797 }
798 return true;
799}
800
Nate Begemanc7bd4822005-04-11 06:34:10 +0000801unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +0000802 unsigned Result;
803 unsigned Tmp1, Tmp2, Tmp3;
804 unsigned Opc = 0;
805 unsigned opcode = N.getOpcode();
806
807 SDNode *Node = N.Val;
808 MVT::ValueType DestType = N.getValueType();
809
Chris Lattnera8cd0152005-08-16 21:58:15 +0000810 if (Node->getOpcode() == ISD::CopyFromReg) {
811 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana43b1762005-06-14 03:55:23 +0000812 // Just use the specified register as our input.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000813 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1)
814 return Reg;
815 }
Nate Begemana43b1762005-06-14 03:55:23 +0000816
Nate Begemana9795f82005-03-24 04:41:43 +0000817 unsigned &Reg = ExprMap[N];
818 if (Reg) return Reg;
819
Nate Begeman27eeb002005-04-02 05:59:34 +0000820 switch (N.getOpcode()) {
821 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000822 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000823 MakeReg(N.getValueType()) : 1;
824 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000825 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +0000826 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000827 // If this is a call instruction, make sure to prepare ALL of the result
828 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000829 if (Node->getNumValues() == 1)
830 Reg = Result = 1; // Void call, just a chain.
831 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000832 Result = MakeReg(Node->getValueType(0));
833 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000834 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000835 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000836 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000837 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000838 break;
839 case ISD::ADD_PARTS:
840 case ISD::SUB_PARTS:
841 case ISD::SHL_PARTS:
842 case ISD::SRL_PARTS:
843 case ISD::SRA_PARTS:
844 Result = MakeReg(Node->getValueType(0));
845 ExprMap[N.getValue(0)] = Result;
846 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
847 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
848 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000849 }
850
Nate Begemana9795f82005-03-24 04:41:43 +0000851 switch (opcode) {
852 default:
Nate Begeman5a014812005-08-14 01:17:16 +0000853 Node->dump(); std::cerr << '\n';
854 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000855 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000856 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
857 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000858 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000859 // Generate both result values. FIXME: Need a better commment here?
860 if (Result != 1)
861 ExprMap[N.getValue(1)] = 1;
862 else
863 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
864
865 // FIXME: We are currently ignoring the requested alignment for handling
866 // greater than the stack alignment. This will need to be revisited at some
867 // point. Align = N.getOperand(2);
868 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
869 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
870 std::cerr << "Cannot allocate stack object with greater alignment than"
871 << " the stack alignment yet!";
872 abort();
873 }
874 Select(N.getOperand(0));
875 Tmp1 = SelectExpr(N.getOperand(1));
876 // Subtract size from stack pointer, thereby allocating some space.
877 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
878 // Put a pointer to the space into the result register by copying the SP
879 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
880 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000881
882 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000883 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
Chris Lattner54abfc52005-08-11 17:15:31 +0000884 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000885 if (PICEnabled)
886 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
887 .addConstantPoolIndex(Tmp1);
888 else
889 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000890 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
891 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000892
893 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000894 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000895 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000896 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000897
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000898 case ISD::GlobalAddress: {
899 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +0000900 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000901 if (PICEnabled)
902 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
903 .addGlobalAddress(GV);
904 else
Chris Lattner4015ea82005-07-28 04:42:11 +0000905 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000906 if (GV->hasWeakLinkage() || GV->isExternal()) {
907 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
908 } else {
909 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
910 }
911 return Result;
912 }
913
Nate Begeman5e966612005-03-24 06:28:42 +0000914 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000915 case ISD::EXTLOAD:
916 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000917 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000918 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000919 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +0000920 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000921
Nate Begeman5e966612005-03-24 06:28:42 +0000922 // Make sure we generate both values.
923 if (Result != 1)
924 ExprMap[N.getValue(1)] = 1; // Generate the token
925 else
926 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
927
928 SDOperand Chain = N.getOperand(0);
929 SDOperand Address = N.getOperand(1);
930 Select(Chain);
931
Nate Begeman9db505c2005-03-28 19:36:43 +0000932 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000933 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000934 case MVT::i1: Opc = PPC::LBZ; break;
935 case MVT::i8: Opc = PPC::LBZ; break;
936 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
937 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000938 case MVT::f32: Opc = PPC::LFS; break;
939 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000940 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000941
Nate Begeman74d73452005-03-31 00:15:26 +0000942 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000943 Tmp1 = MakeIntReg();
Nate Begeman74d73452005-03-31 00:15:26 +0000944 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +0000945 if (PICEnabled)
946 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
947 .addConstantPoolIndex(CPI);
948 else
949 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +0000950 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +0000951 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000952 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
953 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000954 } else {
955 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000956 switch(SelectAddr(Address, Tmp1, offset)) {
957 default: assert(0 && "Unhandled return value from SelectAddr");
958 case 0: // imm offset, no frame, no index
959 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
960 break;
961 case 1: // imm offset + frame index
962 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
963 break;
964 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +0000965 Opc = IndexedOpForOp(Opc);
966 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000967 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +0000968 case 3: {
969 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
970 GlobalValue *GV = GN->getGlobal();
971 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
972 }
Nate Begeman04730362005-04-01 04:45:11 +0000973 }
Nate Begeman5e966612005-03-24 06:28:42 +0000974 }
975 return Result;
976 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000977
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000978 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000979 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000980 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000981 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000982 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
983 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
984 };
985 static const unsigned FPR[] = {
986 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
987 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
988 };
989
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000990 // Lower the chain for this call.
991 Select(N.getOperand(0));
992 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000993
Nate Begemand860aa62005-04-04 22:17:48 +0000994 MachineInstr *CallMI;
995 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000996 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +0000997 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000998 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +0000999 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001000 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001001 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001002 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001003 true);
1004 } else {
1005 Tmp1 = SelectExpr(N.getOperand(1));
1006 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1007 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1008 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1009 .addReg(PPC::R12);
1010 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001011
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001012 // Load the register args to virtual regs
1013 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001014 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001015 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1016
1017 // Copy the virtual registers into the appropriate argument register
1018 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1019 switch(N.getOperand(i+2).getValueType()) {
1020 default: Node->dump(); assert(0 && "Unknown value type for call");
1021 case MVT::i1:
1022 case MVT::i8:
1023 case MVT::i16:
1024 case MVT::i32:
1025 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001026 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001027 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001028 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1029 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001030 ++GPR_idx;
1031 break;
1032 case MVT::f64:
1033 case MVT::f32:
1034 assert(FPR_idx < 13 && "Too many fp args");
1035 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001036 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001037 ++FPR_idx;
1038 break;
1039 }
1040 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001041
Nate Begemand860aa62005-04-04 22:17:48 +00001042 // Put the call instruction in the correct place in the MachineBasicBlock
1043 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001044
1045 switch (Node->getValueType(0)) {
1046 default: assert(0 && "Unknown value type for call result!");
1047 case MVT::Other: return 1;
1048 case MVT::i1:
1049 case MVT::i8:
1050 case MVT::i16:
1051 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001052 if (Node->getValueType(1) == MVT::i32) {
1053 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1054 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1055 } else {
1056 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1057 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001058 break;
1059 case MVT::f32:
1060 case MVT::f64:
1061 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1062 break;
1063 }
1064 return Result+N.ResNo;
1065 }
Nate Begemana9795f82005-03-24 04:41:43 +00001066
1067 case ISD::SIGN_EXTEND:
1068 case ISD::SIGN_EXTEND_INREG:
1069 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001070 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001071 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001072 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001073 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001074 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001075 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001076 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001077 break;
Nate Begeman74747862005-03-29 22:24:51 +00001078 case MVT::i1:
1079 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1080 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001081 }
Nate Begemana9795f82005-03-24 04:41:43 +00001082 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001083
Nate Begemana9795f82005-03-24 04:41:43 +00001084 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001085 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001086 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001087 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Chris Lattnera8cd0152005-08-16 21:58:15 +00001088 Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001089 if (MVT::isInteger(DestType))
1090 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1091 else
1092 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001093 return Result;
1094
1095 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001096 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001097 unsigned SH, MB, ME;
1098 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1099 isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) {
1100 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1101 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1102 .addImm(MB).addImm(ME);
1103 return Result;
1104 }
1105 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001106 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001107 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001108 .addImm(31-Tmp2);
1109 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001110 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001111 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001112 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1113 }
1114 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001115
Nate Begeman5e966612005-03-24 06:28:42 +00001116 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001117 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001118 unsigned SH, MB, ME;
1119 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1120 isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) {
1121 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1122 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1123 .addImm(MB).addImm(ME);
1124 return Result;
1125 }
1126 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001127 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001128 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001129 .addImm(Tmp2).addImm(31);
1130 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001131 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001132 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001133 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1134 }
1135 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001136
Nate Begeman5e966612005-03-24 06:28:42 +00001137 case ISD::SRA:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001138 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001139 unsigned SH, MB, ME;
1140 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1141 isRotateAndMask(ISD::SRA, Tmp2, Tmp3, true, SH, MB, ME)) {
1142 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1143 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1144 .addImm(MB).addImm(ME);
1145 return Result;
1146 }
1147 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001148 Tmp2 &= 0x1F;
Nate Begeman5e966612005-03-24 06:28:42 +00001149 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1150 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001151 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001152 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001153 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1154 }
1155 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001156
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001157 case ISD::CTLZ:
1158 Tmp1 = SelectExpr(N.getOperand(0));
1159 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1160 return Result;
1161
Nate Begemana9795f82005-03-24 04:41:43 +00001162 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001163 if (!MVT::isInteger(DestType)) {
1164 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1165 N.getOperand(0).Val->hasOneUse()) {
1166 ++FusedFP; // Statistic
1167 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1168 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1169 Tmp3 = SelectExpr(N.getOperand(1));
1170 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1171 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1172 return Result;
1173 }
1174 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1175 N.getOperand(1).Val->hasOneUse()) {
1176 ++FusedFP; // Statistic
1177 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1178 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1179 Tmp3 = SelectExpr(N.getOperand(0));
1180 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1181 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1182 return Result;
1183 }
1184 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1185 Tmp1 = SelectExpr(N.getOperand(0));
1186 Tmp2 = SelectExpr(N.getOperand(1));
1187 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1188 return Result;
1189 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001190 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
1191 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001192 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001193 Tmp2 = SelectExpr(N.getOperand(1));
1194 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001195 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001196
Nate Begemana9795f82005-03-24 04:41:43 +00001197 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001198 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001199 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1200 unsigned SH, MB, ME;
1201 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1202 unsigned OprOpc;
1203 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1204 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001205 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001206 } else {
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 isRunOfOnes(Tmp2, MB, ME);
1209 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001210 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001211 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1212 .addImm(MB).addImm(ME);
1213 RecordSuccess = true;
1214 return Result;
1215 } else if (isUInt16(Tmp2)) {
1216 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001217 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001218 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001219 RecordSuccess = true;
1220 return Result;
1221 } else if (isUInt16(Tmp2)) {
1222 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001223 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001224 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001225 RecordSuccess = true;
1226 return Result;
1227 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001228 }
Jim Laskey847c3a92005-08-12 23:38:02 +00001229 if (isOprNot(N.getOperand(1))) {
1230 Tmp1 = SelectExpr(N.getOperand(0));
1231 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1232 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1233 RecordSuccess = false;
1234 return Result;
1235 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001236 if (isOprNot(N.getOperand(0))) {
Jim Laskey847c3a92005-08-12 23:38:02 +00001237 Tmp1 = SelectExpr(N.getOperand(1));
1238 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1239 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001240 RecordSuccess = false;
1241 return Result;
1242 }
1243 // emit a regular and
1244 Tmp1 = SelectExpr(N.getOperand(0));
1245 Tmp2 = SelectExpr(N.getOperand(1));
1246 Opc = Recording ? PPC::ANDo : PPC::AND;
1247 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001248 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001249 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001250
Nate Begemana9795f82005-03-24 04:41:43 +00001251 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001252 if (SelectBitfieldInsert(N, Result))
1253 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001254 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1255 return Result;
Jim Laskey847c3a92005-08-12 23:38:02 +00001256 if (isOprNot(N.getOperand(1))) {
1257 Tmp1 = SelectExpr(N.getOperand(0));
1258 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1259 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1260 RecordSuccess = false;
1261 return Result;
1262 }
1263 if (isOprNot(N.getOperand(0))) {
1264 Tmp1 = SelectExpr(N.getOperand(1));
1265 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1266 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1267 RecordSuccess = false;
1268 return Result;
1269 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001270 // emit regular or
1271 Tmp1 = SelectExpr(N.getOperand(0));
1272 Tmp2 = SelectExpr(N.getOperand(1));
1273 Opc = Recording ? PPC::ORo : PPC::OR;
1274 RecordSuccess = true;
1275 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001276 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001277
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001278 case ISD::XOR: {
1279 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001280 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001281 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1282 Tmp2 = SelectExpr(N.getOperand(1));
1283 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1284 return Result;
1285 }
Chris Lattner837a5212005-04-21 21:09:11 +00001286 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001287 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001288 switch(N.getOperand(0).getOpcode()) {
1289 case ISD::OR:
1290 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1291 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1292 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1293 break;
1294 case ISD::AND:
1295 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1296 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1297 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1298 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001299 case ISD::XOR:
1300 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1301 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1302 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1303 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001304 default:
1305 Tmp1 = SelectExpr(N.getOperand(0));
1306 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1307 break;
1308 }
1309 return Result;
1310 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001311 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1312 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001313 // emit regular xor
1314 Tmp1 = SelectExpr(N.getOperand(0));
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001317 return Result;
1318 }
1319
Chris Lattner5b909172005-08-08 21:30:29 +00001320 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001321 if (!MVT::isInteger(DestType)) {
1322 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1323 N.getOperand(0).Val->hasOneUse()) {
1324 ++FusedFP; // Statistic
1325 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1326 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1327 Tmp3 = SelectExpr(N.getOperand(1));
1328 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1329 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1330 return Result;
1331 }
1332 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1333 N.getOperand(1).Val->hasOneUse()) {
1334 ++FusedFP; // Statistic
1335 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1336 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1337 Tmp3 = SelectExpr(N.getOperand(0));
1338 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1339 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1340 return Result;
1341 }
1342 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1343 Tmp1 = SelectExpr(N.getOperand(0));
1344 Tmp2 = SelectExpr(N.getOperand(1));
1345 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1346 return Result;
1347 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001348 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001349 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001350 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001351 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001352 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001353 }
1354 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001355 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001356 Tmp1 = SelectExpr(N.getOperand(0));
1357 Tmp2 = SelectExpr(N.getOperand(1));
1358 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001359 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001360
Nate Begeman5e966612005-03-24 06:28:42 +00001361 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001362 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001363 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001364 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001365 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001366 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001367 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001368 switch (DestType) {
1369 default: assert(0 && "Unknown type to ISD::MUL"); break;
1370 case MVT::i32: Opc = PPC::MULLW; break;
1371 case MVT::f32: Opc = PPC::FMULS; break;
1372 case MVT::f64: Opc = PPC::FMUL; break;
1373 }
1374 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001375 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001376 return Result;
1377
Nate Begeman815d6da2005-04-06 00:25:27 +00001378 case ISD::MULHS:
1379 case ISD::MULHU:
1380 Tmp1 = SelectExpr(N.getOperand(0));
1381 Tmp2 = SelectExpr(N.getOperand(1));
1382 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1383 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1384 return Result;
1385
Nate Begemanf3d08f32005-03-29 00:03:27 +00001386 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001387 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001388 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1389 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001390 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001391 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001392 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1393 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001394 return Result;
1395 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1396 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001397 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001398 Tmp1 = MakeIntReg();
1399 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001400 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1401 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1402 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1403 return Result;
Nate Begeman9f833d32005-04-12 00:10:02 +00001404 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001405 }
1406 // fall thru
1407 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001408 // If this is a divide by constant, we can emit code using some magic
1409 // constants to implement it as a multiply instead.
Chris Lattner59b21c22005-08-09 18:29:55 +00001410 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001411 if (opcode == ISD::SDIV) {
1412 if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) {
1413 ExprMap.erase(N);
1414 return SelectExpr(BuildSDIVSequence(N));
1415 }
1416 } else {
1417 if ((signed)Tmp3 > 1) {
1418 ExprMap.erase(N);
1419 return SelectExpr(BuildUDIVSequence(N));
1420 }
1421 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001422 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001423 Tmp1 = SelectExpr(N.getOperand(0));
1424 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001425 switch (DestType) {
1426 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1427 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1428 case MVT::f32: Opc = PPC::FDIVS; break;
1429 case MVT::f64: Opc = PPC::FDIV; break;
1430 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001431 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1432 return Result;
1433
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001434 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001435 case ISD::SUB_PARTS: {
1436 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1437 "Not an i64 add/sub!");
Nate Begeman456f1e82005-08-17 00:20:08 +00001438 unsigned Tmp4 = 0;
1439 bool ME = isIntImmediate(N.getOperand(3),Tmp3) && ((signed)Tmp3 == -1);
1440 bool ZE = isIntImmediate(N.getOperand(3),Tmp3) && (Tmp3 == 0);
1441 bool IM = isIntImmediate(N.getOperand(2),Tmp3) && ((signed)Tmp3 >= -32768 ||
1442 (signed)Tmp3 < 32768);
1443 Tmp1 = SelectExpr(N.getOperand(0));
1444 Tmp2 = SelectExpr(N.getOperand(1));
1445 if (!IM || N.getOpcode() == ISD::SUB_PARTS)
1446 Tmp3 = SelectExpr(N.getOperand(2));
1447 if ((!ME && !ZE) || N.getOpcode() == ISD::SUB_PARTS)
1448 Tmp4 = SelectExpr(N.getOperand(3));
1449
Nate Begemanca12a2b2005-03-28 22:28:37 +00001450 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001451 // Codegen the low 32 bits of the add. Interestingly, there is no shifted
1452 // form of add immediate carrying.
1453 if (IM)
1454 BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3);
1455 else
1456 BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3);
1457 // Codegen the high 32 bits, adding zero, minus one, or the full value
1458 // along with the carry flag produced by addc/addic to tmp2.
1459 if (ZE)
1460 BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2);
1461 else if (ME)
1462 BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2);
1463 else
1464 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001465 } else {
Nate Begeman456f1e82005-08-17 00:20:08 +00001466 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1);
1467 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2);
Nate Begeman27eeb002005-04-02 05:59:34 +00001468 }
1469 return Result+N.ResNo;
1470 }
1471
1472 case ISD::SHL_PARTS:
1473 case ISD::SRA_PARTS:
1474 case ISD::SRL_PARTS: {
1475 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1476 "Not an i64 shift!");
1477 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1478 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001479 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
Chris Lattner54abfc52005-08-11 17:15:31 +00001480 Tmp1 = MakeIntReg();
1481 Tmp2 = MakeIntReg();
1482 Tmp3 = MakeIntReg();
1483 unsigned Tmp4 = MakeIntReg();
1484 unsigned Tmp5 = MakeIntReg();
1485 unsigned Tmp6 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001486 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1487 if (ISD::SHL_PARTS == opcode) {
1488 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1489 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1490 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1491 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001492 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001493 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1494 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1495 } else if (ISD::SRL_PARTS == opcode) {
1496 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1497 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1498 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1499 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1500 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1501 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1502 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1503 } else {
1504 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1505 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1506 MachineBasicBlock *OldMBB = BB;
1507 MachineFunction *F = BB->getParent();
1508 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1509 F->getBasicBlockList().insert(It, TmpMBB);
1510 F->getBasicBlockList().insert(It, PhiMBB);
1511 BB->addSuccessor(TmpMBB);
1512 BB->addSuccessor(PhiMBB);
1513 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1514 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1515 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1516 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1517 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1518 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1519 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1520 // Select correct least significant half if the shift amount > 32
1521 BB = TmpMBB;
Chris Lattner54abfc52005-08-11 17:15:31 +00001522 unsigned Tmp7 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001523 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1524 TmpMBB->addSuccessor(PhiMBB);
1525 BB = PhiMBB;
1526 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1527 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001528 }
1529 return Result+N.ResNo;
1530 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001531
Nate Begeman6b559972005-04-01 02:59:27 +00001532 case ISD::FP_TO_SINT: {
Nate Begeman6b559972005-04-01 02:59:27 +00001533 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman5a014812005-08-14 01:17:16 +00001534 Tmp2 = MakeFPReg();
1535 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1536 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1537 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1538 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1539 return Result;
Nate Begeman6b559972005-04-01 02:59:27 +00001540 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001541
Chris Lattner88ac32c2005-08-09 20:21:10 +00001542 case ISD::SETCC: {
1543 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1544 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
1545 // We can codegen setcc op, imm very efficiently compared to a brcond.
1546 // Check for those cases here.
1547 // setcc op, 0
1548 if (Tmp3 == 0) {
1549 Tmp1 = SelectExpr(Node->getOperand(0));
1550 switch (CC) {
1551 default: Node->dump(); assert(0 && "Unhandled SetCC condition"); abort();
1552 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001553 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001554 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1555 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1556 .addImm(5).addImm(31);
1557 break;
1558 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001559 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001560 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1561 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1562 break;
1563 case ISD::SETLT:
1564 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1565 .addImm(31).addImm(31);
1566 break;
1567 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001568 Tmp2 = MakeIntReg();
1569 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001570 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1571 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1572 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1573 .addImm(31).addImm(31);
1574 break;
Nate Begeman9765c252005-04-12 21:22:28 +00001575 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001576 return Result;
1577 } else if (Tmp3 == ~0U) { // setcc op, -1
1578 Tmp1 = SelectExpr(Node->getOperand(0));
1579 switch (CC) {
1580 default: assert(0 && "Unhandled SetCC condition"); abort();
1581 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001582 Tmp2 = MakeIntReg();
1583 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001584 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
1585 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
1586 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
1587 break;
1588 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001589 Tmp2 = MakeIntReg();
1590 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001591 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1592 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
1593 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
1594 break;
1595 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001596 Tmp2 = MakeIntReg();
1597 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001598 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
1599 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1600 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1601 .addImm(31).addImm(31);
1602 break;
1603 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001604 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001605 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1606 .addImm(31).addImm(31);
1607 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1608 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001609 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001610 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001611 }
Nate Begeman33162522005-03-29 21:54:38 +00001612 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001613
Nate Begemanc24d4842005-08-10 20:52:09 +00001614 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1615 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001616 return Result;
1617 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001618
1619 case ISD::SELECT_CC: {
1620 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
1621 if (!MVT::isInteger(N.getOperand(0).getValueType()) &&
1622 !MVT::isInteger(N.getOperand(2).getValueType()) &&
1623 CC != ISD::SETEQ && CC != ISD::SETNE) {
1624 MVT::ValueType VT = N.getOperand(0).getValueType();
1625 unsigned TV = SelectExpr(N.getOperand(2)); // Use if TRUE
1626 unsigned FV = SelectExpr(N.getOperand(3)); // Use if FALSE
Nate Begemana3fd4002005-07-19 16:51:05 +00001627
Nate Begemanc24d4842005-08-10 20:52:09 +00001628 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001629 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001630 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00001631 default: assert(0 && "Invalid FSEL condition"); abort();
1632 case ISD::SETULT:
1633 case ISD::SETLT:
1634 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1635 case ISD::SETUGE:
1636 case ISD::SETGE:
Nate Begemanc24d4842005-08-10 20:52:09 +00001637 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00001638 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1639 return Result;
1640 case ISD::SETUGT:
1641 case ISD::SETGT:
1642 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1643 case ISD::SETULE:
1644 case ISD::SETLE: {
Nate Begemanc24d4842005-08-10 20:52:09 +00001645 if (N.getOperand(0).getOpcode() == ISD::FNEG) {
1646 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Nate Begemana3fd4002005-07-19 16:51:05 +00001647 } else {
1648 Tmp2 = MakeReg(VT);
Nate Begemanc24d4842005-08-10 20:52:09 +00001649 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00001650 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1651 }
1652 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1653 return Result;
1654 }
1655 }
1656 } else {
1657 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanc24d4842005-08-10 20:52:09 +00001658 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
1659 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001660 Tmp3 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001661 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00001662 default: assert(0 && "Invalid FSEL condition"); abort();
1663 case ISD::SETULT:
1664 case ISD::SETLT:
1665 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1666 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1667 return Result;
1668 case ISD::SETUGE:
1669 case ISD::SETGE:
1670 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1671 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1672 return Result;
1673 case ISD::SETUGT:
1674 case ISD::SETGT:
1675 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1676 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1677 return Result;
1678 case ISD::SETULE:
1679 case ISD::SETLE:
1680 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1681 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1682 return Result;
1683 }
1684 }
1685 assert(0 && "Should never get here");
Nate Begemana3fd4002005-07-19 16:51:05 +00001686 }
1687
Nate Begeman5a014812005-08-14 01:17:16 +00001688 // If the False value only has one use, we can generate better code by
1689 // selecting it in the fallthrough basic block rather than here, which
1690 // increases register pressure.
Nate Begeman5a014812005-08-14 01:17:16 +00001691 unsigned TrueValue = SelectExpr(N.getOperand(2));
Nate Begeman2d56e722005-08-14 18:38:32 +00001692 unsigned FalseValue = SelectExpr(N.getOperand(3));
Nate Begemanc24d4842005-08-10 20:52:09 +00001693 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1694 Opc = getBCCForSetCC(CC);
1695
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001696 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00001697 // value and the MBB to hold the PHI instruction for this SetCC.
1698 MachineBasicBlock *thisMBB = BB;
1699 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1700 ilist<MachineBasicBlock>::iterator It = BB;
1701 ++It;
1702
1703 // thisMBB:
1704 // ...
1705 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001706 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00001707 // bCC copy1MBB
1708 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001709 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1710 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001711 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001712 MachineFunction *F = BB->getParent();
1713 F->getBasicBlockList().insert(It, copy0MBB);
1714 F->getBasicBlockList().insert(It, sinkMBB);
1715 // Update machine-CFG edges
1716 BB->addSuccessor(copy0MBB);
1717 BB->addSuccessor(sinkMBB);
1718
1719 // copy0MBB:
1720 // %FalseValue = ...
1721 // # fallthrough to sinkMBB
1722 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001723 // Update machine-CFG edges
1724 BB->addSuccessor(sinkMBB);
1725
1726 // sinkMBB:
1727 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1728 // ...
1729 BB = sinkMBB;
1730 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1731 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001732 return Result;
1733 }
Nate Begemana9795f82005-03-24 04:41:43 +00001734
Chris Lattner0c09a412005-08-18 17:16:52 +00001735 case ISD::Constant: {
1736 assert(N.getValueType() == MVT::i32 &&
1737 "Only i32 constants are legal on this target!");
1738 int v = (int)cast<ConstantSDNode>(N)->getValue();
Nate Begemana6940472005-08-18 18:01:39 +00001739 unsigned Hi = HA16(v);
Chris Lattner0c09a412005-08-18 17:16:52 +00001740 unsigned Lo = Lo16(v);
1741 if (Hi && Lo) {
1742 Tmp1 = MakeIntReg();
1743 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi);
1744 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo);
1745 } else if (Hi) {
1746 BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi);
1747 } else {
1748 BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo);
Nate Begemana9795f82005-03-24 04:41:43 +00001749 }
1750 return Result;
Chris Lattner0c09a412005-08-18 17:16:52 +00001751 }
Nate Begemana3fd4002005-07-19 16:51:05 +00001752
1753 case ISD::ConstantFP: {
1754 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
1755 Result = getConstDouble(CN->getValue(), Result);
1756 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001757 }
1758
Nate Begemana3fd4002005-07-19 16:51:05 +00001759 case ISD::FNEG:
1760 if (!NoExcessFPPrecision &&
1761 ISD::ADD == N.getOperand(0).getOpcode() &&
1762 N.getOperand(0).Val->hasOneUse() &&
1763 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1764 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
1765 ++FusedFP; // Statistic
1766 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1767 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1768 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1769 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1770 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1771 } else if (!NoExcessFPPrecision &&
1772 ISD::ADD == N.getOperand(0).getOpcode() &&
1773 N.getOperand(0).Val->hasOneUse() &&
1774 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1775 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
1776 ++FusedFP; // Statistic
1777 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1778 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1779 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1780 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1781 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1782 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
1783 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1784 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1785 } else {
1786 Tmp1 = SelectExpr(N.getOperand(0));
1787 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1788 }
1789 return Result;
1790
1791 case ISD::FABS:
1792 Tmp1 = SelectExpr(N.getOperand(0));
1793 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1794 return Result;
1795
Nate Begemanadeb43d2005-07-20 22:42:00 +00001796 case ISD::FSQRT:
1797 Tmp1 = SelectExpr(N.getOperand(0));
1798 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
1799 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1800 return Result;
1801
Nate Begemana3fd4002005-07-19 16:51:05 +00001802 case ISD::FP_ROUND:
1803 assert (DestType == MVT::f32 &&
1804 N.getOperand(0).getValueType() == MVT::f64 &&
1805 "only f64 to f32 conversion supported here");
1806 Tmp1 = SelectExpr(N.getOperand(0));
1807 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1808 return Result;
1809
1810 case ISD::FP_EXTEND:
1811 assert (DestType == MVT::f64 &&
1812 N.getOperand(0).getValueType() == MVT::f32 &&
1813 "only f32 to f64 conversion supported here");
1814 Tmp1 = SelectExpr(N.getOperand(0));
1815 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1816 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00001817 }
Nate Begemana9795f82005-03-24 04:41:43 +00001818 return 0;
1819}
1820
1821void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00001822 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00001823 unsigned opcode = N.getOpcode();
1824
1825 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1826 return; // Already selected.
1827
1828 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001829
Nate Begemana9795f82005-03-24 04:41:43 +00001830 switch (Node->getOpcode()) {
1831 default:
1832 Node->dump(); std::cerr << "\n";
1833 assert(0 && "Node not handled yet!");
1834 case ISD::EntryToken: return; // Noop
1835 case ISD::TokenFactor:
1836 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1837 Select(Node->getOperand(i));
1838 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001839 case ISD::CALLSEQ_START:
1840 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00001841 Select(N.getOperand(0));
1842 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001843 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00001844 PPC::ADJCALLSTACKUP;
1845 BuildMI(BB, Opc, 1).addImm(Tmp1);
1846 return;
1847 case ISD::BR: {
1848 MachineBasicBlock *Dest =
1849 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001850 Select(N.getOperand(0));
1851 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1852 return;
1853 }
Nate Begeman7cbd5252005-08-16 19:49:35 +00001854 case ISD::BR_CC:
1855 case ISD::BRTWOWAY_CC:
Nate Begemana9795f82005-03-24 04:41:43 +00001856 SelectBranchCC(N);
1857 return;
1858 case ISD::CopyToReg:
1859 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001860 Tmp1 = SelectExpr(N.getOperand(2));
1861 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001862
Nate Begemana9795f82005-03-24 04:41:43 +00001863 if (Tmp1 != Tmp2) {
Chris Lattnera8cd0152005-08-16 21:58:15 +00001864 if (N.getOperand(2).getValueType() == MVT::f64 ||
1865 N.getOperand(2).getValueType() == MVT::f32)
Nate Begemana9795f82005-03-24 04:41:43 +00001866 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1867 else
1868 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1869 }
1870 return;
1871 case ISD::ImplicitDef:
1872 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001873 BuildMI(BB, PPC::IMPLICIT_DEF, 0,
1874 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Nate Begemana9795f82005-03-24 04:41:43 +00001875 return;
1876 case ISD::RET:
1877 switch (N.getNumOperands()) {
1878 default:
1879 assert(0 && "Unknown return instruction!");
1880 case 3:
1881 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1882 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001883 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00001884 Select(N.getOperand(0));
1885 Tmp1 = SelectExpr(N.getOperand(1));
1886 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001887 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1888 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001889 break;
1890 case 2:
1891 Select(N.getOperand(0));
1892 Tmp1 = SelectExpr(N.getOperand(1));
1893 switch (N.getOperand(1).getValueType()) {
1894 default:
1895 assert(0 && "Unknown return type!");
1896 case MVT::f64:
1897 case MVT::f32:
1898 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1899 break;
1900 case MVT::i32:
1901 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1902 break;
1903 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001904 case 1:
1905 Select(N.getOperand(0));
1906 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001907 }
1908 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1909 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001910 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00001911 case ISD::STORE: {
1912 SDOperand Chain = N.getOperand(0);
1913 SDOperand Value = N.getOperand(1);
1914 SDOperand Address = N.getOperand(2);
1915 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00001916
Nate Begeman2497e632005-07-21 20:44:43 +00001917 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00001918
Nate Begeman2497e632005-07-21 20:44:43 +00001919 if (opcode == ISD::STORE) {
1920 switch(Value.getValueType()) {
1921 default: assert(0 && "unknown Type in store");
1922 case MVT::i32: Opc = PPC::STW; break;
1923 case MVT::f64: Opc = PPC::STFD; break;
1924 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001925 }
Nate Begeman2497e632005-07-21 20:44:43 +00001926 } else { //ISD::TRUNCSTORE
1927 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1928 default: assert(0 && "unknown Type in store");
1929 case MVT::i1:
1930 case MVT::i8: Opc = PPC::STB; break;
1931 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001932 }
Nate Begemana9795f82005-03-24 04:41:43 +00001933 }
Nate Begeman2497e632005-07-21 20:44:43 +00001934
1935 if(Address.getOpcode() == ISD::FrameIndex) {
1936 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1937 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00001938 } else {
1939 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001940 switch(SelectAddr(Address, Tmp2, offset)) {
1941 default: assert(0 && "Unhandled return value from SelectAddr");
1942 case 0: // imm offset, no frame, no index
1943 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
1944 break;
1945 case 1: // imm offset + frame index
1946 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
1947 break;
1948 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00001949 Opc = IndexedOpForOp(Opc);
1950 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001951 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001952 case 3: {
1953 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1954 GlobalValue *GV = GN->getGlobal();
1955 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
1956 }
Nate Begeman2497e632005-07-21 20:44:43 +00001957 }
1958 }
1959 return;
1960 }
Nate Begemana9795f82005-03-24 04:41:43 +00001961 case ISD::EXTLOAD:
1962 case ISD::SEXTLOAD:
1963 case ISD::ZEXTLOAD:
1964 case ISD::LOAD:
1965 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001966 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00001967 case ISD::CALL:
1968 case ISD::DYNAMIC_STACKALLOC:
1969 ExprMap.erase(N);
1970 SelectExpr(N);
1971 return;
1972 }
1973 assert(0 && "Should not be reached!");
1974}
1975
1976
1977/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1978/// into a machine code representation using pattern matching and a machine
1979/// description file.
1980///
1981FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001982 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001983}
1984