blob: 75f568f349ad906cae216d97fbb9239457f0fecd [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 {
Nate Begemanc7bd4822005-04-11 06:34:10 +000039Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +000040Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman2a05c8e2005-07-28 03:02:05 +000041Statistic<>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//===--------------------------------------------------------------------===//
44/// ISel - PPC32 specific code to select PPC32 machine instructions for
45/// SelectionDAG operations.
46//===--------------------------------------------------------------------===//
47class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +000048 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +000049 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
50 // for sdiv and udiv until it is put into the future
51 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000052
Nate Begemana9795f82005-03-24 04:41:43 +000053 /// ExprMap - As shared expressions are codegen'd, we keep track of which
54 /// vreg the value is produced in, so we only emit one copy of each compiled
55 /// tree.
56 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +000057
58 unsigned GlobalBaseReg;
59 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +000060 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +000061public:
Nate Begeman815d6da2005-04-06 00:25:27 +000062 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
63 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000064
Nate Begemanc7b09f12005-03-25 08:34:25 +000065 /// runOnFunction - Override this function in order to reset our per-function
66 /// variables.
67 virtual bool runOnFunction(Function &Fn) {
68 // Make sure we re-emit a set of the global base reg if necessary
69 GlobalBaseInitialized = false;
70 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000071 }
72
Nate Begemana9795f82005-03-24 04:41:43 +000073 /// InstructionSelectBasicBlock - This callback is invoked by
74 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
75 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
76 DEBUG(BB->dump());
77 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +000078 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +000079 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +000080
Nate Begemana9795f82005-03-24 04:41:43 +000081 // Clear state used for selection.
82 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +000083 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +000084 }
Nate Begeman815d6da2005-04-06 00:25:27 +000085
Chris Lattner54abfc52005-08-11 17:15:31 +000086 // convenience functions for virtual register creation
87 inline unsigned MakeIntReg() {
88 return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
89 }
90 inline unsigned MakeFPReg() {
91 return RegMap->createVirtualRegister(PPC32::FPRCRegisterClass);
92 }
93
Nate Begeman815d6da2005-04-06 00:25:27 +000094 // dag -> dag expanders for integer divide by constant
95 SDOperand BuildSDIVSequence(SDOperand N);
96 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000097
Nate Begemandffcfcc2005-04-01 00:32:34 +000098 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +000099 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemanc24d4842005-08-10 20:52:09 +0000100 void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000101 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000102 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begemanc24d4842005-08-10 20:52:09 +0000103 unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
Chris Lattnerb4138c42005-08-10 18:11:33 +0000104 bool SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000105 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000106 bool IsArithmetic = false, bool Negate = false);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000107 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000108 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000109
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000110 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000111 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000112
113 virtual const char *getPassName() const {
114 return "PowerPC Pattern Instruction Selection";
115 }
Nate Begemana9795f82005-03-24 04:41:43 +0000116};
117
Chris Lattner02efa6c2005-08-08 21:08:09 +0000118// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
119// any number of 0s on either side. The 1s are allowed to wrap from LSB to
120// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
121// not, since all 1s are not contiguous.
122static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
123 if (isShiftedMask_32(Val)) {
124 // look for the first non-zero bit
125 MB = CountLeadingZeros_32(Val);
126 // look for the first zero bit after the run of ones
127 ME = CountLeadingZeros_32((Val - 1) ^ Val);
128 return true;
129 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
130 // effectively look for the first zero bit
131 ME = CountLeadingZeros_32(Val) - 1;
132 // effectively look for the first one bit after the run of zeros
133 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
134 return true;
135 }
136 // no run present
137 return false;
138}
139
Chris Lattnercf1cf182005-08-08 21:10:27 +0000140// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
141// and mask opcode and mask operation.
142static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
143 bool IsShiftMask,
144 unsigned &SH, unsigned &MB, unsigned &ME) {
145 if (Shift > 31) return false;
146 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
147
148 if (Opcode == ISD::SHL) { // shift left
149 // apply shift to mask if it comes first
150 if (IsShiftMask) Mask = Mask << Shift;
151 // determine which bits are made indeterminant by shift
152 Indeterminant = ~(0xFFFFFFFFu << Shift);
153 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
154 // apply shift to mask if it comes first
155 if (IsShiftMask) Mask = Mask >> Shift;
156 // determine which bits are made indeterminant by shift
157 Indeterminant = ~(0xFFFFFFFFu >> Shift);
158 // adjust for the left rotate
159 Shift = 32 - Shift;
160 }
161
162 // if the mask doesn't intersect any Indeterminant bits
Jim Laskeycf083e32005-08-12 23:52:46 +0000163 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000164 SH = Shift;
165 // make sure the mask is still a mask (wrap arounds may not be)
166 return isRunOfOnes(Mask, MB, ME);
167 }
168
169 // can't do it
170 return false;
171}
172
Chris Lattner59b21c22005-08-09 18:29:55 +0000173// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000174// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000175static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000176 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000177 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000178 // retrieve value
Chris Lattner59b21c22005-08-09 18:29:55 +0000179 Imm = (unsigned)CN->getSignExtended();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000180 // passes muster
181 return true;
182 }
183 // not a constant
184 return false;
185}
186
Jim Laskey191cf942005-08-11 21:59:23 +0000187// isOpcWithIntImmediate - This method tests to see if the node is a specific
188// opcode and that it has a immediate integer right operand.
189// If so Imm will receive the 32 bit value.
190static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) {
191 return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm);
192}
193
Chris Lattnercf1cf182005-08-08 21:10:27 +0000194// isOprShiftImm - Returns true if the specified operand is a shift opcode with
195// a immediate shift count less than 32.
196static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
197 Opc = N.getOpcode();
198 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000199 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000200}
201
202// isOprNot - Returns true if the specified operand is an xor with immediate -1.
203static bool isOprNot(SDOperand N) {
204 unsigned Imm;
Jim Laskey191cf942005-08-11 21:59:23 +0000205 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000206}
207
208// Immediate constant composers.
209// Lo16 - grabs the lo 16 bits from a 32 bit constant.
210// Hi16 - grabs the hi 16 bits from a 32 bit constant.
211// HA16 - computes the hi bits required if the lo bits are add/subtracted in
212// arithmethically.
213static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
214static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
215static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
216
Nate Begemanc7bd4822005-04-11 06:34:10 +0000217/// NodeHasRecordingVariant - If SelectExpr can always produce code for
218/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
219/// return false.
220static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
221 switch(NodeOpcode) {
222 default: return false;
223 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000225 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000226 }
227}
228
Nate Begeman3e897162005-03-31 23:55:40 +0000229/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
Nate Begemanc24d4842005-08-10 20:52:09 +0000230/// to Condition.
231static unsigned getBCCForSetCC(ISD::CondCode CC) {
232 switch (CC) {
Nate Begeman3e897162005-03-31 23:55:40 +0000233 default: assert(0 && "Unknown condition!"); abort();
234 case ISD::SETEQ: return PPC::BEQ;
235 case ISD::SETNE: return PPC::BNE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000236 case ISD::SETULT:
Nate Begeman3e897162005-03-31 23:55:40 +0000237 case ISD::SETLT: return PPC::BLT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000238 case ISD::SETULE:
Nate Begeman3e897162005-03-31 23:55:40 +0000239 case ISD::SETLE: return PPC::BLE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000240 case ISD::SETUGT:
Nate Begeman3e897162005-03-31 23:55:40 +0000241 case ISD::SETGT: return PPC::BGT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000242 case ISD::SETUGE:
Nate Begeman3e897162005-03-31 23:55:40 +0000243 case ISD::SETGE: return PPC::BGE;
244 }
Nate Begeman04730362005-04-01 04:45:11 +0000245 return 0;
246}
247
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000248/// getCROpForOp - Return the condition register opcode (or inverted opcode)
249/// associated with the SelectionDAG opcode.
250static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
251 switch (Opcode) {
252 default: assert(0 && "Unknown opcode!"); abort();
253 case ISD::AND:
254 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
255 if (!Inv1 && !Inv2) return PPC::CRAND;
256 if (Inv1 ^ Inv2) return PPC::CRANDC;
257 case ISD::OR:
258 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
259 if (!Inv1 && !Inv2) return PPC::CROR;
260 if (Inv1 ^ Inv2) return PPC::CRORC;
261 }
262 return 0;
263}
264
265/// getCRIdxForSetCC - Return the index of the condition register field
266/// associated with the SetCC condition, and whether or not the field is
267/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000268static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
269 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000270 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000271 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000272 case ISD::SETLT: Inv = false; return 0;
273 case ISD::SETUGE:
274 case ISD::SETGE: Inv = true; return 0;
275 case ISD::SETUGT:
276 case ISD::SETGT: Inv = false; return 1;
277 case ISD::SETULE:
278 case ISD::SETLE: Inv = true; return 1;
279 case ISD::SETEQ: Inv = false; return 2;
280 case ISD::SETNE: Inv = true; return 2;
281 }
282 return 0;
283}
284
Nate Begeman04730362005-04-01 04:45:11 +0000285/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
286/// and store immediate instructions.
287static unsigned IndexedOpForOp(unsigned Opcode) {
288 switch(Opcode) {
289 default: assert(0 && "Unknown opcode!"); abort();
290 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
291 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
292 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
293 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
294 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
295 case PPC::LFD: return PPC::LFDX;
296 }
297 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000298}
Nate Begeman815d6da2005-04-06 00:25:27 +0000299
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000300// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000301// a multiply.
302struct ms {
303 int m; // magic number
304 int s; // shift amount
305};
306
307struct mu {
308 unsigned int m; // magic number
309 int a; // add indicator
310 int s; // shift amount
311};
312
313/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000314/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000315/// or -1.
316static struct ms magic(int d) {
317 int p;
318 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000319 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000320 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000321
Nate Begeman815d6da2005-04-06 00:25:27 +0000322 ad = abs(d);
323 t = two31 + ((unsigned int)d >> 31);
324 anc = t - 1 - t%ad; // absolute value of nc
325 p = 31; // initialize p
326 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
327 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
328 q2 = two31/ad; // initialize q2 = 2p/abs(d)
329 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
330 do {
331 p = p + 1;
332 q1 = 2*q1; // update q1 = 2p/abs(nc)
333 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
334 if (r1 >= anc) { // must be unsigned comparison
335 q1 = q1 + 1;
336 r1 = r1 - anc;
337 }
338 q2 = 2*q2; // update q2 = 2p/abs(d)
339 r2 = 2*r2; // update r2 = rem(2p/abs(d))
340 if (r2 >= ad) { // must be unsigned comparison
341 q2 = q2 + 1;
342 r2 = r2 - ad;
343 }
344 delta = ad - r2;
345 } while (q1 < delta || (q1 == delta && r1 == 0));
346
347 mag.m = q2 + 1;
348 if (d < 0) mag.m = -mag.m; // resulting magic number
349 mag.s = p - 32; // resulting shift
350 return mag;
351}
352
353/// magicu - calculate the magic numbers required to codegen an integer udiv as
354/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
355static struct mu magicu(unsigned d)
356{
357 int p;
358 unsigned int nc, delta, q1, r1, q2, r2;
359 struct mu magu;
360 magu.a = 0; // initialize "add" indicator
361 nc = - 1 - (-d)%d;
362 p = 31; // initialize p
363 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
364 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
365 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
366 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
367 do {
368 p = p + 1;
369 if (r1 >= nc - r1 ) {
370 q1 = 2*q1 + 1; // update q1
371 r1 = 2*r1 - nc; // update r1
372 }
373 else {
374 q1 = 2*q1; // update q1
375 r1 = 2*r1; // update r1
376 }
377 if (r2 + 1 >= d - r2) {
378 if (q2 >= 0x7FFFFFFF) magu.a = 1;
379 q2 = 2*q2 + 1; // update q2
380 r2 = 2*r2 + 1 - d; // update r2
381 }
382 else {
383 if (q2 >= 0x80000000) magu.a = 1;
384 q2 = 2*q2; // update q2
385 r2 = 2*r2 + 1; // update r2
386 }
387 delta = d - 1 - r2;
388 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
389 magu.m = q2 + 1; // resulting magic number
390 magu.s = p - 32; // resulting shift
391 return magu;
392}
393}
394
395/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
396/// return a DAG expression to select that will generate the same value by
397/// multiplying by a magic number. See:
398/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
399SDOperand ISel::BuildSDIVSequence(SDOperand N) {
400 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
401 ms magics = magic(d);
402 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000403 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000404 ISelDAG->getConstant(magics.m, MVT::i32));
405 // If d > 0 and m < 0, add the numerator
406 if (d > 0 && magics.m < 0)
407 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
408 // If d < 0 and m > 0, subtract the numerator.
409 if (d < 0 && magics.m > 0)
410 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
411 // Shift right algebraic if shift value is nonzero
412 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000413 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000414 ISelDAG->getConstant(magics.s, MVT::i32));
415 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000416 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000417 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000418 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000419}
420
421/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
422/// return a DAG expression to select that will generate the same value by
423/// multiplying by a magic number. See:
424/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
425SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000426 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000427 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
428 mu magics = magicu(d);
429 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000430 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000431 ISelDAG->getConstant(magics.m, MVT::i32));
432 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000433 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000434 ISelDAG->getConstant(magics.s, MVT::i32));
435 } else {
436 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000437 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000438 ISelDAG->getConstant(1, MVT::i32));
439 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000440 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000441 ISelDAG->getConstant(magics.s-1, MVT::i32));
442 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000443 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000444}
445
Nate Begemanc7b09f12005-03-25 08:34:25 +0000446/// getGlobalBaseReg - Output the instructions required to put the
447/// base address to use for accessing globals into a register.
448///
449unsigned ISel::getGlobalBaseReg() {
450 if (!GlobalBaseInitialized) {
451 // Insert the set of GlobalBaseReg into the first MBB of the function
452 MachineBasicBlock &FirstMBB = BB->getParent()->front();
453 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000454 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000455 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
456 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
457 GlobalBaseInitialized = true;
458 }
459 return GlobalBaseReg;
460}
461
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000462/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000463/// Constant Pool. Optionally takes a register in which to load the value.
464unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000465 unsigned Tmp1 = MakeIntReg();
466 if (0 == Result) Result = MakeFPReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000467 MachineConstantPool *CP = BB->getParent()->getConstantPool();
468 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
469 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000470 if (PICEnabled)
471 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
472 .addConstantPoolIndex(CPI);
473 else
474 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000475 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
476 return Result;
477}
478
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000479/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000480/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000481void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
482 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000483 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000484 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000485 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000486 bool GPOpt =
487 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
488 BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000489 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000490 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000491 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
492 .addImm(31).addImm(31);
493 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
494 } else {
495 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
496 .addImm(31).addImm(31);
497 }
498}
499
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000500/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000501/// the rotate left word immediate then mask insert (rlwimi) instruction.
502/// Returns true on success, false if the caller still needs to select OR.
503///
504/// Patterns matched:
505/// 1. or shl, and 5. or and, and
506/// 2. or and, shl 6. or shl, shr
507/// 3. or shr, and 7. or shr, shl
508/// 4. or and, shr
509bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000510 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000511 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000512 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000513
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000514 SDOperand Op0 = OR.getOperand(0);
515 SDOperand Op1 = OR.getOperand(1);
516
517 unsigned Op0Opc = Op0.getOpcode();
518 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000519
Nate Begeman7ddecb42005-04-06 23:51:40 +0000520 // Verify that we have the correct opcodes
521 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
522 return false;
523 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
524 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000525
Nate Begeman7ddecb42005-04-06 23:51:40 +0000526 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +0000527 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000528 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000529 case ISD::SHL: TgtMask <<= Value; break;
530 case ISD::SRL: TgtMask >>= Value; break;
531 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000532 }
533 } else {
534 return false;
535 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000536
Nate Begeman7ddecb42005-04-06 23:51:40 +0000537 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +0000538 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000539 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000540 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000541 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000542 InsMask <<= Amount;
543 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000544 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000545 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000546 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000547 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000548 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000549 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000550 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000551 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000552 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000553 break;
554 }
555 } else {
556 return false;
557 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000558
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000559 unsigned Tmp3 = 0;
560
561 // If both of the inputs are ANDs and one of them has a logical shift by
562 // constant as its input, make that the inserted value so that we can combine
563 // the shift into the rotate part of the rlwimi instruction
564 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000565 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000566 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000567 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000568 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000569 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000570 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
571 }
572 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
573 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000574 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000575 std::swap(Op0, Op1);
576 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +0000577 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000578 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000579 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
580 }
581 }
582 }
583
Nate Begeman7ddecb42005-04-06 23:51:40 +0000584 // Verify that the Target mask and Insert mask together form a full word mask
585 // and that the Insert mask is a run of set bits (which implies both are runs
586 // of set bits). Given that, Select the arguments and generate the rlwimi
587 // instruction.
588 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +0000589 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000590 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000591 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000592 // Check for rotlwi / rotrwi here, a special case of bitfield insert
593 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000594 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +0000595 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000596 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
597 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
598 .addImm(0).addImm(31);
599 return true;
600 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000601 if (Op0Opc == ISD::AND && fullMask)
602 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000603 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000604 Tmp1 = SelectExpr(Op0);
605 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000606 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
607 .addImm(Amount).addImm(MB).addImm(ME);
608 return true;
609 }
610 return false;
611}
612
Nate Begeman3664cef2005-04-13 22:14:14 +0000613/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
614/// low six bits. If the shift amount is an ISD::AND node with a mask that is
615/// wider than the implicit mask, then we can get rid of the AND and let the
616/// shift do the mask.
617unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Jim Laskey191cf942005-08-11 21:59:23 +0000618 unsigned C;
619 if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63)
Nate Begeman3664cef2005-04-13 22:14:14 +0000620 return SelectExpr(N.getOperand(0));
621 else
622 return SelectExpr(N);
623}
624
Nate Begemanc24d4842005-08-10 20:52:09 +0000625unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000626 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000627 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000628 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +0000629 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000630
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000631 // Allocate a condition register for this expression
632 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000633
Nate Begemanc24d4842005-08-10 20:52:09 +0000634 // Use U to determine whether the SETCC immediate range is signed or not.
635 bool U = ISD::isUnsignedIntSetCC(CC);
636 if (isIntImmediate(RHS, Tmp2) &&
637 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
638 Tmp2 = Lo16(Tmp2);
639 // For comparisons against zero, we can implicity set CR0 if a recording
640 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
641 // operand zero of the SetCC node is available.
642 if (Tmp2 == 0 &&
643 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
644 RecordSuccess = false;
645 Tmp1 = SelectExpr(LHS, true);
646 if (RecordSuccess) {
647 ++Recorded;
648 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
649 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000650 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000651 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000652 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000653 // If we could not implicitly set CR0, then emit a compare immediate
654 // instead.
655 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
656 if (U)
657 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
658 else
659 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000660 } else {
Nate Begemanc24d4842005-08-10 20:52:09 +0000661 bool IsInteger = MVT::isInteger(LHS.getValueType());
662 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
663 Tmp1 = SelectExpr(LHS);
664 Tmp2 = SelectExpr(RHS);
665 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000666 }
667 return Result;
668}
669
Nate Begemand3ded2d2005-08-08 22:22:56 +0000670/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000671unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000672{
Nate Begeman96fc6812005-03-31 02:05:53 +0000673 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000674 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000675 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +0000676 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +0000677 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000678 if (isFrame) {
679 ++FrameOff;
680 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
681 return 1;
682 } else {
683 Reg = SelectExpr(N.getOperand(0));
684 return 0;
685 }
686 } else {
687 Reg = SelectExpr(N.getOperand(0));
688 offset = SelectExpr(N.getOperand(1));
689 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000690 }
Nate Begeman04730362005-04-01 04:45:11 +0000691 }
Nate Begemand3ded2d2005-08-08 22:22:56 +0000692 // Now check if we're dealing with a global, and whether or not we should emit
693 // an optimized load or store for statics.
694 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
695 GlobalValue *GV = GN->getGlobal();
696 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000697 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +0000698 if (PICEnabled)
699 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
700 .addGlobalAddress(GV);
701 else
702 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
703 Reg = GlobalHi;
704 offset = 0;
705 return 3;
706 }
707 }
Nate Begemana9795f82005-03-24 04:41:43 +0000708 Reg = SelectExpr(N);
709 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000710 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000711}
712
713void ISel::SelectBranchCC(SDOperand N)
714{
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000715 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +0000716 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000717
Nate Begemana9795f82005-03-24 04:41:43 +0000718 Select(N.getOperand(0)); //chain
Nate Begemanc24d4842005-08-10 20:52:09 +0000719
720 // FIXME: Until we have Branch_CC and Branch_Twoway_CC, we're going to have to
721 // Fake it up by hand by checking to see if op 1 is a SetCC, or a boolean.
722 unsigned CCReg;
723 ISD::CondCode CC;
724 SDOperand Cond = N.getOperand(1);
725 if (Cond.getOpcode() == ISD::SETCC) {
726 CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
727 CCReg = SelectCC(Cond.getOperand(0), Cond.getOperand(1), CC);
728 } else {
729 CC = ISD::SETNE;
730 CCReg = SelectCC(Cond, ISelDAG->getConstant(0, Cond.getValueType()), CC);
731 }
732 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000733
Nate Begeman439009c2005-06-15 18:22:43 +0000734 // Iterate to the next basic block
735 ilist<MachineBasicBlock>::iterator It = BB;
736 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000737
738 // If this is a two way branch, then grab the fallthrough basic block argument
739 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
740 // if necessary by the branch selection pass. Otherwise, emit a standard
741 // conditional branch.
742 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000743 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +0000744 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
745 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000746 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +0000747 .addMBB(Dest).addMBB(Fallthrough);
748 if (Fallthrough != It)
749 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
750 } else {
751 if (Fallthrough != It) {
752 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000753 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +0000754 .addMBB(Fallthrough).addMBB(Dest);
755 }
756 }
757 } else {
Nate Begeman439009c2005-06-15 18:22:43 +0000758 // If the fallthrough path is off the end of the function, which would be
759 // undefined behavior, set it to be the same as the current block because
760 // we have nothing better to set it to, and leaving it alone will cause the
761 // PowerPC Branch Selection pass to crash.
762 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000763 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +0000764 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000765 }
Nate Begemana9795f82005-03-24 04:41:43 +0000766 return;
767}
768
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000769// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +0000770bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000771 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000772 bool IsArithmetic, bool Negate) {
773 // check constant
774 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
775 // exit if not a constant
776 if (!CN) return false;
777 // extract immediate
778 unsigned C = (unsigned)CN->getSignExtended();
779 // negate if required (ISD::SUB)
780 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000781 // get the hi and lo portions of constant
782 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
783 unsigned Lo = Lo16(C);
784 // assume no intermediate result from lo instruction (same as final result)
785 unsigned Tmp = Result;
786 // check if two instructions are needed
787 if (Hi && Lo) {
788 // exit if usage indicates it would be better to load immediate into a
789 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +0000790 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000791 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +0000792 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000793 }
794 // get first operand
795 unsigned Opr0 = SelectExpr(N.getOperand(0));
796 // is a lo instruction needed
797 if (Lo) {
798 // generate instruction for hi portion
799 const MachineInstrBuilder &MIBLo = BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0);
800 if (IsArithmetic) MIBLo.addSImm(Lo); else MIBLo.addImm(Lo);
801 // need to switch out first operand for hi instruction
802 Opr0 = Tmp;
803 }
804 // is a ho instruction needed
805 if (Hi) {
806 // generate instruction for hi portion
807 const MachineInstrBuilder &MIBHi = BuildMI(BB, OCHi, 2, Result).addReg(Opr0);
808 if (IsArithmetic) MIBHi.addSImm(Hi); else MIBHi.addImm(Hi);
809 }
810 return true;
811}
812
Nate Begemanc7bd4822005-04-11 06:34:10 +0000813unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +0000814 unsigned Result;
815 unsigned Tmp1, Tmp2, Tmp3;
816 unsigned Opc = 0;
817 unsigned opcode = N.getOpcode();
818
819 SDNode *Node = N.Val;
820 MVT::ValueType DestType = N.getValueType();
821
Nate Begemana43b1762005-06-14 03:55:23 +0000822 if (Node->getOpcode() == ISD::CopyFromReg &&
Chris Lattner988b1dd2005-07-28 05:23:43 +0000823 (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
824 cast<RegSDNode>(Node)->getReg() == PPC::R1))
Nate Begemana43b1762005-06-14 03:55:23 +0000825 // Just use the specified register as our input.
826 return cast<RegSDNode>(Node)->getReg();
827
Nate Begemana9795f82005-03-24 04:41:43 +0000828 unsigned &Reg = ExprMap[N];
829 if (Reg) return Reg;
830
Nate Begeman27eeb002005-04-02 05:59:34 +0000831 switch (N.getOpcode()) {
832 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000833 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000834 MakeReg(N.getValueType()) : 1;
835 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000836 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +0000837 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000838 // If this is a call instruction, make sure to prepare ALL of the result
839 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000840 if (Node->getNumValues() == 1)
841 Reg = Result = 1; // Void call, just a chain.
842 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000843 Result = MakeReg(Node->getValueType(0));
844 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000845 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000846 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000847 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000848 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000849 break;
850 case ISD::ADD_PARTS:
851 case ISD::SUB_PARTS:
852 case ISD::SHL_PARTS:
853 case ISD::SRL_PARTS:
854 case ISD::SRA_PARTS:
855 Result = MakeReg(Node->getValueType(0));
856 ExprMap[N.getValue(0)] = Result;
857 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
858 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
859 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000860 }
861
Nate Begemana9795f82005-03-24 04:41:43 +0000862 switch (opcode) {
863 default:
Nate Begeman5a014812005-08-14 01:17:16 +0000864 Node->dump(); std::cerr << '\n';
865 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000866 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000867 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
868 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000869 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000870 // Generate both result values. FIXME: Need a better commment here?
871 if (Result != 1)
872 ExprMap[N.getValue(1)] = 1;
873 else
874 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
875
876 // FIXME: We are currently ignoring the requested alignment for handling
877 // greater than the stack alignment. This will need to be revisited at some
878 // point. Align = N.getOperand(2);
879 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
880 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
881 std::cerr << "Cannot allocate stack object with greater alignment than"
882 << " the stack alignment yet!";
883 abort();
884 }
885 Select(N.getOperand(0));
886 Tmp1 = SelectExpr(N.getOperand(1));
887 // Subtract size from stack pointer, thereby allocating some space.
888 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
889 // Put a pointer to the space into the result register by copying the SP
890 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
891 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000892
893 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000894 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
Chris Lattner54abfc52005-08-11 17:15:31 +0000895 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000896 if (PICEnabled)
897 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
898 .addConstantPoolIndex(Tmp1);
899 else
900 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000901 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
902 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000903
904 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000905 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000906 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000907 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000908
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000909 case ISD::GlobalAddress: {
910 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +0000911 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000912 if (PICEnabled)
913 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
914 .addGlobalAddress(GV);
915 else
Chris Lattner4015ea82005-07-28 04:42:11 +0000916 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000917 if (GV->hasWeakLinkage() || GV->isExternal()) {
918 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
919 } else {
920 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
921 }
922 return Result;
923 }
924
Nate Begeman5e966612005-03-24 06:28:42 +0000925 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000926 case ISD::EXTLOAD:
927 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000928 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000929 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000930 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +0000931 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000932
Nate Begeman5e966612005-03-24 06:28:42 +0000933 // Make sure we generate both values.
934 if (Result != 1)
935 ExprMap[N.getValue(1)] = 1; // Generate the token
936 else
937 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
938
939 SDOperand Chain = N.getOperand(0);
940 SDOperand Address = N.getOperand(1);
941 Select(Chain);
942
Nate Begeman9db505c2005-03-28 19:36:43 +0000943 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000944 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000945 case MVT::i1: Opc = PPC::LBZ; break;
946 case MVT::i8: Opc = PPC::LBZ; break;
947 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
948 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000949 case MVT::f32: Opc = PPC::LFS; break;
950 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000951 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000952
Nate Begeman74d73452005-03-31 00:15:26 +0000953 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000954 Tmp1 = MakeIntReg();
Nate Begeman74d73452005-03-31 00:15:26 +0000955 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +0000956 if (PICEnabled)
957 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
958 .addConstantPoolIndex(CPI);
959 else
960 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +0000961 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +0000962 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000963 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
964 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000965 } else {
966 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000967 switch(SelectAddr(Address, Tmp1, offset)) {
968 default: assert(0 && "Unhandled return value from SelectAddr");
969 case 0: // imm offset, no frame, no index
970 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
971 break;
972 case 1: // imm offset + frame index
973 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
974 break;
975 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +0000976 Opc = IndexedOpForOp(Opc);
977 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000978 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +0000979 case 3: {
980 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
981 GlobalValue *GV = GN->getGlobal();
982 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
983 }
Nate Begeman04730362005-04-01 04:45:11 +0000984 }
Nate Begeman5e966612005-03-24 06:28:42 +0000985 }
986 return Result;
987 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000988
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000989 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000990 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000991 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000992 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000993 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
994 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
995 };
996 static const unsigned FPR[] = {
997 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
998 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
999 };
1000
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001001 // Lower the chain for this call.
1002 Select(N.getOperand(0));
1003 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001004
Nate Begemand860aa62005-04-04 22:17:48 +00001005 MachineInstr *CallMI;
1006 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001007 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001008 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001009 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001010 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001011 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001012 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001013 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001014 true);
1015 } else {
1016 Tmp1 = SelectExpr(N.getOperand(1));
1017 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1018 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1019 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1020 .addReg(PPC::R12);
1021 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001022
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001023 // Load the register args to virtual regs
1024 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001025 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001026 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1027
1028 // Copy the virtual registers into the appropriate argument register
1029 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1030 switch(N.getOperand(i+2).getValueType()) {
1031 default: Node->dump(); assert(0 && "Unknown value type for call");
1032 case MVT::i1:
1033 case MVT::i8:
1034 case MVT::i16:
1035 case MVT::i32:
1036 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001037 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001038 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001039 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1040 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001041 ++GPR_idx;
1042 break;
1043 case MVT::f64:
1044 case MVT::f32:
1045 assert(FPR_idx < 13 && "Too many fp args");
1046 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001047 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001048 ++FPR_idx;
1049 break;
1050 }
1051 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001052
Nate Begemand860aa62005-04-04 22:17:48 +00001053 // Put the call instruction in the correct place in the MachineBasicBlock
1054 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001055
1056 switch (Node->getValueType(0)) {
1057 default: assert(0 && "Unknown value type for call result!");
1058 case MVT::Other: return 1;
1059 case MVT::i1:
1060 case MVT::i8:
1061 case MVT::i16:
1062 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001063 if (Node->getValueType(1) == MVT::i32) {
1064 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1065 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1066 } else {
1067 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1068 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001069 break;
1070 case MVT::f32:
1071 case MVT::f64:
1072 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1073 break;
1074 }
1075 return Result+N.ResNo;
1076 }
Nate Begemana9795f82005-03-24 04:41:43 +00001077
1078 case ISD::SIGN_EXTEND:
1079 case ISD::SIGN_EXTEND_INREG:
1080 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001081 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001082 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001083 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001084 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001085 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001086 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001087 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001088 break;
Nate Begeman74747862005-03-29 22:24:51 +00001089 case MVT::i1:
1090 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1091 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001092 }
Nate Begemana9795f82005-03-24 04:41:43 +00001093 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001094
Nate Begemana9795f82005-03-24 04:41:43 +00001095 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001096 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001097 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001098 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001099 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001100 if (MVT::isInteger(DestType))
1101 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1102 else
1103 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001104 return Result;
1105
1106 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001107 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001108 unsigned SH, MB, ME;
1109 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1110 isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) {
1111 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1112 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1113 .addImm(MB).addImm(ME);
1114 return Result;
1115 }
1116 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001117 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001118 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001119 .addImm(31-Tmp2);
1120 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001121 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001122 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001123 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1124 }
1125 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001126
Nate Begeman5e966612005-03-24 06:28:42 +00001127 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001128 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001129 unsigned SH, MB, ME;
1130 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1131 isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) {
1132 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1133 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1134 .addImm(MB).addImm(ME);
1135 return Result;
1136 }
1137 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001138 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001139 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001140 .addImm(Tmp2).addImm(31);
1141 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001142 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001143 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001144 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1145 }
1146 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001147
Nate Begeman5e966612005-03-24 06:28:42 +00001148 case ISD::SRA:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001149 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +00001150 unsigned SH, MB, ME;
1151 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
1152 isRotateAndMask(ISD::SRA, Tmp2, Tmp3, true, SH, MB, ME)) {
1153 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1154 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
1155 .addImm(MB).addImm(ME);
1156 return Result;
1157 }
1158 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001159 Tmp2 &= 0x1F;
Nate Begeman5e966612005-03-24 06:28:42 +00001160 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1161 } else {
Jim Laskey191cf942005-08-11 21:59:23 +00001162 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +00001163 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001164 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1165 }
1166 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001167
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001168 case ISD::CTLZ:
1169 Tmp1 = SelectExpr(N.getOperand(0));
1170 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1171 return Result;
1172
Nate Begemana9795f82005-03-24 04:41:43 +00001173 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001174 if (!MVT::isInteger(DestType)) {
1175 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1176 N.getOperand(0).Val->hasOneUse()) {
1177 ++FusedFP; // Statistic
1178 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1179 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1180 Tmp3 = SelectExpr(N.getOperand(1));
1181 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1182 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1183 return Result;
1184 }
1185 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1186 N.getOperand(1).Val->hasOneUse()) {
1187 ++FusedFP; // Statistic
1188 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1189 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1190 Tmp3 = SelectExpr(N.getOperand(0));
1191 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1192 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1193 return Result;
1194 }
1195 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 Tmp2 = SelectExpr(N.getOperand(1));
1198 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1199 return Result;
1200 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001201 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
1202 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001203 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001204 Tmp2 = SelectExpr(N.getOperand(1));
1205 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001206 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001207
Nate Begemana9795f82005-03-24 04:41:43 +00001208 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001209 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001210 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1211 unsigned SH, MB, ME;
1212 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1213 unsigned OprOpc;
1214 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1215 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001216 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001217 } else {
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 isRunOfOnes(Tmp2, MB, ME);
1220 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001221 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001222 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1223 .addImm(MB).addImm(ME);
1224 RecordSuccess = true;
1225 return Result;
1226 } else if (isUInt16(Tmp2)) {
1227 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001228 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001229 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001230 RecordSuccess = true;
1231 return Result;
1232 } else if (isUInt16(Tmp2)) {
1233 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001234 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001235 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001236 RecordSuccess = true;
1237 return Result;
1238 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001239 }
Jim Laskey847c3a92005-08-12 23:38:02 +00001240 if (isOprNot(N.getOperand(1))) {
1241 Tmp1 = SelectExpr(N.getOperand(0));
1242 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1243 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1244 RecordSuccess = false;
1245 return Result;
1246 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001247 if (isOprNot(N.getOperand(0))) {
Jim Laskey847c3a92005-08-12 23:38:02 +00001248 Tmp1 = SelectExpr(N.getOperand(1));
1249 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1250 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001251 RecordSuccess = false;
1252 return Result;
1253 }
1254 // emit a regular and
1255 Tmp1 = SelectExpr(N.getOperand(0));
1256 Tmp2 = SelectExpr(N.getOperand(1));
1257 Opc = Recording ? PPC::ANDo : PPC::AND;
1258 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001259 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001260 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001261
Nate Begemana9795f82005-03-24 04:41:43 +00001262 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001263 if (SelectBitfieldInsert(N, Result))
1264 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001265 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1266 return Result;
Jim Laskey847c3a92005-08-12 23:38:02 +00001267 if (isOprNot(N.getOperand(1))) {
1268 Tmp1 = SelectExpr(N.getOperand(0));
1269 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1270 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1271 RecordSuccess = false;
1272 return Result;
1273 }
1274 if (isOprNot(N.getOperand(0))) {
1275 Tmp1 = SelectExpr(N.getOperand(1));
1276 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1277 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1278 RecordSuccess = false;
1279 return Result;
1280 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001281 // emit regular or
1282 Tmp1 = SelectExpr(N.getOperand(0));
1283 Tmp2 = SelectExpr(N.getOperand(1));
1284 Opc = Recording ? PPC::ORo : PPC::OR;
1285 RecordSuccess = true;
1286 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001287 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001288
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001289 case ISD::XOR: {
1290 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001291 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001292 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1293 Tmp2 = SelectExpr(N.getOperand(1));
1294 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1295 return Result;
1296 }
Chris Lattner837a5212005-04-21 21:09:11 +00001297 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001298 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001299 switch(N.getOperand(0).getOpcode()) {
1300 case ISD::OR:
1301 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1302 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1303 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1304 break;
1305 case ISD::AND:
1306 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1307 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1308 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1309 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001310 case ISD::XOR:
1311 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1312 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1313 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1314 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001315 default:
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1318 break;
1319 }
1320 return Result;
1321 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001322 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1323 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001324 // emit regular xor
1325 Tmp1 = SelectExpr(N.getOperand(0));
1326 Tmp2 = SelectExpr(N.getOperand(1));
1327 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001328 return Result;
1329 }
1330
Chris Lattner5b909172005-08-08 21:30:29 +00001331 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001332 if (!MVT::isInteger(DestType)) {
1333 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1334 N.getOperand(0).Val->hasOneUse()) {
1335 ++FusedFP; // Statistic
1336 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1337 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1338 Tmp3 = SelectExpr(N.getOperand(1));
1339 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1340 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1341 return Result;
1342 }
1343 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1344 N.getOperand(1).Val->hasOneUse()) {
1345 ++FusedFP; // Statistic
1346 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1347 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1348 Tmp3 = SelectExpr(N.getOperand(0));
1349 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1350 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1351 return Result;
1352 }
1353 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1354 Tmp1 = SelectExpr(N.getOperand(0));
1355 Tmp2 = SelectExpr(N.getOperand(1));
1356 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1357 return Result;
1358 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001359 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001360 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001361 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001362 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001363 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001364 }
1365 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001366 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001367 Tmp1 = SelectExpr(N.getOperand(0));
1368 Tmp2 = SelectExpr(N.getOperand(1));
1369 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001370 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001371
Nate Begeman5e966612005-03-24 06:28:42 +00001372 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001373 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001374 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001375 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001376 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001377 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001378 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001379 switch (DestType) {
1380 default: assert(0 && "Unknown type to ISD::MUL"); break;
1381 case MVT::i32: Opc = PPC::MULLW; break;
1382 case MVT::f32: Opc = PPC::FMULS; break;
1383 case MVT::f64: Opc = PPC::FMUL; break;
1384 }
1385 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001386 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001387 return Result;
1388
Nate Begeman815d6da2005-04-06 00:25:27 +00001389 case ISD::MULHS:
1390 case ISD::MULHU:
1391 Tmp1 = SelectExpr(N.getOperand(0));
1392 Tmp2 = SelectExpr(N.getOperand(1));
1393 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1394 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1395 return Result;
1396
Nate Begemanf3d08f32005-03-29 00:03:27 +00001397 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001398 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001399 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1400 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001401 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001402 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001403 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1404 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001405 return Result;
1406 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1407 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001408 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001409 Tmp1 = MakeIntReg();
1410 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001411 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1412 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1413 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1414 return Result;
Nate Begeman9f833d32005-04-12 00:10:02 +00001415 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001416 }
1417 // fall thru
1418 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001419 // If this is a divide by constant, we can emit code using some magic
1420 // constants to implement it as a multiply instead.
Chris Lattner59b21c22005-08-09 18:29:55 +00001421 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001422 if (opcode == ISD::SDIV) {
1423 if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) {
1424 ExprMap.erase(N);
1425 return SelectExpr(BuildSDIVSequence(N));
1426 }
1427 } else {
1428 if ((signed)Tmp3 > 1) {
1429 ExprMap.erase(N);
1430 return SelectExpr(BuildUDIVSequence(N));
1431 }
1432 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001433 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001434 Tmp1 = SelectExpr(N.getOperand(0));
1435 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001436 switch (DestType) {
1437 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1438 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1439 case MVT::f32: Opc = PPC::FDIVS; break;
1440 case MVT::f64: Opc = PPC::FDIV; break;
1441 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001442 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1443 return Result;
1444
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001445 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001446 case ISD::SUB_PARTS: {
1447 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1448 "Not an i64 add/sub!");
1449 // Emit all of the operands.
1450 std::vector<unsigned> InVals;
1451 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1452 InVals.push_back(SelectExpr(N.getOperand(i)));
1453 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001454 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1455 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001456 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001457 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1458 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1459 }
1460 return Result+N.ResNo;
1461 }
1462
1463 case ISD::SHL_PARTS:
1464 case ISD::SRA_PARTS:
1465 case ISD::SRL_PARTS: {
1466 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1467 "Not an i64 shift!");
1468 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1469 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001470 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
Chris Lattner54abfc52005-08-11 17:15:31 +00001471 Tmp1 = MakeIntReg();
1472 Tmp2 = MakeIntReg();
1473 Tmp3 = MakeIntReg();
1474 unsigned Tmp4 = MakeIntReg();
1475 unsigned Tmp5 = MakeIntReg();
1476 unsigned Tmp6 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001477 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1478 if (ISD::SHL_PARTS == opcode) {
1479 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1480 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1481 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1482 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001483 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001484 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1485 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1486 } else if (ISD::SRL_PARTS == opcode) {
1487 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1488 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1489 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1490 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1491 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1492 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1493 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1494 } else {
1495 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1496 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1497 MachineBasicBlock *OldMBB = BB;
1498 MachineFunction *F = BB->getParent();
1499 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1500 F->getBasicBlockList().insert(It, TmpMBB);
1501 F->getBasicBlockList().insert(It, PhiMBB);
1502 BB->addSuccessor(TmpMBB);
1503 BB->addSuccessor(PhiMBB);
1504 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1505 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1506 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1507 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1508 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1509 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1510 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1511 // Select correct least significant half if the shift amount > 32
1512 BB = TmpMBB;
Chris Lattner54abfc52005-08-11 17:15:31 +00001513 unsigned Tmp7 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001514 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1515 TmpMBB->addSuccessor(PhiMBB);
1516 BB = PhiMBB;
1517 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1518 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001519 }
1520 return Result+N.ResNo;
1521 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001522
Nate Begeman6b559972005-04-01 02:59:27 +00001523 case ISD::FP_TO_SINT: {
Nate Begeman6b559972005-04-01 02:59:27 +00001524 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman5a014812005-08-14 01:17:16 +00001525 Tmp2 = MakeFPReg();
1526 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1527 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1528 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1529 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1530 return Result;
Nate Begeman6b559972005-04-01 02:59:27 +00001531 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001532
Chris Lattner88ac32c2005-08-09 20:21:10 +00001533 case ISD::SETCC: {
1534 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1535 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
1536 // We can codegen setcc op, imm very efficiently compared to a brcond.
1537 // Check for those cases here.
1538 // setcc op, 0
1539 if (Tmp3 == 0) {
1540 Tmp1 = SelectExpr(Node->getOperand(0));
1541 switch (CC) {
1542 default: Node->dump(); assert(0 && "Unhandled SetCC condition"); abort();
1543 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001544 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001545 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1546 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1547 .addImm(5).addImm(31);
1548 break;
1549 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001550 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001551 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1552 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1553 break;
1554 case ISD::SETLT:
1555 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1556 .addImm(31).addImm(31);
1557 break;
1558 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001559 Tmp2 = MakeIntReg();
1560 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001561 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1562 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1563 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1564 .addImm(31).addImm(31);
1565 break;
Nate Begeman9765c252005-04-12 21:22:28 +00001566 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001567 return Result;
1568 } else if (Tmp3 == ~0U) { // setcc op, -1
1569 Tmp1 = SelectExpr(Node->getOperand(0));
1570 switch (CC) {
1571 default: assert(0 && "Unhandled SetCC condition"); abort();
1572 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001573 Tmp2 = MakeIntReg();
1574 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001575 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
1576 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
1577 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
1578 break;
1579 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001580 Tmp2 = MakeIntReg();
1581 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001582 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1583 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
1584 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
1585 break;
1586 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001587 Tmp2 = MakeIntReg();
1588 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001589 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
1590 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1591 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1592 .addImm(31).addImm(31);
1593 break;
1594 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001595 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001596 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1597 .addImm(31).addImm(31);
1598 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1599 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001600 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001601 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001602 }
Nate Begeman33162522005-03-29 21:54:38 +00001603 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001604
Nate Begemanc24d4842005-08-10 20:52:09 +00001605 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1606 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001607 return Result;
1608 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001609
1610 case ISD::SELECT_CC: {
1611 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
1612 if (!MVT::isInteger(N.getOperand(0).getValueType()) &&
1613 !MVT::isInteger(N.getOperand(2).getValueType()) &&
1614 CC != ISD::SETEQ && CC != ISD::SETNE) {
1615 MVT::ValueType VT = N.getOperand(0).getValueType();
1616 unsigned TV = SelectExpr(N.getOperand(2)); // Use if TRUE
1617 unsigned FV = SelectExpr(N.getOperand(3)); // Use if FALSE
Nate Begemana3fd4002005-07-19 16:51:05 +00001618
Nate Begemanc24d4842005-08-10 20:52:09 +00001619 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001620 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001621 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00001622 default: assert(0 && "Invalid FSEL condition"); abort();
1623 case ISD::SETULT:
1624 case ISD::SETLT:
1625 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1626 case ISD::SETUGE:
1627 case ISD::SETGE:
Nate Begemanc24d4842005-08-10 20:52:09 +00001628 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00001629 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1630 return Result;
1631 case ISD::SETUGT:
1632 case ISD::SETGT:
1633 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
1634 case ISD::SETULE:
1635 case ISD::SETLE: {
Nate Begemanc24d4842005-08-10 20:52:09 +00001636 if (N.getOperand(0).getOpcode() == ISD::FNEG) {
1637 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Nate Begemana3fd4002005-07-19 16:51:05 +00001638 } else {
1639 Tmp2 = MakeReg(VT);
Nate Begemanc24d4842005-08-10 20:52:09 +00001640 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00001641 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1642 }
1643 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1644 return Result;
1645 }
1646 }
1647 } else {
1648 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanc24d4842005-08-10 20:52:09 +00001649 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
1650 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001651 Tmp3 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001652 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00001653 default: assert(0 && "Invalid FSEL condition"); abort();
1654 case ISD::SETULT:
1655 case ISD::SETLT:
1656 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1657 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1658 return Result;
1659 case ISD::SETUGE:
1660 case ISD::SETGE:
1661 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1662 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1663 return Result;
1664 case ISD::SETUGT:
1665 case ISD::SETGT:
1666 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1667 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1668 return Result;
1669 case ISD::SETULE:
1670 case ISD::SETLE:
1671 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1672 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1673 return Result;
1674 }
1675 }
1676 assert(0 && "Should never get here");
Nate Begemana3fd4002005-07-19 16:51:05 +00001677 }
1678
Nate Begeman5a014812005-08-14 01:17:16 +00001679 // If the False value only has one use, we can generate better code by
1680 // selecting it in the fallthrough basic block rather than here, which
1681 // increases register pressure.
Nate Begeman5a014812005-08-14 01:17:16 +00001682 unsigned TrueValue = SelectExpr(N.getOperand(2));
Nate Begeman2d56e722005-08-14 18:38:32 +00001683 unsigned FalseValue = SelectExpr(N.getOperand(3));
Nate Begemanc24d4842005-08-10 20:52:09 +00001684 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1685 Opc = getBCCForSetCC(CC);
1686
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001687 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00001688 // value and the MBB to hold the PHI instruction for this SetCC.
1689 MachineBasicBlock *thisMBB = BB;
1690 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1691 ilist<MachineBasicBlock>::iterator It = BB;
1692 ++It;
1693
1694 // thisMBB:
1695 // ...
1696 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001697 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00001698 // bCC copy1MBB
1699 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001700 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1701 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001702 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001703 MachineFunction *F = BB->getParent();
1704 F->getBasicBlockList().insert(It, copy0MBB);
1705 F->getBasicBlockList().insert(It, sinkMBB);
1706 // Update machine-CFG edges
1707 BB->addSuccessor(copy0MBB);
1708 BB->addSuccessor(sinkMBB);
1709
1710 // copy0MBB:
1711 // %FalseValue = ...
1712 // # fallthrough to sinkMBB
1713 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001714 // Update machine-CFG edges
1715 BB->addSuccessor(sinkMBB);
1716
1717 // sinkMBB:
1718 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1719 // ...
1720 BB = sinkMBB;
1721 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1722 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001723 return Result;
1724 }
Nate Begemana9795f82005-03-24 04:41:43 +00001725
1726 case ISD::Constant:
1727 switch (N.getValueType()) {
1728 default: assert(0 && "Cannot use constants of this type!");
1729 case MVT::i1:
1730 BuildMI(BB, PPC::LI, 1, Result)
1731 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1732 break;
1733 case MVT::i32:
1734 {
1735 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1736 if (v < 32768 && v >= -32768) {
1737 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1738 } else {
Chris Lattner54abfc52005-08-11 17:15:31 +00001739 Tmp1 = MakeIntReg();
Nate Begeman5e966612005-03-24 06:28:42 +00001740 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1741 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001742 }
1743 }
1744 }
1745 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00001746
1747 case ISD::ConstantFP: {
1748 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
1749 Result = getConstDouble(CN->getValue(), Result);
1750 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001751 }
1752
Nate Begemana3fd4002005-07-19 16:51:05 +00001753 case ISD::FNEG:
1754 if (!NoExcessFPPrecision &&
1755 ISD::ADD == N.getOperand(0).getOpcode() &&
1756 N.getOperand(0).Val->hasOneUse() &&
1757 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1758 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
1759 ++FusedFP; // Statistic
1760 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1761 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1762 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1763 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1764 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1765 } else if (!NoExcessFPPrecision &&
1766 ISD::ADD == N.getOperand(0).getOpcode() &&
1767 N.getOperand(0).Val->hasOneUse() &&
1768 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1769 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
1770 ++FusedFP; // Statistic
1771 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1772 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1773 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1774 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1775 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1776 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
1777 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1778 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1779 } else {
1780 Tmp1 = SelectExpr(N.getOperand(0));
1781 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1782 }
1783 return Result;
1784
1785 case ISD::FABS:
1786 Tmp1 = SelectExpr(N.getOperand(0));
1787 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1788 return Result;
1789
Nate Begemanadeb43d2005-07-20 22:42:00 +00001790 case ISD::FSQRT:
1791 Tmp1 = SelectExpr(N.getOperand(0));
1792 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
1793 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1794 return Result;
1795
Nate Begemana3fd4002005-07-19 16:51:05 +00001796 case ISD::FP_ROUND:
1797 assert (DestType == MVT::f32 &&
1798 N.getOperand(0).getValueType() == MVT::f64 &&
1799 "only f64 to f32 conversion supported here");
1800 Tmp1 = SelectExpr(N.getOperand(0));
1801 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1802 return Result;
1803
1804 case ISD::FP_EXTEND:
1805 assert (DestType == MVT::f64 &&
1806 N.getOperand(0).getValueType() == MVT::f32 &&
1807 "only f32 to f64 conversion supported here");
1808 Tmp1 = SelectExpr(N.getOperand(0));
1809 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1810 return Result;
1811
1812 case ISD::UINT_TO_FP:
1813 case ISD::SINT_TO_FP: {
1814 assert (N.getOperand(0).getValueType() == MVT::i32
1815 && "int to float must operate on i32");
1816 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1817 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Chris Lattner54abfc52005-08-11 17:15:31 +00001818 Tmp2 = MakeFPReg(); // temp reg to load the integer value into
1819 Tmp3 = MakeIntReg(); // temp reg to hold the conversion constant
Nate Begemana3fd4002005-07-19 16:51:05 +00001820
1821 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1822 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jim Laskeyea0617a2005-08-15 17:35:26 +00001823 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
Nate Begemana3fd4002005-07-19 16:51:05 +00001824
1825 if (IsUnsigned) {
1826 unsigned ConstF = getConstDouble(0x1.000000p52);
1827 // Store the hi & low halves of the fp value, currently in int regs
1828 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1829 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1830 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1831 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1832 // Generate the return value with a subtract
Jim Laskeyea0617a2005-08-15 17:35:26 +00001833 BuildMI(BB, Opc, 2, Result).addReg(Tmp2).addReg(ConstF);
Nate Begemana3fd4002005-07-19 16:51:05 +00001834 } else {
1835 unsigned ConstF = getConstDouble(0x1.000008p52);
Chris Lattner54abfc52005-08-11 17:15:31 +00001836 unsigned TmpL = MakeIntReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001837 // Store the hi & low halves of the fp value, currently in int regs
1838 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1839 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1840 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1841 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1842 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1843 // Generate the return value with a subtract
Jim Laskeyea0617a2005-08-15 17:35:26 +00001844 BuildMI(BB, Opc, 2, Result).addReg(Tmp2).addReg(ConstF);
Nate Begemana3fd4002005-07-19 16:51:05 +00001845 }
1846 return Result;
1847 }
1848 }
Nate Begemana9795f82005-03-24 04:41:43 +00001849 return 0;
1850}
1851
1852void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00001853 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00001854 unsigned opcode = N.getOpcode();
1855
1856 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1857 return; // Already selected.
1858
1859 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001860
Nate Begemana9795f82005-03-24 04:41:43 +00001861 switch (Node->getOpcode()) {
1862 default:
1863 Node->dump(); std::cerr << "\n";
1864 assert(0 && "Node not handled yet!");
1865 case ISD::EntryToken: return; // Noop
1866 case ISD::TokenFactor:
1867 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1868 Select(Node->getOperand(i));
1869 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001870 case ISD::CALLSEQ_START:
1871 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00001872 Select(N.getOperand(0));
1873 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001874 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00001875 PPC::ADJCALLSTACKUP;
1876 BuildMI(BB, Opc, 1).addImm(Tmp1);
1877 return;
1878 case ISD::BR: {
1879 MachineBasicBlock *Dest =
1880 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001881 Select(N.getOperand(0));
1882 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1883 return;
1884 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001885 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00001886 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00001887 SelectBranchCC(N);
1888 return;
1889 case ISD::CopyToReg:
1890 Select(N.getOperand(0));
1891 Tmp1 = SelectExpr(N.getOperand(1));
1892 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001893
Nate Begemana9795f82005-03-24 04:41:43 +00001894 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001895 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00001896 N.getOperand(1).getValueType() == MVT::f32)
1897 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1898 else
1899 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1900 }
1901 return;
1902 case ISD::ImplicitDef:
1903 Select(N.getOperand(0));
1904 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1905 return;
1906 case ISD::RET:
1907 switch (N.getNumOperands()) {
1908 default:
1909 assert(0 && "Unknown return instruction!");
1910 case 3:
1911 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1912 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001913 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00001914 Select(N.getOperand(0));
1915 Tmp1 = SelectExpr(N.getOperand(1));
1916 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001917 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1918 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001919 break;
1920 case 2:
1921 Select(N.getOperand(0));
1922 Tmp1 = SelectExpr(N.getOperand(1));
1923 switch (N.getOperand(1).getValueType()) {
1924 default:
1925 assert(0 && "Unknown return type!");
1926 case MVT::f64:
1927 case MVT::f32:
1928 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1929 break;
1930 case MVT::i32:
1931 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1932 break;
1933 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001934 case 1:
1935 Select(N.getOperand(0));
1936 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001937 }
1938 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1939 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001940 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00001941 case ISD::STORE: {
1942 SDOperand Chain = N.getOperand(0);
1943 SDOperand Value = N.getOperand(1);
1944 SDOperand Address = N.getOperand(2);
1945 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00001946
Nate Begeman2497e632005-07-21 20:44:43 +00001947 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00001948
Nate Begeman2497e632005-07-21 20:44:43 +00001949 if (opcode == ISD::STORE) {
1950 switch(Value.getValueType()) {
1951 default: assert(0 && "unknown Type in store");
1952 case MVT::i32: Opc = PPC::STW; break;
1953 case MVT::f64: Opc = PPC::STFD; break;
1954 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001955 }
Nate Begeman2497e632005-07-21 20:44:43 +00001956 } else { //ISD::TRUNCSTORE
1957 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1958 default: assert(0 && "unknown Type in store");
1959 case MVT::i1:
1960 case MVT::i8: Opc = PPC::STB; break;
1961 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001962 }
Nate Begemana9795f82005-03-24 04:41:43 +00001963 }
Nate Begeman2497e632005-07-21 20:44:43 +00001964
1965 if(Address.getOpcode() == ISD::FrameIndex) {
1966 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1967 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00001968 } else {
1969 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001970 switch(SelectAddr(Address, Tmp2, offset)) {
1971 default: assert(0 && "Unhandled return value from SelectAddr");
1972 case 0: // imm offset, no frame, no index
1973 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
1974 break;
1975 case 1: // imm offset + frame index
1976 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
1977 break;
1978 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00001979 Opc = IndexedOpForOp(Opc);
1980 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001981 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001982 case 3: {
1983 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1984 GlobalValue *GV = GN->getGlobal();
1985 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
1986 }
Nate Begeman2497e632005-07-21 20:44:43 +00001987 }
1988 }
1989 return;
1990 }
Nate Begemana9795f82005-03-24 04:41:43 +00001991 case ISD::EXTLOAD:
1992 case ISD::SEXTLOAD:
1993 case ISD::ZEXTLOAD:
1994 case ISD::LOAD:
1995 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001996 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00001997 case ISD::CALL:
1998 case ISD::DYNAMIC_STACKALLOC:
1999 ExprMap.erase(N);
2000 SelectExpr(N);
2001 return;
2002 }
2003 assert(0 && "Should not be reached!");
2004}
2005
2006
2007/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2008/// into a machine code representation using pattern matching and a machine
2009/// description file.
2010///
2011FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002012 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002013}
2014