blob: 1a9d09c64bc9b638b6af265348b95a7af3ec9d1c [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
Chris Lattner26689592005-10-14 23:51:18 +000016#include "PPC.h"
Chris Lattner26bd0d42005-10-14 23:45:43 +000017#include "PPCInstrBuilder.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
19#include "PPCISelLowering.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000020#include "llvm/Constants.h"
Nate Begemana9795f82005-03-24 04:41:43 +000021#include "llvm/Function.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begemana9795f82005-03-24 04:41:43 +000023#include "llvm/CodeGen/MachineFunction.h"
Nate Begemana9795f82005-03-24 04:41:43 +000024#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetData.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000028#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
33#include <algorithm>
34using namespace llvm;
35
Nate Begemana9795f82005-03-24 04:41:43 +000036namespace {
Chris Lattner6d9aed42005-08-17 01:25:14 +000037Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted");
38Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations");
39Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Chris Lattner3c304a32005-08-05 22:05:03 +000040
Nate Begemana9795f82005-03-24 04:41:43 +000041//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000042// ISel - PPC32 specific code to select PPC32 machine instructions for
43// SelectionDAG operations.
Nate Begemana9795f82005-03-24 04:41:43 +000044//===--------------------------------------------------------------------===//
Chris Lattner6d9aed42005-08-17 01:25:14 +000045
Nate Begemana9795f82005-03-24 04:41:43 +000046class ISel : public SelectionDAGISel {
Nate Begeman21e463b2005-10-16 05:39:50 +000047 PPCTargetLowering PPCLowering;
Nate Begeman815d6da2005-04-06 00:25:27 +000048 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
49 // for sdiv and udiv until it is put into the future
50 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000051
Nate Begemana9795f82005-03-24 04:41:43 +000052 /// ExprMap - As shared expressions are codegen'd, we keep track of which
53 /// vreg the value is produced in, so we only emit one copy of each compiled
54 /// tree.
55 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +000056
57 unsigned GlobalBaseReg;
58 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +000059 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +000060public:
Nate Begeman21e463b2005-10-16 05:39:50 +000061 ISel(TargetMachine &TM) : SelectionDAGISel(PPCLowering), PPCLowering(TM),
Nate Begeman815d6da2005-04-06 00:25:27 +000062 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000063
Nate Begemanc7b09f12005-03-25 08:34:25 +000064 /// runOnFunction - Override this function in order to reset our per-function
65 /// variables.
66 virtual bool runOnFunction(Function &Fn) {
67 // Make sure we re-emit a set of the global base reg if necessary
68 GlobalBaseInitialized = false;
69 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000070 }
71
Nate Begemana9795f82005-03-24 04:41:43 +000072 /// InstructionSelectBasicBlock - This callback is invoked by
73 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
74 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
75 DEBUG(BB->dump());
76 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +000077 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +000078 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +000079
Nate Begemana9795f82005-03-24 04:41:43 +000080 // Clear state used for selection.
81 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +000082 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +000083 }
Nate Begeman815d6da2005-04-06 00:25:27 +000084
Chris Lattner54abfc52005-08-11 17:15:31 +000085 // convenience functions for virtual register creation
86 inline unsigned MakeIntReg() {
Nate Begeman1d9d7422005-10-18 00:28:58 +000087 return RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
Chris Lattner54abfc52005-08-11 17:15:31 +000088 }
Chris Lattner54abfc52005-08-11 17:15:31 +000089
Nate Begeman815d6da2005-04-06 00:25:27 +000090 // dag -> dag expanders for integer divide by constant
91 SDOperand BuildSDIVSequence(SDOperand N);
92 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000093
Nate Begemandffcfcc2005-04-01 00:32:34 +000094 unsigned getGlobalBaseReg();
Nate Begemanc24d4842005-08-10 20:52:09 +000095 void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +000096 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +000097 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begemanc24d4842005-08-10 20:52:09 +000098 unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
Chris Lattnerb4138c42005-08-10 18:11:33 +000099 bool SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000100 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000101 bool IsArithmetic = false, bool Negate = false);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000102 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000103 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000104
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000105 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000106 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000107
108 virtual const char *getPassName() const {
109 return "PowerPC Pattern Instruction Selection";
110 }
Nate Begemana9795f82005-03-24 04:41:43 +0000111};
112
Chris Lattner02efa6c2005-08-08 21:08:09 +0000113// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
114// any number of 0s on either side. The 1s are allowed to wrap from LSB to
115// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
116// not, since all 1s are not contiguous.
117static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
118 if (isShiftedMask_32(Val)) {
119 // look for the first non-zero bit
120 MB = CountLeadingZeros_32(Val);
121 // look for the first zero bit after the run of ones
122 ME = CountLeadingZeros_32((Val - 1) ^ Val);
123 return true;
124 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
125 // effectively look for the first zero bit
126 ME = CountLeadingZeros_32(Val) - 1;
127 // effectively look for the first one bit after the run of zeros
128 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
129 return true;
130 }
131 // no run present
132 return false;
133}
134
Chris Lattnercf1cf182005-08-08 21:10:27 +0000135// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
136// and mask opcode and mask operation.
137static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
138 bool IsShiftMask,
139 unsigned &SH, unsigned &MB, unsigned &ME) {
140 if (Shift > 31) return false;
141 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
142
143 if (Opcode == ISD::SHL) { // shift left
144 // apply shift to mask if it comes first
145 if (IsShiftMask) Mask = Mask << Shift;
146 // determine which bits are made indeterminant by shift
147 Indeterminant = ~(0xFFFFFFFFu << Shift);
Chris Lattnerd2424192005-10-15 19:04:48 +0000148 } else if (Opcode == ISD::SRL) { // shift rights
Chris Lattnercf1cf182005-08-08 21:10:27 +0000149 // 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 // adjust for the left rotate
154 Shift = 32 - Shift;
155 }
156
157 // if the mask doesn't intersect any Indeterminant bits
Jim Laskeycf083e32005-08-12 23:52:46 +0000158 if (Mask && !(Mask & Indeterminant)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000159 SH = Shift;
160 // make sure the mask is still a mask (wrap arounds may not be)
161 return isRunOfOnes(Mask, MB, ME);
162 }
163
164 // can't do it
165 return false;
166}
167
Chris Lattner59b21c22005-08-09 18:29:55 +0000168// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000169// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000170static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000171 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000172 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000173 // retrieve value
Jim Laskeyb454cfd2005-08-18 00:15:15 +0000174 Imm = (unsigned)CN->getValue();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000175 // passes muster
176 return true;
177 }
178 // not a constant
179 return false;
180}
181
Jim Laskey191cf942005-08-11 21:59:23 +0000182// isOpcWithIntImmediate - This method tests to see if the node is a specific
183// opcode and that it has a immediate integer right operand.
184// If so Imm will receive the 32 bit value.
185static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) {
186 return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm);
187}
188
Chris Lattnercf1cf182005-08-08 21:10:27 +0000189// isOprShiftImm - Returns true if the specified operand is a shift opcode with
190// a immediate shift count less than 32.
191static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
192 Opc = N.getOpcode();
193 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000194 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000195}
196
197// isOprNot - Returns true if the specified operand is an xor with immediate -1.
198static bool isOprNot(SDOperand N) {
199 unsigned Imm;
Jim Laskey191cf942005-08-11 21:59:23 +0000200 return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000201}
202
203// Immediate constant composers.
204// Lo16 - grabs the lo 16 bits from a 32 bit constant.
205// Hi16 - grabs the hi 16 bits from a 32 bit constant.
206// HA16 - computes the hi bits required if the lo bits are add/subtracted in
207// arithmethically.
208static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
209static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
210static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
211
Nate Begemanc7bd4822005-04-11 06:34:10 +0000212/// NodeHasRecordingVariant - If SelectExpr can always produce code for
213/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
214/// return false.
215static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
216 switch(NodeOpcode) {
217 default: return false;
218 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000220 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000221 }
222}
223
Nate Begeman3e897162005-03-31 23:55:40 +0000224/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
Nate Begemanc24d4842005-08-10 20:52:09 +0000225/// to Condition.
226static unsigned getBCCForSetCC(ISD::CondCode CC) {
227 switch (CC) {
Nate Begeman3e897162005-03-31 23:55:40 +0000228 default: assert(0 && "Unknown condition!"); abort();
229 case ISD::SETEQ: return PPC::BEQ;
230 case ISD::SETNE: return PPC::BNE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000231 case ISD::SETULT:
Nate Begeman3e897162005-03-31 23:55:40 +0000232 case ISD::SETLT: return PPC::BLT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000233 case ISD::SETULE:
Nate Begeman3e897162005-03-31 23:55:40 +0000234 case ISD::SETLE: return PPC::BLE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000235 case ISD::SETUGT:
Nate Begeman3e897162005-03-31 23:55:40 +0000236 case ISD::SETGT: return PPC::BGT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000237 case ISD::SETUGE:
Nate Begeman3e897162005-03-31 23:55:40 +0000238 case ISD::SETGE: return PPC::BGE;
239 }
Nate Begeman04730362005-04-01 04:45:11 +0000240 return 0;
241}
242
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000243/// getCRIdxForSetCC - Return the index of the condition register field
244/// associated with the SetCC condition, and whether or not the field is
245/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000246static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
247 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000248 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000249 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000250 case ISD::SETLT: Inv = false; return 0;
251 case ISD::SETUGE:
252 case ISD::SETGE: Inv = true; return 0;
253 case ISD::SETUGT:
254 case ISD::SETGT: Inv = false; return 1;
255 case ISD::SETULE:
256 case ISD::SETLE: Inv = true; return 1;
257 case ISD::SETEQ: Inv = false; return 2;
258 case ISD::SETNE: Inv = true; return 2;
259 }
260 return 0;
261}
262
Nate Begeman04730362005-04-01 04:45:11 +0000263/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
264/// and store immediate instructions.
265static unsigned IndexedOpForOp(unsigned Opcode) {
266 switch(Opcode) {
267 default: assert(0 && "Unknown opcode!"); abort();
268 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
269 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
270 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
271 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
272 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
273 case PPC::LFD: return PPC::LFDX;
274 }
275 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000276}
Nate Begemana9795f82005-03-24 04:41:43 +0000277}
278
Nate Begemanc7b09f12005-03-25 08:34:25 +0000279/// getGlobalBaseReg - Output the instructions required to put the
280/// base address to use for accessing globals into a register.
281///
282unsigned ISel::getGlobalBaseReg() {
283 if (!GlobalBaseInitialized) {
284 // Insert the set of GlobalBaseReg into the first MBB of the function
285 MachineBasicBlock &FirstMBB = BB->getParent()->front();
286 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000287 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000288 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
Chris Lattner3f852b42005-08-18 23:24:50 +0000289 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000290 GlobalBaseInitialized = true;
291 }
292 return GlobalBaseReg;
293}
294
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000295/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000296/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000297void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
298 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000299 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000300 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000301 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000302 bool GPOpt =
303 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
Nate Begeman27d53ba2005-08-19 03:42:28 +0000304 if (GPOpt)
305 BuildMI(BB, PPC::MFOCRF, 1, IntCR).addReg(PPC::CR7);
306 else
307 BuildMI(BB, PPC::MFCR, 0, IntCR);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000308 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000309 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000310 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
311 .addImm(31).addImm(31);
312 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
313 } else {
314 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
315 .addImm(31).addImm(31);
316 }
317}
318
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000319/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000320/// the rotate left word immediate then mask insert (rlwimi) instruction.
321/// Returns true on success, false if the caller still needs to select OR.
322///
323/// Patterns matched:
324/// 1. or shl, and 5. or and, and
325/// 2. or and, shl 6. or shl, shr
326/// 3. or shr, and 7. or shr, shl
327/// 4. or and, shr
328bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000329 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000330 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000331 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000332
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000333 SDOperand Op0 = OR.getOperand(0);
334 SDOperand Op1 = OR.getOperand(1);
335
336 unsigned Op0Opc = Op0.getOpcode();
337 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000338
Nate Begeman7ddecb42005-04-06 23:51:40 +0000339 // Verify that we have the correct opcodes
340 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
341 return false;
342 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
343 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000344
Nate Begeman7ddecb42005-04-06 23:51:40 +0000345 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +0000346 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000347 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000348 case ISD::SHL: TgtMask <<= Value; break;
349 case ISD::SRL: TgtMask >>= Value; break;
350 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000351 }
352 } else {
353 return false;
354 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000355
Nate Begeman7ddecb42005-04-06 23:51:40 +0000356 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +0000357 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000358 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000359 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000360 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000361 InsMask <<= Amount;
362 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000363 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000364 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000365 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000366 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000367 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000368 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000369 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000370 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000371 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000372 break;
373 }
374 } else {
375 return false;
376 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000377
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000378 unsigned Tmp3 = 0;
379
380 // If both of the inputs are ANDs and one of them has a logical shift by
381 // constant as its input, make that the inserted value so that we can combine
382 // the shift into the rotate part of the rlwimi instruction
383 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000384 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000385 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000386 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000387 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000388 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000389 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
390 }
391 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
392 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +0000393 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000394 std::swap(Op0, Op1);
395 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +0000396 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +0000397 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000398 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
399 }
400 }
401 }
402
Nate Begeman7ddecb42005-04-06 23:51:40 +0000403 // Verify that the Target mask and Insert mask together form a full word mask
404 // and that the Insert mask is a run of set bits (which implies both are runs
405 // of set bits). Given that, Select the arguments and generate the rlwimi
406 // instruction.
407 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +0000408 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000409 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000410 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000411 // Check for rotlwi / rotrwi here, a special case of bitfield insert
412 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000413 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +0000414 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000415 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
416 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
417 .addImm(0).addImm(31);
418 return true;
419 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000420 if (Op0Opc == ISD::AND && fullMask)
421 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000422 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000423 Tmp1 = SelectExpr(Op0);
424 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +0000425 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
426 .addImm(Amount).addImm(MB).addImm(ME);
427 return true;
428 }
429 return false;
430}
431
Nate Begeman3664cef2005-04-13 22:14:14 +0000432/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
433/// low six bits. If the shift amount is an ISD::AND node with a mask that is
434/// wider than the implicit mask, then we can get rid of the AND and let the
435/// shift do the mask.
436unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Jim Laskey191cf942005-08-11 21:59:23 +0000437 unsigned C;
438 if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63)
Nate Begeman3664cef2005-04-13 22:14:14 +0000439 return SelectExpr(N.getOperand(0));
440 else
441 return SelectExpr(N);
442}
443
Nate Begemanc24d4842005-08-10 20:52:09 +0000444unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000445 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000446 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000447
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000448 // Allocate a condition register for this expression
Nate Begeman1d9d7422005-10-18 00:28:58 +0000449 Result = RegMap->createVirtualRegister(PPC::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000450
Nate Begemanc24d4842005-08-10 20:52:09 +0000451 // Use U to determine whether the SETCC immediate range is signed or not.
452 bool U = ISD::isUnsignedIntSetCC(CC);
453 if (isIntImmediate(RHS, Tmp2) &&
454 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
455 Tmp2 = Lo16(Tmp2);
456 // For comparisons against zero, we can implicity set CR0 if a recording
457 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
458 // operand zero of the SetCC node is available.
459 if (Tmp2 == 0 &&
460 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
461 RecordSuccess = false;
462 Tmp1 = SelectExpr(LHS, true);
463 if (RecordSuccess) {
464 ++Recorded;
465 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
466 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000467 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000468 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000469 }
Nate Begemanc24d4842005-08-10 20:52:09 +0000470 // If we could not implicitly set CR0, then emit a compare immediate
471 // instead.
472 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
473 if (U)
474 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
475 else
476 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000477 } else {
Chris Lattner919c0322005-10-01 01:35:02 +0000478 unsigned CompareOpc;
479 if (MVT::isInteger(LHS.getValueType()))
480 CompareOpc = U ? PPC::CMPLW : PPC::CMPW;
481 else if (LHS.getValueType() == MVT::f32)
482 CompareOpc = PPC::FCMPUS;
483 else
484 CompareOpc = PPC::FCMPUD;
Nate Begemanc24d4842005-08-10 20:52:09 +0000485 Tmp1 = SelectExpr(LHS);
486 Tmp2 = SelectExpr(RHS);
487 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000488 }
489 return Result;
490}
491
Nate Begemand3ded2d2005-08-08 22:22:56 +0000492/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000493unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000494{
Nate Begeman96fc6812005-03-31 02:05:53 +0000495 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000496 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000497 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +0000498 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +0000499 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000500 if (isFrame) {
501 ++FrameOff;
502 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
503 return 1;
504 } else {
505 Reg = SelectExpr(N.getOperand(0));
506 return 0;
507 }
508 } else {
509 Reg = SelectExpr(N.getOperand(0));
510 offset = SelectExpr(N.getOperand(1));
511 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000512 }
Nate Begeman04730362005-04-01 04:45:11 +0000513 }
Nate Begemand3ded2d2005-08-08 22:22:56 +0000514 // Now check if we're dealing with a global, and whether or not we should emit
515 // an optimized load or store for statics.
516 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
517 GlobalValue *GV = GN->getGlobal();
518 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000519 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +0000520 if (PICEnabled)
521 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
522 .addGlobalAddress(GV);
523 else
524 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
525 Reg = GlobalHi;
526 offset = 0;
527 return 3;
528 }
529 }
Nate Begemana9795f82005-03-24 04:41:43 +0000530 Reg = SelectExpr(N);
531 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000532 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000533}
534
535void ISel::SelectBranchCC(SDOperand N)
536{
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000537 MachineBasicBlock *Dest =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000538 cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000539
Nate Begemana9795f82005-03-24 04:41:43 +0000540 Select(N.getOperand(0)); //chain
Nate Begeman7cbd5252005-08-16 19:49:35 +0000541 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get();
542 unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC);
Nate Begemanc24d4842005-08-10 20:52:09 +0000543 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000544
Nate Begemancd08e4c2005-04-09 20:09:12 +0000545 // If this is a two way branch, then grab the fallthrough basic block argument
546 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
547 // if necessary by the branch selection pass. Otherwise, emit a standard
548 // conditional branch.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000549 if (N.getOpcode() == ISD::BRTWOWAY_CC) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000550 MachineBasicBlock *Fallthrough =
Nate Begeman7cbd5252005-08-16 19:49:35 +0000551 cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock();
Chris Lattnerf913d3f2005-08-21 19:03:28 +0000552 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
553 .addMBB(Dest).addMBB(Fallthrough);
554 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000555 } else {
Chris Lattnerf913d3f2005-08-21 19:03:28 +0000556 // Iterate to the next basic block
557 ilist<MachineBasicBlock>::iterator It = BB;
558 ++It;
559
Nate Begeman439009c2005-06-15 18:22:43 +0000560 // If the fallthrough path is off the end of the function, which would be
561 // undefined behavior, set it to be the same as the current block because
562 // we have nothing better to set it to, and leaving it alone will cause the
563 // PowerPC Branch Selection pass to crash.
564 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000565 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +0000566 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +0000567 }
Nate Begemana9795f82005-03-24 04:41:43 +0000568 return;
569}
570
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000571// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +0000572bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000573 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000574 bool IsArithmetic, bool Negate) {
575 // check constant
576 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
577 // exit if not a constant
578 if (!CN) return false;
579 // extract immediate
Chris Lattner6d9aed42005-08-17 01:25:14 +0000580 unsigned C = (unsigned)CN->getValue();
Chris Lattnerb4138c42005-08-10 18:11:33 +0000581 // negate if required (ISD::SUB)
582 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000583 // get the hi and lo portions of constant
584 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
585 unsigned Lo = Lo16(C);
586 // assume no intermediate result from lo instruction (same as final result)
587 unsigned Tmp = Result;
588 // check if two instructions are needed
589 if (Hi && Lo) {
590 // exit if usage indicates it would be better to load immediate into a
591 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +0000592 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000593 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +0000594 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000595 }
596 // get first operand
597 unsigned Opr0 = SelectExpr(N.getOperand(0));
598 // is a lo instruction needed
599 if (Lo) {
Chris Lattner6d9aed42005-08-17 01:25:14 +0000600 // generate instruction for lo portion
601 BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000602 // need to switch out first operand for hi instruction
603 Opr0 = Tmp;
604 }
Chris Lattner6d9aed42005-08-17 01:25:14 +0000605 // is a hi instruction needed
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000606 if (Hi) {
607 // generate instruction for hi portion
Chris Lattner6d9aed42005-08-17 01:25:14 +0000608 BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000609 }
610 return true;
611}
612
Nate Begemanc7bd4822005-04-11 06:34:10 +0000613unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +0000614 unsigned Result;
615 unsigned Tmp1, Tmp2, Tmp3;
616 unsigned Opc = 0;
617 unsigned opcode = N.getOpcode();
618
619 SDNode *Node = N.Val;
620 MVT::ValueType DestType = N.getValueType();
621
Chris Lattnera8cd0152005-08-16 21:58:15 +0000622 if (Node->getOpcode() == ISD::CopyFromReg) {
623 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana43b1762005-06-14 03:55:23 +0000624 // Just use the specified register as our input.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000625 if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1)
626 return Reg;
627 }
Nate Begemana43b1762005-06-14 03:55:23 +0000628
Nate Begemana9795f82005-03-24 04:41:43 +0000629 unsigned &Reg = ExprMap[N];
630 if (Reg) return Reg;
631
Nate Begeman27eeb002005-04-02 05:59:34 +0000632 switch (N.getOpcode()) {
633 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000634 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000635 MakeReg(N.getValueType()) : 1;
636 break;
Chris Lattner5dd7fea2005-08-31 17:48:04 +0000637 case ISD::AssertSext:
638 case ISD::AssertZext:
639 // Don't allocate a vreg for these nodes.
640 return Reg = SelectExpr(N.getOperand(0));
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000641 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +0000642 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000643 // If this is a call instruction, make sure to prepare ALL of the result
644 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000645 if (Node->getNumValues() == 1)
646 Reg = Result = 1; // Void call, just a chain.
647 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000648 Result = MakeReg(Node->getValueType(0));
649 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000650 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000651 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000652 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000653 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000654 break;
655 case ISD::ADD_PARTS:
656 case ISD::SUB_PARTS:
Nate Begeman27eeb002005-04-02 05:59:34 +0000657 Result = MakeReg(Node->getValueType(0));
658 ExprMap[N.getValue(0)] = Result;
659 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
660 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
661 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000662 }
663
Nate Begemana9795f82005-03-24 04:41:43 +0000664 switch (opcode) {
665 default:
Nate Begeman5a014812005-08-14 01:17:16 +0000666 Node->dump(); std::cerr << '\n';
667 assert(0 && "Node not handled!\n");
Chris Lattner0bbea952005-08-26 20:25:03 +0000668 case PPCISD::FSEL:
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000669 Tmp1 = SelectExpr(N.getOperand(0));
670 Tmp2 = SelectExpr(N.getOperand(1));
671 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner43f07a42005-10-02 07:07:49 +0000672
673 // Extend the comparison to 64-bits if needed.
674 if (N.getOperand(0).getValueType() == MVT::f32) {
675 unsigned Tmp1New = MakeReg(MVT::f64);
676 BuildMI(BB, PPC::FMRSD, 1, Tmp1New).addReg(Tmp1);
677 Tmp1 = Tmp1New;
678 }
679
680 Opc = N.Val->getValueType(0) == MVT::f32 ? PPC::FSELS : PPC::FSELD;
Chris Lattner867940d2005-10-02 06:58:23 +0000681 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000682 return Result;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000683 case PPCISD::FCFID:
684 Tmp1 = SelectExpr(N.getOperand(0));
685 BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp1);
686 return Result;
687 case PPCISD::FCTIDZ:
688 Tmp1 = SelectExpr(N.getOperand(0));
689 BuildMI(BB, PPC::FCTIDZ, 1, Result).addReg(Tmp1);
690 return Result;
Chris Lattnerf7605322005-08-31 21:09:52 +0000691 case PPCISD::FCTIWZ:
692 Tmp1 = SelectExpr(N.getOperand(0));
693 BuildMI(BB, PPC::FCTIWZ, 1, Result).addReg(Tmp1);
694 return Result;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000695 case ISD::UNDEF:
Chris Lattner2b544002005-08-24 23:08:16 +0000696 if (Node->getValueType(0) == MVT::i32)
697 BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Result);
Chris Lattner919c0322005-10-01 01:35:02 +0000698 else if (Node->getValueType(0) == MVT::f32)
699 BuildMI(BB, PPC::IMPLICIT_DEF_F4, 0, Result);
Chris Lattner2b544002005-08-24 23:08:16 +0000700 else
Chris Lattner919c0322005-10-01 01:35:02 +0000701 BuildMI(BB, PPC::IMPLICIT_DEF_F8, 0, Result);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000702 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000703 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000704 // Generate both result values. FIXME: Need a better commment here?
705 if (Result != 1)
706 ExprMap[N.getValue(1)] = 1;
707 else
708 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
709
710 // FIXME: We are currently ignoring the requested alignment for handling
711 // greater than the stack alignment. This will need to be revisited at some
712 // point. Align = N.getOperand(2);
713 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
714 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
715 std::cerr << "Cannot allocate stack object with greater alignment than"
716 << " the stack alignment yet!";
717 abort();
718 }
719 Select(N.getOperand(0));
720 Tmp1 = SelectExpr(N.getOperand(1));
721 // Subtract size from stack pointer, thereby allocating some space.
722 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
723 // Put a pointer to the space into the result register by copying the SP
Nate Begeman1d9d7422005-10-18 00:28:58 +0000724 BuildMI(BB, PPC::OR4, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
Nate Begeman5e966612005-03-24 06:28:42 +0000725 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000726
727 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000728 Tmp1 = BB->getParent()->getConstantPool()->
729 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner54abfc52005-08-11 17:15:31 +0000730 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000731 if (PICEnabled)
732 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
733 .addConstantPoolIndex(Tmp1);
734 else
735 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000736 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
737 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000738
739 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000740 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000741 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000742 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000743
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000744 case ISD::GlobalAddress: {
745 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +0000746 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +0000747 if (PICEnabled)
748 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
749 .addGlobalAddress(GV);
750 else
Chris Lattner4015ea82005-07-28 04:42:11 +0000751 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000752 if (GV->hasWeakLinkage() || GV->isExternal()) {
753 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
754 } else {
755 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
756 }
757 return Result;
758 }
759
Nate Begeman5e966612005-03-24 06:28:42 +0000760 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000761 case ISD::EXTLOAD:
762 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000763 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000764 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000765 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +0000766 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000767
Nate Begeman5e966612005-03-24 06:28:42 +0000768 // Make sure we generate both values.
769 if (Result != 1)
770 ExprMap[N.getValue(1)] = 1; // Generate the token
771 else
772 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
773
774 SDOperand Chain = N.getOperand(0);
775 SDOperand Address = N.getOperand(1);
776 Select(Chain);
777
Nate Begeman9db505c2005-03-28 19:36:43 +0000778 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000779 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000780 case MVT::i1: Opc = PPC::LBZ; break;
781 case MVT::i8: Opc = PPC::LBZ; break;
782 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
783 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000784 case MVT::f32: Opc = PPC::LFS; break;
785 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000786 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000787
Nate Begeman74d73452005-03-31 00:15:26 +0000788 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000789 Tmp1 = MakeIntReg();
Chris Lattner5839bf22005-08-26 17:15:30 +0000790 unsigned CPI = BB->getParent()->getConstantPool()->
791 getConstantPoolIndex(CP->get());
Nate Begeman2497e632005-07-21 20:44:43 +0000792 if (PICEnabled)
793 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
794 .addConstantPoolIndex(CPI);
795 else
796 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +0000797 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +0000798 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000799 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
800 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000801 } else {
802 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000803 switch(SelectAddr(Address, Tmp1, offset)) {
804 default: assert(0 && "Unhandled return value from SelectAddr");
805 case 0: // imm offset, no frame, no index
806 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
807 break;
808 case 1: // imm offset + frame index
809 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
810 break;
811 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +0000812 Opc = IndexedOpForOp(Opc);
813 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000814 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +0000815 case 3: {
816 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
817 GlobalValue *GV = GN->getGlobal();
818 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
819 }
Nate Begeman04730362005-04-01 04:45:11 +0000820 }
Nate Begeman5e966612005-03-24 06:28:42 +0000821 }
822 return Result;
823 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000824
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000825 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000826 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000827 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000828 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000829 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
830 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
831 };
832 static const unsigned FPR[] = {
833 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
834 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
835 };
836
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000837 // Lower the chain for this call.
838 Select(N.getOperand(0));
839 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000840
Nate Begemand860aa62005-04-04 22:17:48 +0000841 MachineInstr *CallMI;
842 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000843 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +0000844 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000845 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +0000846 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000847 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +0000848 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000849 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +0000850 true);
851 } else {
852 Tmp1 = SelectExpr(N.getOperand(1));
Chris Lattner86fac6b2005-08-24 22:21:47 +0000853 BuildMI(BB, PPC::MTCTR, 1).addReg(Tmp1);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000854 BuildMI(BB, PPC::OR4, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
Nate Begemand860aa62005-04-04 22:17:48 +0000855 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
856 .addReg(PPC::R12);
857 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000858
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000859 // Load the register args to virtual regs
860 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000861 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000862 ArgVR.push_back(SelectExpr(N.getOperand(i)));
863
864 // Copy the virtual registers into the appropriate argument register
865 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
866 switch(N.getOperand(i+2).getValueType()) {
867 default: Node->dump(); assert(0 && "Unknown value type for call");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000868 case MVT::i32:
869 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +0000870 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000871 BuildMI(BB, PPC::OR4,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +0000872 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
873 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000874 ++GPR_idx;
875 break;
876 case MVT::f64:
877 case MVT::f32:
878 assert(FPR_idx < 13 && "Too many fp args");
Chris Lattner919c0322005-10-01 01:35:02 +0000879 BuildMI(BB, N.getOperand(i+2).getValueType() == MVT::f32 ? PPC::FMRS :
880 PPC::FMRD, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +0000881 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000882 ++FPR_idx;
883 break;
884 }
885 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000886
Nate Begemand860aa62005-04-04 22:17:48 +0000887 // Put the call instruction in the correct place in the MachineBasicBlock
888 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000889
890 switch (Node->getValueType(0)) {
891 default: assert(0 && "Unknown value type for call result!");
892 case MVT::Other: return 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000893 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +0000894 if (Node->getValueType(1) == MVT::i32) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000895 BuildMI(BB, PPC::OR4, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
896 BuildMI(BB, PPC::OR4, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
Nate Begemane5846682005-04-04 06:52:38 +0000897 } else {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000898 BuildMI(BB, PPC::OR4, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begemane5846682005-04-04 06:52:38 +0000899 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000900 break;
901 case MVT::f32:
Chris Lattner919c0322005-10-01 01:35:02 +0000902 BuildMI(BB, PPC::FMRS, 1, Result).addReg(PPC::F1);
903 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000904 case MVT::f64:
Chris Lattner919c0322005-10-01 01:35:02 +0000905 BuildMI(BB, PPC::FMRD, 1, Result).addReg(PPC::F1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000906 break;
907 }
908 return Result+N.ResNo;
909 }
Nate Begemana9795f82005-03-24 04:41:43 +0000910
Nate Begemana9795f82005-03-24 04:41:43 +0000911 case ISD::SIGN_EXTEND_INREG:
912 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000913 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +0000914 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000915 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000916 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000917 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000918 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000919 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000920 break;
921 }
Nate Begemana9795f82005-03-24 04:41:43 +0000922 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000923
Nate Begemana9795f82005-03-24 04:41:43 +0000924 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +0000925 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +0000926 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +0000927 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Chris Lattner52897f82005-09-29 17:38:52 +0000928 else
929 ExprMap[N.getValue(1)] = 1;
Chris Lattnera8cd0152005-08-16 21:58:15 +0000930 Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +0000931 if (MVT::isInteger(DestType))
Nate Begeman1d9d7422005-10-18 00:28:58 +0000932 BuildMI(BB, PPC::OR4, 2, Result).addReg(Tmp1).addReg(Tmp1);
Chris Lattner919c0322005-10-01 01:35:02 +0000933 else if (DestType == MVT::f32)
934 BuildMI(BB, PPC::FMRS, 1, Result).addReg(Tmp1);
Nate Begemana3fd4002005-07-19 16:51:05 +0000935 else
Chris Lattner919c0322005-10-01 01:35:02 +0000936 BuildMI(BB, PPC::FMRD, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +0000937 return Result;
938
939 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000940 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +0000941 unsigned SH, MB, ME;
942 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
943 isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) {
944 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
945 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH)
946 .addImm(MB).addImm(ME);
947 return Result;
948 }
949 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +0000950 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +0000951 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +0000952 .addImm(31-Tmp2);
953 } else {
Jim Laskey191cf942005-08-11 21:59:23 +0000954 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +0000955 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +0000956 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
957 }
958 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000959
Nate Begeman5e966612005-03-24 06:28:42 +0000960 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000961 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +0000962 unsigned SH, MB, ME;
963 if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) &&
964 isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) {
965 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000966 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH & 0x1F)
Jim Laskey191cf942005-08-11 21:59:23 +0000967 .addImm(MB).addImm(ME);
968 return Result;
969 }
970 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +0000971 Tmp2 &= 0x1F;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000972 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm((32-Tmp2) & 0x1F)
Nate Begeman5e966612005-03-24 06:28:42 +0000973 .addImm(Tmp2).addImm(31);
974 } else {
Jim Laskey191cf942005-08-11 21:59:23 +0000975 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +0000976 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +0000977 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
978 }
979 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000980
Nate Begeman5e966612005-03-24 06:28:42 +0000981 case ISD::SRA:
Chris Lattner2b48bc62005-08-11 17:56:50 +0000982 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Jim Laskey191cf942005-08-11 21:59:23 +0000983 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerd2424192005-10-15 19:04:48 +0000984 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2 & 0x1F);
Nate Begeman5e966612005-03-24 06:28:42 +0000985 } else {
Jim Laskey191cf942005-08-11 21:59:23 +0000986 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman3664cef2005-04-13 22:14:14 +0000987 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +0000988 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
989 }
990 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000991
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000992 case ISD::CTLZ:
993 Tmp1 = SelectExpr(N.getOperand(0));
994 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
995 return Result;
996
Nate Begemana9795f82005-03-24 04:41:43 +0000997 case ISD::ADD:
Chris Lattnerb4138c42005-08-10 18:11:33 +0000998 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
999 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001000 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001001 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman1d9d7422005-10-18 00:28:58 +00001002 BuildMI(BB, PPC::ADD4, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001003 return Result;
Chris Lattner615c2d02005-09-28 22:29:58 +00001004
1005 case ISD::FADD:
1006 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::FMUL &&
1007 N.getOperand(0).Val->hasOneUse()) {
1008 ++FusedFP; // Statistic
1009 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1010 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1011 Tmp3 = SelectExpr(N.getOperand(1));
1012 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1013 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1014 return Result;
1015 }
1016 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::FMUL &&
1017 N.getOperand(1).Val->hasOneUse()) {
1018 ++FusedFP; // Statistic
1019 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1020 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1021 Tmp3 = SelectExpr(N.getOperand(0));
1022 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1023 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1024 return Result;
1025 }
1026 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1027 Tmp1 = SelectExpr(N.getOperand(0));
1028 Tmp2 = SelectExpr(N.getOperand(1));
1029 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1030 return Result;
1031
Nate Begemana9795f82005-03-24 04:41:43 +00001032 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001033 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001034 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1035 unsigned SH, MB, ME;
1036 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1037 unsigned OprOpc;
1038 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1039 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001040 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001041 } else {
1042 Tmp1 = SelectExpr(N.getOperand(0));
1043 isRunOfOnes(Tmp2, MB, ME);
1044 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001045 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001046 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1047 .addImm(MB).addImm(ME);
1048 RecordSuccess = true;
1049 return Result;
1050 } else if (isUInt16(Tmp2)) {
1051 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001052 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001053 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001054 RecordSuccess = true;
1055 return Result;
1056 } else if (isUInt16(Tmp2)) {
1057 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001058 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001059 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001060 RecordSuccess = true;
1061 return Result;
1062 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001063 }
Jim Laskey847c3a92005-08-12 23:38:02 +00001064 if (isOprNot(N.getOperand(1))) {
1065 Tmp1 = SelectExpr(N.getOperand(0));
1066 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1067 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1068 RecordSuccess = false;
1069 return Result;
1070 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001071 if (isOprNot(N.getOperand(0))) {
Jim Laskey847c3a92005-08-12 23:38:02 +00001072 Tmp1 = SelectExpr(N.getOperand(1));
1073 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1074 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001075 RecordSuccess = false;
1076 return Result;
1077 }
1078 // emit a regular and
1079 Tmp1 = SelectExpr(N.getOperand(0));
1080 Tmp2 = SelectExpr(N.getOperand(1));
1081 Opc = Recording ? PPC::ANDo : PPC::AND;
1082 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001083 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001084 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001085
Nate Begemana9795f82005-03-24 04:41:43 +00001086 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001087 if (SelectBitfieldInsert(N, Result))
1088 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001089 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1090 return Result;
Jim Laskey847c3a92005-08-12 23:38:02 +00001091 if (isOprNot(N.getOperand(1))) {
1092 Tmp1 = SelectExpr(N.getOperand(0));
1093 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1094 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1095 RecordSuccess = false;
1096 return Result;
1097 }
1098 if (isOprNot(N.getOperand(0))) {
1099 Tmp1 = SelectExpr(N.getOperand(1));
1100 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1101 BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2);
1102 RecordSuccess = false;
1103 return Result;
1104 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001105 // emit regular or
1106 Tmp1 = SelectExpr(N.getOperand(0));
1107 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman1d9d7422005-10-18 00:28:58 +00001108 Opc = Recording ? PPC::ORo : PPC::OR4;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001109 RecordSuccess = true;
1110 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001111 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001112
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001113 case ISD::XOR: {
1114 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001115 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001116 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1117 Tmp2 = SelectExpr(N.getOperand(1));
1118 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1119 return Result;
1120 }
Chris Lattner837a5212005-04-21 21:09:11 +00001121 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001122 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001123 switch(N.getOperand(0).getOpcode()) {
1124 case ISD::OR:
1125 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1126 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1127 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1128 break;
1129 case ISD::AND:
1130 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1131 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1132 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1133 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001134 case ISD::XOR:
1135 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1136 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1137 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1138 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001139 default:
1140 Tmp1 = SelectExpr(N.getOperand(0));
1141 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1142 break;
1143 }
1144 return Result;
1145 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001146 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1147 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001148 // emit regular xor
1149 Tmp1 = SelectExpr(N.getOperand(0));
1150 Tmp2 = SelectExpr(N.getOperand(1));
1151 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001152 return Result;
1153 }
1154
Chris Lattner615c2d02005-09-28 22:29:58 +00001155 case ISD::FSUB:
1156 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::FMUL &&
1157 N.getOperand(0).Val->hasOneUse()) {
1158 ++FusedFP; // Statistic
1159 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1160 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1161 Tmp3 = SelectExpr(N.getOperand(1));
1162 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1163 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Nate Begemana3fd4002005-07-19 16:51:05 +00001164 return Result;
1165 }
Chris Lattner615c2d02005-09-28 22:29:58 +00001166 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::FMUL &&
1167 N.getOperand(1).Val->hasOneUse()) {
1168 ++FusedFP; // Statistic
1169 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1170 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1171 Tmp3 = SelectExpr(N.getOperand(0));
1172 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1173 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1174 return Result;
1175 }
1176 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1177 Tmp1 = SelectExpr(N.getOperand(0));
1178 Tmp2 = SelectExpr(N.getOperand(1));
1179 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1180 return Result;
1181 case ISD::SUB:
Chris Lattner59b21c22005-08-09 18:29:55 +00001182 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001183 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001184 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman4b46fc02005-08-24 04:59:21 +00001185 if (0 == Tmp1)
1186 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp2);
1187 else
1188 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001189 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001190 }
1191 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001192 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001193 Tmp1 = SelectExpr(N.getOperand(0));
1194 Tmp2 = SelectExpr(N.getOperand(1));
1195 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001196 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001197
Chris Lattner615c2d02005-09-28 22:29:58 +00001198 case ISD::FMUL:
1199 Tmp1 = SelectExpr(N.getOperand(0));
1200 Tmp2 = SelectExpr(N.getOperand(1));
1201 BuildMI(BB, DestType == MVT::f32 ? PPC::FMULS : PPC::FMUL, 2,
1202 Result).addReg(Tmp1).addReg(Tmp2);
1203 return Result;
1204
Nate Begeman5e966612005-03-24 06:28:42 +00001205 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001206 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001207 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001208 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001209 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001210 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001211 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner615c2d02005-09-28 22:29:58 +00001212 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001213 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001214 return Result;
1215
Nate Begeman815d6da2005-04-06 00:25:27 +00001216 case ISD::MULHS:
1217 case ISD::MULHU:
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 Tmp2 = SelectExpr(N.getOperand(1));
1220 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1221 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1222 return Result;
1223
Chris Lattner615c2d02005-09-28 22:29:58 +00001224 case ISD::FDIV:
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 Tmp2 = SelectExpr(N.getOperand(1));
1227 switch (DestType) {
1228 default: assert(0 && "Unknown type to ISD::FDIV"); break;
1229 case MVT::f32: Opc = PPC::FDIVS; break;
1230 case MVT::f64: Opc = PPC::FDIV; break;
1231 }
1232 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1233 return Result;
1234
Nate Begemanf3d08f32005-03-29 00:03:27 +00001235 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001236 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001237 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1238 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001239 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001240 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001241 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1242 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001243 return Result;
1244 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1245 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001246 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001247 Tmp1 = MakeIntReg();
1248 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001249 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1250 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1251 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1252 return Result;
Nate Begeman9f833d32005-04-12 00:10:02 +00001253 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001254 }
1255 // fall thru
1256 case ISD::UDIV:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001257 Tmp1 = SelectExpr(N.getOperand(0));
1258 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner615c2d02005-09-28 22:29:58 +00001259 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
Nate Begemanf3d08f32005-03-29 00:03:27 +00001260 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1261 return Result;
1262
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001263 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001264 case ISD::SUB_PARTS: {
1265 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1266 "Not an i64 add/sub!");
Nate Begeman456f1e82005-08-17 00:20:08 +00001267 unsigned Tmp4 = 0;
Nate Begeman456f1e82005-08-17 00:20:08 +00001268 Tmp1 = SelectExpr(N.getOperand(0));
1269 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman456f1e82005-08-17 00:20:08 +00001270
Nate Begemanca12a2b2005-03-28 22:28:37 +00001271 if (N.getOpcode() == ISD::ADD_PARTS) {
Chris Lattner95e06822005-08-26 16:38:51 +00001272 bool ME = false, ZE = false;
Chris Lattner801d5f52005-08-25 23:19:58 +00001273 if (isIntImmediate(N.getOperand(3), Tmp3)) {
1274 ME = (signed)Tmp3 == -1;
1275 ZE = Tmp3 == 0;
1276 }
1277
1278 if (!ZE && !ME)
1279 Tmp4 = SelectExpr(N.getOperand(3));
1280
1281 if (isIntImmediate(N.getOperand(2), Tmp3) &&
1282 ((signed)Tmp3 >= -32768 || (signed)Tmp3 < 32768)) {
1283 // Codegen the low 32 bits of the add. Interestingly, there is no
1284 // shifted form of add immediate carrying.
Nate Begeman456f1e82005-08-17 00:20:08 +00001285 BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3);
Chris Lattner801d5f52005-08-25 23:19:58 +00001286 } else {
1287 Tmp3 = SelectExpr(N.getOperand(2));
Nate Begeman456f1e82005-08-17 00:20:08 +00001288 BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3);
Chris Lattner801d5f52005-08-25 23:19:58 +00001289 }
1290
Nate Begeman456f1e82005-08-17 00:20:08 +00001291 // Codegen the high 32 bits, adding zero, minus one, or the full value
1292 // along with the carry flag produced by addc/addic to tmp2.
Chris Lattner801d5f52005-08-25 23:19:58 +00001293 if (ZE) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001294 BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2);
Chris Lattner801d5f52005-08-25 23:19:58 +00001295 } else if (ME) {
Nate Begeman456f1e82005-08-17 00:20:08 +00001296 BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2);
Chris Lattner801d5f52005-08-25 23:19:58 +00001297 } else {
Nate Begeman456f1e82005-08-17 00:20:08 +00001298 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4);
Chris Lattner801d5f52005-08-25 23:19:58 +00001299 }
Nate Begemanca12a2b2005-03-28 22:28:37 +00001300 } else {
Chris Lattner801d5f52005-08-25 23:19:58 +00001301 Tmp3 = SelectExpr(N.getOperand(2));
1302 Tmp4 = SelectExpr(N.getOperand(3));
Nate Begeman456f1e82005-08-17 00:20:08 +00001303 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1);
1304 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2);
Nate Begeman27eeb002005-04-02 05:59:34 +00001305 }
1306 return Result+N.ResNo;
1307 }
1308
Chris Lattner88ac32c2005-08-09 20:21:10 +00001309 case ISD::SETCC: {
1310 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
1311 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
1312 // We can codegen setcc op, imm very efficiently compared to a brcond.
1313 // Check for those cases here.
1314 // setcc op, 0
1315 if (Tmp3 == 0) {
1316 Tmp1 = SelectExpr(Node->getOperand(0));
1317 switch (CC) {
Chris Lattneree84f112005-08-25 17:49:31 +00001318 default: Node->dump(); assert(0 && "Unhandled SetCC condition");abort();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001319 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001320 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001321 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1322 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1323 .addImm(5).addImm(31);
1324 break;
1325 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001326 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001327 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1328 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1329 break;
1330 case ISD::SETLT:
1331 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1332 .addImm(31).addImm(31);
1333 break;
1334 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001335 Tmp2 = MakeIntReg();
1336 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001337 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1338 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1339 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1340 .addImm(31).addImm(31);
1341 break;
Nate Begeman9765c252005-04-12 21:22:28 +00001342 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001343 return Result;
1344 } else if (Tmp3 == ~0U) { // setcc op, -1
1345 Tmp1 = SelectExpr(Node->getOperand(0));
1346 switch (CC) {
1347 default: assert(0 && "Unhandled SetCC condition"); abort();
1348 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00001349 Tmp2 = MakeIntReg();
1350 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001351 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
1352 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
1353 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
1354 break;
1355 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00001356 Tmp2 = MakeIntReg();
1357 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001358 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1359 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
1360 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
1361 break;
1362 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001363 Tmp2 = MakeIntReg();
1364 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001365 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
1366 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1367 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1368 .addImm(31).addImm(31);
1369 break;
1370 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00001371 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00001372 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1373 .addImm(31).addImm(31);
1374 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1375 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001376 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001377 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001378 }
Nate Begeman33162522005-03-29 21:54:38 +00001379 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001380
Nate Begemanc24d4842005-08-10 20:52:09 +00001381 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1382 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001383 return Result;
1384 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001385
1386 case ISD::SELECT_CC: {
1387 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
Nate Begemana3fd4002005-07-19 16:51:05 +00001388
Nate Begeman4b46fc02005-08-24 04:59:21 +00001389 // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1390 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
1391 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N.getOperand(2));
1392 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N.getOperand(3));
1393 if (N1C && N2C && N3C && N1C->isNullValue() && N3C->isNullValue() &&
Nate Begeman6ef49492005-08-24 05:06:48 +00001394 N2C->getValue() == 1ULL && CC == ISD::SETNE) {
Nate Begeman4b46fc02005-08-24 04:59:21 +00001395 Tmp1 = SelectExpr(Node->getOperand(0));
1396 Tmp2 = MakeIntReg();
Nate Begeman6ef49492005-08-24 05:06:48 +00001397 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1398 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman4b46fc02005-08-24 04:59:21 +00001399 return Result;
1400 }
1401
Nate Begeman5a014812005-08-14 01:17:16 +00001402 // If the False value only has one use, we can generate better code by
1403 // selecting it in the fallthrough basic block rather than here, which
1404 // increases register pressure.
Nate Begeman5a014812005-08-14 01:17:16 +00001405 unsigned TrueValue = SelectExpr(N.getOperand(2));
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001406 unsigned FalseValue;
1407
1408 // If the false value is simple enough, evaluate it inline in the false
1409 // block.
Chris Lattnerb30ee6a2005-08-22 00:47:28 +00001410 if (N.getOperand(3).Val->hasOneUse() &&
1411 (isa<ConstantSDNode>(N.getOperand(3)) ||
Chris Lattnerb30ee6a2005-08-22 00:47:28 +00001412 isa<GlobalAddressSDNode>(N.getOperand(3))))
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001413 FalseValue = 0;
1414 else
1415 FalseValue = SelectExpr(N.getOperand(3));
Nate Begemanc24d4842005-08-10 20:52:09 +00001416 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
1417 Opc = getBCCForSetCC(CC);
1418
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001419 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00001420 // value and the MBB to hold the PHI instruction for this SetCC.
1421 MachineBasicBlock *thisMBB = BB;
1422 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1423 ilist<MachineBasicBlock>::iterator It = BB;
1424 ++It;
1425
1426 // thisMBB:
1427 // ...
1428 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001429 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00001430 // bCC copy1MBB
1431 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001432 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1433 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001434 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001435 MachineFunction *F = BB->getParent();
1436 F->getBasicBlockList().insert(It, copy0MBB);
1437 F->getBasicBlockList().insert(It, sinkMBB);
1438 // Update machine-CFG edges
1439 BB->addSuccessor(copy0MBB);
1440 BB->addSuccessor(sinkMBB);
1441
1442 // copy0MBB:
1443 // %FalseValue = ...
1444 // # fallthrough to sinkMBB
1445 BB = copy0MBB;
Chris Lattner4dd4a2d2005-08-21 17:41:11 +00001446
1447 // If the false value is simple enough, evaluate it here, to avoid it being
1448 // evaluated on the true edge.
1449 if (FalseValue == 0)
1450 FalseValue = SelectExpr(N.getOperand(3));
1451
Nate Begeman74747862005-03-29 22:24:51 +00001452 // Update machine-CFG edges
1453 BB->addSuccessor(sinkMBB);
1454
1455 // sinkMBB:
1456 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1457 // ...
1458 BB = sinkMBB;
1459 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1460 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001461 return Result;
1462 }
Nate Begemana9795f82005-03-24 04:41:43 +00001463
Chris Lattner0c09a412005-08-18 17:16:52 +00001464 case ISD::Constant: {
1465 assert(N.getValueType() == MVT::i32 &&
1466 "Only i32 constants are legal on this target!");
Nate Begeman58dfb082005-08-18 18:14:49 +00001467 unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue();
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001468 if (isInt16(v)) {
1469 BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo16(v));
Chris Lattner0c09a412005-08-18 17:16:52 +00001470 } else {
Jim Laskey5b5f0b72005-08-18 18:58:23 +00001471 unsigned Hi = Hi16(v);
1472 unsigned Lo = Lo16(v);
1473 if (Lo) {
1474 Tmp1 = MakeIntReg();
1475 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi);
1476 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo);
1477 } else {
1478 BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi);
1479 }
Nate Begemana9795f82005-03-24 04:41:43 +00001480 }
1481 return Result;
Chris Lattner0c09a412005-08-18 17:16:52 +00001482 }
Nate Begemana3fd4002005-07-19 16:51:05 +00001483
Nate Begemana3fd4002005-07-19 16:51:05 +00001484 case ISD::FNEG:
1485 if (!NoExcessFPPrecision &&
Chris Lattner615c2d02005-09-28 22:29:58 +00001486 ISD::FADD == N.getOperand(0).getOpcode() &&
Nate Begemana3fd4002005-07-19 16:51:05 +00001487 N.getOperand(0).Val->hasOneUse() &&
Chris Lattner615c2d02005-09-28 22:29:58 +00001488 ISD::FMUL == N.getOperand(0).getOperand(0).getOpcode() &&
Nate Begemana3fd4002005-07-19 16:51:05 +00001489 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
1490 ++FusedFP; // Statistic
1491 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1492 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1493 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1494 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1495 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1496 } else if (!NoExcessFPPrecision &&
Chris Lattner615c2d02005-09-28 22:29:58 +00001497 ISD::FADD == N.getOperand(0).getOpcode() &&
Nate Begemana3fd4002005-07-19 16:51:05 +00001498 N.getOperand(0).Val->hasOneUse() &&
Chris Lattner615c2d02005-09-28 22:29:58 +00001499 ISD::FMUL == N.getOperand(0).getOperand(1).getOpcode() &&
Nate Begemana3fd4002005-07-19 16:51:05 +00001500 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
1501 ++FusedFP; // Statistic
1502 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1503 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1504 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1505 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1506 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1507 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
1508 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001509 if (N.getOperand(0).getValueType() == MVT::f32)
1510 BuildMI(BB, PPC::FNABSS, 1, Result).addReg(Tmp1);
1511 else
1512 BuildMI(BB, PPC::FNABSD, 1, Result).addReg(Tmp1);
1513
Nate Begemana3fd4002005-07-19 16:51:05 +00001514 } else {
1515 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001516 if (N.getOperand(0).getValueType() == MVT::f32)
1517 BuildMI(BB, PPC::FNEGS, 1, Result).addReg(Tmp1);
1518 else
1519 BuildMI(BB, PPC::FNEGD, 1, Result).addReg(Tmp1);
Nate Begemana3fd4002005-07-19 16:51:05 +00001520 }
1521 return Result;
1522
1523 case ISD::FABS:
1524 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001525 if (N.getOperand(0).getValueType() == MVT::f32)
1526 BuildMI(BB, PPC::FABSS, 1, Result).addReg(Tmp1);
1527 else
1528 BuildMI(BB, PPC::FABSD, 1, Result).addReg(Tmp1);
Nate Begemana3fd4002005-07-19 16:51:05 +00001529 return Result;
1530
Nate Begemanadeb43d2005-07-20 22:42:00 +00001531 case ISD::FSQRT:
1532 Tmp1 = SelectExpr(N.getOperand(0));
1533 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
1534 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1535 return Result;
1536
Nate Begemana3fd4002005-07-19 16:51:05 +00001537 case ISD::FP_ROUND:
1538 assert (DestType == MVT::f32 &&
1539 N.getOperand(0).getValueType() == MVT::f64 &&
1540 "only f64 to f32 conversion supported here");
1541 Tmp1 = SelectExpr(N.getOperand(0));
1542 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1543 return Result;
1544
1545 case ISD::FP_EXTEND:
1546 assert (DestType == MVT::f64 &&
1547 N.getOperand(0).getValueType() == MVT::f32 &&
1548 "only f32 to f64 conversion supported here");
1549 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner919c0322005-10-01 01:35:02 +00001550 BuildMI(BB, PPC::FMRSD, 1, Result).addReg(Tmp1);
Nate Begemana3fd4002005-07-19 16:51:05 +00001551 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00001552 }
Nate Begemana9795f82005-03-24 04:41:43 +00001553 return 0;
1554}
1555
1556void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00001557 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00001558 unsigned opcode = N.getOpcode();
1559
1560 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1561 return; // Already selected.
1562
1563 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001564
Nate Begemana9795f82005-03-24 04:41:43 +00001565 switch (Node->getOpcode()) {
1566 default:
1567 Node->dump(); std::cerr << "\n";
1568 assert(0 && "Node not handled yet!");
1569 case ISD::EntryToken: return; // Noop
1570 case ISD::TokenFactor:
1571 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1572 Select(Node->getOperand(i));
1573 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001574 case ISD::CALLSEQ_START:
1575 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00001576 Select(N.getOperand(0));
1577 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00001578 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00001579 PPC::ADJCALLSTACKUP;
1580 BuildMI(BB, Opc, 1).addImm(Tmp1);
1581 return;
1582 case ISD::BR: {
1583 MachineBasicBlock *Dest =
1584 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001585 Select(N.getOperand(0));
1586 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1587 return;
1588 }
Nate Begeman7cbd5252005-08-16 19:49:35 +00001589 case ISD::BR_CC:
1590 case ISD::BRTWOWAY_CC:
Nate Begemana9795f82005-03-24 04:41:43 +00001591 SelectBranchCC(N);
1592 return;
1593 case ISD::CopyToReg:
1594 Select(N.getOperand(0));
Chris Lattnera8cd0152005-08-16 21:58:15 +00001595 Tmp1 = SelectExpr(N.getOperand(2));
1596 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001597
Nate Begemana9795f82005-03-24 04:41:43 +00001598 if (Tmp1 != Tmp2) {
Chris Lattner919c0322005-10-01 01:35:02 +00001599 if (N.getOperand(2).getValueType() == MVT::f64)
1600 BuildMI(BB, PPC::FMRD, 1, Tmp2).addReg(Tmp1);
1601 else if (N.getOperand(2).getValueType() == MVT::f32)
1602 BuildMI(BB, PPC::FMRS, 1, Tmp2).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001603 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001604 BuildMI(BB, PPC::OR4, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001605 }
1606 return;
1607 case ISD::ImplicitDef:
1608 Select(N.getOperand(0));
Chris Lattner2b544002005-08-24 23:08:16 +00001609 Tmp1 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
1610 if (N.getOperand(1).getValueType() == MVT::i32)
1611 BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Tmp1);
Chris Lattner919c0322005-10-01 01:35:02 +00001612 else if (N.getOperand(1).getValueType() == MVT::f32)
1613 BuildMI(BB, PPC::IMPLICIT_DEF_F4, 0, Tmp1);
Chris Lattner2b544002005-08-24 23:08:16 +00001614 else
Chris Lattner919c0322005-10-01 01:35:02 +00001615 BuildMI(BB, PPC::IMPLICIT_DEF_F8, 0, Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001616 return;
1617 case ISD::RET:
1618 switch (N.getNumOperands()) {
1619 default:
1620 assert(0 && "Unknown return instruction!");
1621 case 3:
1622 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1623 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001624 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00001625 Select(N.getOperand(0));
1626 Tmp1 = SelectExpr(N.getOperand(1));
1627 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman1d9d7422005-10-18 00:28:58 +00001628 BuildMI(BB, PPC::OR4, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1629 BuildMI(BB, PPC::OR4, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001630 break;
1631 case 2:
1632 Select(N.getOperand(0));
1633 Tmp1 = SelectExpr(N.getOperand(1));
1634 switch (N.getOperand(1).getValueType()) {
1635 default:
1636 assert(0 && "Unknown return type!");
1637 case MVT::f64:
Chris Lattner919c0322005-10-01 01:35:02 +00001638 BuildMI(BB, PPC::FMRD, 1, PPC::F1).addReg(Tmp1);
1639 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001640 case MVT::f32:
Chris Lattner919c0322005-10-01 01:35:02 +00001641 BuildMI(BB, PPC::FMRS, 1, PPC::F1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001642 break;
1643 case MVT::i32:
Nate Begeman1d9d7422005-10-18 00:28:58 +00001644 BuildMI(BB, PPC::OR4, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001645 break;
1646 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001647 case 1:
1648 Select(N.getOperand(0));
1649 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001650 }
1651 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1652 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001653 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00001654 case ISD::STORE: {
1655 SDOperand Chain = N.getOperand(0);
1656 SDOperand Value = N.getOperand(1);
1657 SDOperand Address = N.getOperand(2);
1658 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00001659
Nate Begeman2497e632005-07-21 20:44:43 +00001660 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00001661
Nate Begeman2497e632005-07-21 20:44:43 +00001662 if (opcode == ISD::STORE) {
1663 switch(Value.getValueType()) {
1664 default: assert(0 && "unknown Type in store");
1665 case MVT::i32: Opc = PPC::STW; break;
1666 case MVT::f64: Opc = PPC::STFD; break;
1667 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001668 }
Nate Begeman2497e632005-07-21 20:44:43 +00001669 } else { //ISD::TRUNCSTORE
1670 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
1671 default: assert(0 && "unknown Type in store");
Nate Begeman2497e632005-07-21 20:44:43 +00001672 case MVT::i8: Opc = PPC::STB; break;
1673 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001674 }
Nate Begemana9795f82005-03-24 04:41:43 +00001675 }
Nate Begeman2497e632005-07-21 20:44:43 +00001676
1677 if(Address.getOpcode() == ISD::FrameIndex) {
1678 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1679 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00001680 } else {
1681 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001682 switch(SelectAddr(Address, Tmp2, offset)) {
1683 default: assert(0 && "Unhandled return value from SelectAddr");
1684 case 0: // imm offset, no frame, no index
1685 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
1686 break;
1687 case 1: // imm offset + frame index
1688 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
1689 break;
1690 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00001691 Opc = IndexedOpForOp(Opc);
1692 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001693 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001694 case 3: {
1695 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1696 GlobalValue *GV = GN->getGlobal();
1697 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
1698 }
Nate Begeman2497e632005-07-21 20:44:43 +00001699 }
1700 }
1701 return;
1702 }
Nate Begemana9795f82005-03-24 04:41:43 +00001703 case ISD::EXTLOAD:
1704 case ISD::SEXTLOAD:
1705 case ISD::ZEXTLOAD:
1706 case ISD::LOAD:
1707 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001708 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00001709 case ISD::CALL:
1710 case ISD::DYNAMIC_STACKALLOC:
1711 ExprMap.erase(N);
1712 SelectExpr(N);
1713 return;
1714 }
1715 assert(0 && "Should not be reached!");
1716}
1717
1718
Nate Begeman1d9d7422005-10-18 00:28:58 +00001719/// createPPCPatternInstructionSelector - This pass converts an LLVM function
Nate Begemana9795f82005-03-24 04:41:43 +00001720/// into a machine code representation using pattern matching and a machine
1721/// description file.
1722///
Nate Begeman1d9d7422005-10-18 00:28:58 +00001723FunctionPass *llvm::createPPCISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001724 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001725}
1726