blob: e5668e1d35f139096fcb06a2c8da69b5e26e43e8 [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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Nate Begemana9795f82005-03-24 04:41:43 +000020#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000046 // Set up the register classes.
47 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000048 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000049 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
50
Nate Begeman74d73452005-03-31 00:15:26 +000051 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000052 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
53 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
54 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
55
Nate Begeman74d73452005-03-31 00:15:26 +000056 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
57 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
58 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000059
60 // PowerPC has no SREM/UREM instructions
61 setOperationAction(ISD::SREM, MVT::i32, Expand);
62 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000063
Chris Lattnercbd06fc2005-04-07 19:41:49 +000064 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000065 addLegalFPImmediate(+0.0); // Necessary for FSEL
66 addLegalFPImmediate(-0.0); //
67
Nate Begemana9795f82005-03-24 04:41:43 +000068 computeRegisterProperties();
69 }
70
71 /// LowerArguments - This hook must be implemented to indicate how we should
72 /// lower the arguments for the specified function, into the specified DAG.
73 virtual std::vector<SDOperand>
74 LowerArguments(Function &F, SelectionDAG &DAG);
75
76 /// LowerCallTo - This hook lowers an abstract call to a function into an
77 /// actual call.
78 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000079 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
80 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000081
82 virtual std::pair<SDOperand, SDOperand>
83 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand,SDOperand>
86 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
87 const Type *ArgTy, SelectionDAG &DAG);
88
89 virtual std::pair<SDOperand, SDOperand>
90 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
91 SelectionDAG &DAG);
92 };
93}
94
95
96std::vector<SDOperand>
97PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
98 //
99 // add beautiful description of PPC stack frame format, or at least some docs
100 //
101 MachineFunction &MF = DAG.getMachineFunction();
102 MachineFrameInfo *MFI = MF.getFrameInfo();
103 MachineBasicBlock& BB = MF.front();
104 std::vector<SDOperand> ArgValues;
105
106 // Due to the rather complicated nature of the PowerPC ABI, rather than a
107 // fixed size array of physical args, for the sake of simplicity let the STL
108 // handle tracking them for us.
109 std::vector<unsigned> argVR, argPR, argOp;
110 unsigned ArgOffset = 24;
111 unsigned GPR_remaining = 8;
112 unsigned FPR_remaining = 13;
113 unsigned GPR_idx = 0, FPR_idx = 0;
114 static const unsigned GPR[] = {
115 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
116 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
117 };
118 static const unsigned FPR[] = {
119 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
120 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
121 };
122
123 // Add DAG nodes to load the arguments... On entry to a function on PPC,
124 // the arguments start at offset 24, although they are likely to be passed
125 // in registers.
126 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
127 SDOperand newroot, argt;
128 unsigned ObjSize;
129 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000130 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000131 MVT::ValueType ObjectVT = getValueType(I->getType());
132
133 switch (ObjectVT) {
134 default: assert(0 && "Unhandled argument type!");
135 case MVT::i1:
136 case MVT::i8:
137 case MVT::i16:
138 case MVT::i32:
139 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000140 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000141 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000142 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000143 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
144 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000145 if (ObjectVT != MVT::i32)
146 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000147 } else {
148 needsLoad = true;
149 }
150 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000151 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000152 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000153 if (GPR_remaining > 0) {
154 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000155 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000156 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
157 // If we have two or more remaining argument registers, then both halves
158 // of the i64 can be sourced from there. Otherwise, the lower half will
159 // have to come off the stack. This can happen when an i64 is preceded
160 // by 28 bytes of arguments.
161 if (GPR_remaining > 1) {
162 MF.addLiveIn(GPR[GPR_idx+1]);
163 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
164 } else {
165 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
166 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
167 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN);
168 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000169 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000170 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
171 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000172 } else {
173 needsLoad = true;
174 }
175 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000176 case MVT::f32:
177 case MVT::f64:
178 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
179 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000180 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000181 MF.addLiveIn(FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000182 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
183 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000184 --FPR_remaining;
185 ++FPR_idx;
186 } else {
187 needsLoad = true;
188 }
189 break;
190 }
191
192 // We need to load the argument to a virtual register if we determined above
193 // that we ran out of physical registers of the appropriate type
194 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000195 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000196 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000197 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000198 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
199 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000200 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
201 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000202 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
203 }
204
205 // Every 4 bytes of argument space consumes one of the GPRs available for
206 // argument passing.
207 if (GPR_remaining > 0) {
208 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
209 GPR_remaining -= delta;
210 GPR_idx += delta;
211 }
212 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000213 if (newroot.Val)
214 DAG.setRoot(newroot.getValue(1));
Nate Begemana9795f82005-03-24 04:41:43 +0000215
Nate Begemana9795f82005-03-24 04:41:43 +0000216 ArgValues.push_back(argt);
217 }
218
Nate Begemana9795f82005-03-24 04:41:43 +0000219 // If the function takes variable number of arguments, make a frame index for
220 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000221 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000222 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000223 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000224 // If this function is vararg, store any remaining integer argument regs
225 // to their spots on the stack so that they may be loaded by deferencing the
226 // result of va_next.
227 std::vector<SDOperand> MemOps;
228 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000229 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000230 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
231 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
232 Val, FIN);
233 MemOps.push_back(Store);
234 // Increment the address by four for the next argument to store
235 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
236 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
237 }
238 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000239 }
Nate Begemana9795f82005-03-24 04:41:43 +0000240
Nate Begemancd08e4c2005-04-09 20:09:12 +0000241 // Finally, inform the code generator which regs we return values in.
242 switch (getValueType(F.getReturnType())) {
243 default: assert(0 && "Unknown type!");
244 case MVT::isVoid: break;
245 case MVT::i1:
246 case MVT::i8:
247 case MVT::i16:
248 case MVT::i32:
249 MF.addLiveOut(PPC::R3);
250 break;
251 case MVT::i64:
252 MF.addLiveOut(PPC::R3);
253 MF.addLiveOut(PPC::R4);
254 break;
255 case MVT::f32:
256 case MVT::f64:
257 MF.addLiveOut(PPC::F1);
258 break;
259 }
260
Nate Begemana9795f82005-03-24 04:41:43 +0000261 return ArgValues;
262}
263
264std::pair<SDOperand, SDOperand>
265PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000266 const Type *RetTy, bool isVarArg,
267 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
268 // args_to_use will accumulate outgoing args for the ISD::CALL case in
269 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000270 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000271
272 // Count how many bytes are to be pushed on the stack, including the linkage
273 // area, and parameter passing area.
274 unsigned NumBytes = 24;
275
276 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000277 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
278 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000279 } else {
280 for (unsigned i = 0, e = Args.size(); i != e; ++i)
281 switch (getValueType(Args[i].second)) {
282 default: assert(0 && "Unknown value type!");
283 case MVT::i1:
284 case MVT::i8:
285 case MVT::i16:
286 case MVT::i32:
287 case MVT::f32:
288 NumBytes += 4;
289 break;
290 case MVT::i64:
291 case MVT::f64:
292 NumBytes += 8;
293 break;
294 }
295
296 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
297 // plus 32 bytes of argument space in case any called code gets funky on us.
298 if (NumBytes < 56) NumBytes = 56;
299
300 // Adjust the stack pointer for the new arguments...
301 // These operations are automatically eliminated by the prolog/epilog pass
302 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
303 DAG.getConstant(NumBytes, getPointerTy()));
304
305 // Set up a copy of the stack pointer for use loading and storing any
306 // arguments that may not fit in the registers available for argument
307 // passing.
308 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
309 DAG.getEntryNode());
310
311 // Figure out which arguments are going to go in registers, and which in
312 // memory. Also, if this is a vararg function, floating point operations
313 // must be stored to our stack, and loaded into integer regs as well, if
314 // any integer regs are available for argument passing.
315 unsigned ArgOffset = 24;
316 unsigned GPR_remaining = 8;
317 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000318
319 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000320 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
321 // PtrOff will be used to store the current argument to the stack if a
322 // register cannot be found for it.
323 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
324 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000325 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000326
Nate Begemanf7e43382005-03-26 07:46:36 +0000327 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000328 default: assert(0 && "Unexpected ValueType for argument!");
329 case MVT::i1:
330 case MVT::i8:
331 case MVT::i16:
332 // Promote the integer to 32 bits. If the input type is signed use a
333 // sign extend, otherwise use a zero extend.
334 if (Args[i].second->isSigned())
335 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
336 else
337 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
338 // FALL THROUGH
339 case MVT::i32:
340 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000341 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000342 --GPR_remaining;
343 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000344 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
345 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000346 }
347 ArgOffset += 4;
348 break;
349 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000350 // If we have one free GPR left, we can place the upper half of the i64
351 // in it, and store the other half to the stack. If we have two or more
352 // free GPRs, then we can pass both halves of the i64 in registers.
353 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000354 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
355 Args[i].first, DAG.getConstant(1, MVT::i32));
356 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
357 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000358 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000359 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000360 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000361 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000362 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000363 } else {
364 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
365 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
367 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000368 }
Nate Begeman307e7442005-03-26 01:28:53 +0000369 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000370 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
371 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000372 }
373 ArgOffset += 8;
374 break;
375 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000376 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000378 args_to_use.push_back(Args[i].first);
379 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000380 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000381 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
382 Args[i].first, PtrOff);
383 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000384 // Float varargs are always shadowed in available integer registers
385 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000386 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000387 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000388 args_to_use.push_back(Load);
389 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000390 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000391 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000392 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
393 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000394 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000395 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000396 args_to_use.push_back(Load);
397 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000398 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000399 } else {
400 // If we have any FPRs remaining, we may also have GPRs remaining.
401 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
402 // GPRs.
403 if (GPR_remaining > 0) {
404 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
405 --GPR_remaining;
406 }
407 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
408 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
409 --GPR_remaining;
410 }
Nate Begeman74d73452005-03-31 00:15:26 +0000411 }
Nate Begeman307e7442005-03-26 01:28:53 +0000412 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
414 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000415 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000416 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000417 break;
418 }
Nate Begemana9795f82005-03-24 04:41:43 +0000419 }
Nate Begeman74d73452005-03-31 00:15:26 +0000420 if (!MemOps.empty())
421 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000422 }
423
424 std::vector<MVT::ValueType> RetVals;
425 MVT::ValueType RetTyVT = getValueType(RetTy);
426 if (RetTyVT != MVT::isVoid)
427 RetVals.push_back(RetTyVT);
428 RetVals.push_back(MVT::Other);
429
430 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
431 Chain, Callee, args_to_use), 0);
432 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
433 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
434 DAG.getConstant(NumBytes, getPointerTy()));
435 return std::make_pair(TheCall, Chain);
436}
437
438std::pair<SDOperand, SDOperand>
439PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
440 //vastart just returns the address of the VarArgsFrameIndex slot.
441 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
442}
443
444std::pair<SDOperand,SDOperand> PPC32TargetLowering::
445LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
446 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000447 MVT::ValueType ArgVT = getValueType(ArgTy);
448 SDOperand Result;
449 if (!isVANext) {
450 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
451 } else {
452 unsigned Amt;
453 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
454 Amt = 4;
455 else {
456 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
457 "Other types should have been promoted for varargs!");
458 Amt = 8;
459 }
460 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
461 DAG.getConstant(Amt, VAList.getValueType()));
462 }
463 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000464}
465
466
467std::pair<SDOperand, SDOperand> PPC32TargetLowering::
468LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
469 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000470 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000471 abort();
472}
473
474namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000475Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000476Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000477Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000478//===--------------------------------------------------------------------===//
479/// ISel - PPC32 specific code to select PPC32 machine instructions for
480/// SelectionDAG operations.
481//===--------------------------------------------------------------------===//
482class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000483 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000484 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
485 // for sdiv and udiv until it is put into the future
486 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000487
488 /// ExprMap - As shared expressions are codegen'd, we keep track of which
489 /// vreg the value is produced in, so we only emit one copy of each compiled
490 /// tree.
491 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000492
493 unsigned GlobalBaseReg;
494 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000495 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000496public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000497 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
498 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000499
Nate Begemanc7b09f12005-03-25 08:34:25 +0000500 /// runOnFunction - Override this function in order to reset our per-function
501 /// variables.
502 virtual bool runOnFunction(Function &Fn) {
503 // Make sure we re-emit a set of the global base reg if necessary
504 GlobalBaseInitialized = false;
505 return SelectionDAGISel::runOnFunction(Fn);
506 }
507
Nate Begemana9795f82005-03-24 04:41:43 +0000508 /// InstructionSelectBasicBlock - This callback is invoked by
509 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
510 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
511 DEBUG(BB->dump());
512 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000513 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000514 Select(DAG.getRoot());
515
516 // Clear state used for selection.
517 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000518 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000519 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000520
521 // dag -> dag expanders for integer divide by constant
522 SDOperand BuildSDIVSequence(SDOperand N);
523 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000524
Nate Begemandffcfcc2005-04-01 00:32:34 +0000525 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000526 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000527 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000528 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000529 unsigned SelectCC(SDOperand CC, unsigned &Opc);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000530 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000531 unsigned SelectExprFP(SDOperand N, unsigned Result);
532 void Select(SDOperand N);
533
Nate Begeman04730362005-04-01 04:45:11 +0000534 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000535 void SelectBranchCC(SDOperand N);
536};
537
Nate Begeman80196b12005-04-05 00:15:08 +0000538/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
539/// returns zero when the input is not exactly a power of two.
540static unsigned ExactLog2(unsigned Val) {
541 if (Val == 0 || (Val & (Val-1))) return 0;
542 unsigned Count = 0;
543 while (Val != 1) {
544 Val >>= 1;
545 ++Count;
546 }
547 return Count;
548}
549
Nate Begeman7ddecb42005-04-06 23:51:40 +0000550// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
551// any number of 0's on either side. the 1's are allowed to wrap from LSB to
552// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
553// not, since all 1's are not contiguous.
554static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
555 bool isRun = true;
556 MB = 0;
557 ME = 0;
558
559 // look for first set bit
560 int i = 0;
561 for (; i < 32; i++) {
562 if ((Val & (1 << (31 - i))) != 0) {
563 MB = i;
564 ME = i;
565 break;
566 }
567 }
568
569 // look for last set bit
570 for (; i < 32; i++) {
571 if ((Val & (1 << (31 - i))) == 0)
572 break;
573 ME = i;
574 }
575
576 // look for next set bit
577 for (; i < 32; i++) {
578 if ((Val & (1 << (31 - i))) != 0)
579 break;
580 }
581
582 // if we exhausted all the bits, we found a match at this point for 0*1*0*
583 if (i == 32)
584 return true;
585
586 // since we just encountered more 1's, if it doesn't wrap around to the
587 // most significant bit of the word, then we did not find a match to 1*0*1* so
588 // exit.
589 if (MB != 0)
590 return false;
591
592 // look for last set bit
593 for (MB = i; i < 32; i++) {
594 if ((Val & (1 << (31 - i))) == 0)
595 break;
596 }
597
598 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
599 // the value is not a run of ones.
600 if (i == 32)
601 return true;
602 return false;
603}
604
Nate Begeman439b4442005-04-05 04:22:58 +0000605/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000606/// the ConstantSDNode N can be used as an immediate to Opcode. The return
607/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000608/// ConstantSDNode, or is not suitable for use by that opcode.
609/// Return value codes for turning into an enum someday:
610/// 1: constant may be used in normal immediate form.
611/// 2: constant may be used in shifted immediate form.
612/// 3: log base 2 of the constant may be used.
613/// 4: constant is suitable for integer division conversion
614/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000615///
Nate Begeman439b4442005-04-05 04:22:58 +0000616static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
617 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000618 if (N.getOpcode() != ISD::Constant) return 0;
619
620 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
621
622 switch(Opcode) {
623 default: return 0;
624 case ISD::ADD:
625 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
626 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
627 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000628 case ISD::AND: {
629 unsigned MB, ME;
630 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
631 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
632 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
633 break;
634 }
Nate Begemana9795f82005-03-24 04:41:43 +0000635 case ISD::XOR:
636 case ISD::OR:
637 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
638 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
639 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000640 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000641 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000642 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
643 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000644 case ISD::SETCC:
645 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
646 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
647 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000648 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000649 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000650 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000651 if (v <= -2 || v >= 2) { return 4; }
652 break;
653 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000654 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000655 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000656 }
657 return 0;
658}
Nate Begeman3e897162005-03-31 23:55:40 +0000659
Nate Begemanc7bd4822005-04-11 06:34:10 +0000660/// NodeHasRecordingVariant - If SelectExpr can always produce code for
661/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
662/// return false.
663static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
664 switch(NodeOpcode) {
665 default: return false;
666 case ISD::AND:
Nate Begeman9765c252005-04-12 21:22:28 +0000667 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000668 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000669 }
670}
671
Nate Begeman3e897162005-03-31 23:55:40 +0000672/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
673/// to Condition. If the Condition is unordered or unsigned, the bool argument
674/// U is set to true, otherwise it is set to false.
675static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
676 U = false;
677 switch (Condition) {
678 default: assert(0 && "Unknown condition!"); abort();
679 case ISD::SETEQ: return PPC::BEQ;
680 case ISD::SETNE: return PPC::BNE;
681 case ISD::SETULT: U = true;
682 case ISD::SETLT: return PPC::BLT;
683 case ISD::SETULE: U = true;
684 case ISD::SETLE: return PPC::BLE;
685 case ISD::SETUGT: U = true;
686 case ISD::SETGT: return PPC::BGT;
687 case ISD::SETUGE: U = true;
688 case ISD::SETGE: return PPC::BGE;
689 }
Nate Begeman04730362005-04-01 04:45:11 +0000690 return 0;
691}
692
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000693/// getCROpForOp - Return the condition register opcode (or inverted opcode)
694/// associated with the SelectionDAG opcode.
695static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
696 switch (Opcode) {
697 default: assert(0 && "Unknown opcode!"); abort();
698 case ISD::AND:
699 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
700 if (!Inv1 && !Inv2) return PPC::CRAND;
701 if (Inv1 ^ Inv2) return PPC::CRANDC;
702 case ISD::OR:
703 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
704 if (!Inv1 && !Inv2) return PPC::CROR;
705 if (Inv1 ^ Inv2) return PPC::CRORC;
706 }
707 return 0;
708}
709
710/// getCRIdxForSetCC - Return the index of the condition register field
711/// associated with the SetCC condition, and whether or not the field is
712/// treated as inverted. That is, lt = 0; ge = 0 inverted.
713static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
714 switch (Condition) {
715 default: assert(0 && "Unknown condition!"); abort();
716 case ISD::SETULT:
717 case ISD::SETLT: Inv = false; return 0;
718 case ISD::SETUGE:
719 case ISD::SETGE: Inv = true; return 0;
720 case ISD::SETUGT:
721 case ISD::SETGT: Inv = false; return 1;
722 case ISD::SETULE:
723 case ISD::SETLE: Inv = true; return 1;
724 case ISD::SETEQ: Inv = false; return 2;
725 case ISD::SETNE: Inv = true; return 2;
726 }
727 return 0;
728}
729
Nate Begeman16ac7092005-04-18 02:43:24 +0000730/// getCRIdxForBCC - Return the index of the condition register field
731/// associated with the PowerPC branch instruction, and whether or not the field
732/// is treated as inverted. That is, lt = 0; ge = 0 inverted.
733static unsigned getCRIdxForBCC(unsigned Condition, bool& Inv) {
734 switch (Condition) {
735 default: assert(0 && "Unknown condition!"); abort();
736 case PPC::BLT: Inv = false; return 29; // 28 -> 31, rol 29
737 case PPC::BGE: Inv = true; return 29; // 28 -> 31, rol 29
738 case PPC::BGT: Inv = false; return 30; // 29 -> 31, rol 30
739 case PPC::BLE: Inv = true; return 30; // 29 -> 31, rol 30
740 case PPC::BEQ: Inv = false; return 31; // 30 -> 31, rol 31
741 case PPC::BNE: Inv = true; return 31; // 30 -> 31, rol 31
742 }
743 return 0;
744}
745
Nate Begeman04730362005-04-01 04:45:11 +0000746/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
747/// and store immediate instructions.
748static unsigned IndexedOpForOp(unsigned Opcode) {
749 switch(Opcode) {
750 default: assert(0 && "Unknown opcode!"); abort();
751 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
752 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
753 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
754 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
755 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
756 case PPC::LFD: return PPC::LFDX;
757 }
758 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000759}
Nate Begeman815d6da2005-04-06 00:25:27 +0000760
761// Structure used to return the necessary information to codegen an SDIV as
762// a multiply.
763struct ms {
764 int m; // magic number
765 int s; // shift amount
766};
767
768struct mu {
769 unsigned int m; // magic number
770 int a; // add indicator
771 int s; // shift amount
772};
773
774/// magic - calculate the magic numbers required to codegen an integer sdiv as
775/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
776/// or -1.
777static struct ms magic(int d) {
778 int p;
779 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
780 const unsigned int two31 = 2147483648U; // 2^31
781 struct ms mag;
782
783 ad = abs(d);
784 t = two31 + ((unsigned int)d >> 31);
785 anc = t - 1 - t%ad; // absolute value of nc
786 p = 31; // initialize p
787 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
788 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
789 q2 = two31/ad; // initialize q2 = 2p/abs(d)
790 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
791 do {
792 p = p + 1;
793 q1 = 2*q1; // update q1 = 2p/abs(nc)
794 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
795 if (r1 >= anc) { // must be unsigned comparison
796 q1 = q1 + 1;
797 r1 = r1 - anc;
798 }
799 q2 = 2*q2; // update q2 = 2p/abs(d)
800 r2 = 2*r2; // update r2 = rem(2p/abs(d))
801 if (r2 >= ad) { // must be unsigned comparison
802 q2 = q2 + 1;
803 r2 = r2 - ad;
804 }
805 delta = ad - r2;
806 } while (q1 < delta || (q1 == delta && r1 == 0));
807
808 mag.m = q2 + 1;
809 if (d < 0) mag.m = -mag.m; // resulting magic number
810 mag.s = p - 32; // resulting shift
811 return mag;
812}
813
814/// magicu - calculate the magic numbers required to codegen an integer udiv as
815/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
816static struct mu magicu(unsigned d)
817{
818 int p;
819 unsigned int nc, delta, q1, r1, q2, r2;
820 struct mu magu;
821 magu.a = 0; // initialize "add" indicator
822 nc = - 1 - (-d)%d;
823 p = 31; // initialize p
824 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
825 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
826 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
827 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
828 do {
829 p = p + 1;
830 if (r1 >= nc - r1 ) {
831 q1 = 2*q1 + 1; // update q1
832 r1 = 2*r1 - nc; // update r1
833 }
834 else {
835 q1 = 2*q1; // update q1
836 r1 = 2*r1; // update r1
837 }
838 if (r2 + 1 >= d - r2) {
839 if (q2 >= 0x7FFFFFFF) magu.a = 1;
840 q2 = 2*q2 + 1; // update q2
841 r2 = 2*r2 + 1 - d; // update r2
842 }
843 else {
844 if (q2 >= 0x80000000) magu.a = 1;
845 q2 = 2*q2; // update q2
846 r2 = 2*r2 + 1; // update r2
847 }
848 delta = d - 1 - r2;
849 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
850 magu.m = q2 + 1; // resulting magic number
851 magu.s = p - 32; // resulting shift
852 return magu;
853}
854}
855
856/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
857/// return a DAG expression to select that will generate the same value by
858/// multiplying by a magic number. See:
859/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
860SDOperand ISel::BuildSDIVSequence(SDOperand N) {
861 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
862 ms magics = magic(d);
863 // Multiply the numerator (operand 0) by the magic value
864 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
865 ISelDAG->getConstant(magics.m, MVT::i32));
866 // If d > 0 and m < 0, add the numerator
867 if (d > 0 && magics.m < 0)
868 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
869 // If d < 0 and m > 0, subtract the numerator.
870 if (d < 0 && magics.m > 0)
871 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
872 // Shift right algebraic if shift value is nonzero
873 if (magics.s > 0)
874 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
875 ISelDAG->getConstant(magics.s, MVT::i32));
876 // Extract the sign bit and add it to the quotient
877 SDOperand T =
878 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000879 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000880}
881
882/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
883/// return a DAG expression to select that will generate the same value by
884/// multiplying by a magic number. See:
885/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
886SDOperand ISel::BuildUDIVSequence(SDOperand N) {
887 unsigned d =
888 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
889 mu magics = magicu(d);
890 // Multiply the numerator (operand 0) by the magic value
891 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
892 ISelDAG->getConstant(magics.m, MVT::i32));
893 if (magics.a == 0) {
894 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
895 ISelDAG->getConstant(magics.s, MVT::i32));
896 } else {
897 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
898 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
899 ISelDAG->getConstant(1, MVT::i32));
900 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
901 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
902 ISelDAG->getConstant(magics.s-1, MVT::i32));
903 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000904 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000905}
906
Nate Begemanc7b09f12005-03-25 08:34:25 +0000907/// getGlobalBaseReg - Output the instructions required to put the
908/// base address to use for accessing globals into a register.
909///
910unsigned ISel::getGlobalBaseReg() {
911 if (!GlobalBaseInitialized) {
912 // Insert the set of GlobalBaseReg into the first MBB of the function
913 MachineBasicBlock &FirstMBB = BB->getParent()->front();
914 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
915 GlobalBaseReg = MakeReg(MVT::i32);
916 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
917 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
918 GlobalBaseInitialized = true;
919 }
920 return GlobalBaseReg;
921}
922
Nate Begeman6b559972005-04-01 02:59:27 +0000923/// getConstDouble - Loads a floating point value into a register, via the
924/// Constant Pool. Optionally takes a register in which to load the value.
925unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
926 unsigned Tmp1 = MakeReg(MVT::i32);
927 if (0 == Result) Result = MakeReg(MVT::f64);
928 MachineConstantPool *CP = BB->getParent()->getConstantPool();
929 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
930 unsigned CPI = CP->getConstantPoolIndex(CFP);
931 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
932 .addConstantPoolIndex(CPI);
933 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
934 return Result;
935}
936
Nate Begeman7ddecb42005-04-06 23:51:40 +0000937/// SelectBitfieldInsert - turn an or of two masked values into
938/// the rotate left word immediate then mask insert (rlwimi) instruction.
939/// Returns true on success, false if the caller still needs to select OR.
940///
941/// Patterns matched:
942/// 1. or shl, and 5. or and, and
943/// 2. or and, shl 6. or shl, shr
944/// 3. or shr, and 7. or shr, shl
945/// 4. or and, shr
946bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000947 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000948 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
949 unsigned Op0Opc = OR.getOperand(0).getOpcode();
950 unsigned Op1Opc = OR.getOperand(1).getOpcode();
951
952 // Verify that we have the correct opcodes
953 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
954 return false;
955 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
956 return false;
957
958 // Generate Mask value for Target
959 if (ConstantSDNode *CN =
960 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
961 switch(Op0Opc) {
962 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
963 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
964 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
965 }
966 } else {
967 return false;
968 }
969
970 // Generate Mask value for Insert
971 if (ConstantSDNode *CN =
972 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
973 switch(Op1Opc) {
974 case ISD::SHL:
975 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000976 InsMask <<= Amount;
977 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000978 break;
979 case ISD::SRL:
980 Amount = CN->getValue();
981 InsMask >>= Amount;
982 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000983 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000984 break;
985 case ISD::AND:
986 InsMask &= (unsigned)CN->getValue();
987 break;
988 }
989 } else {
990 return false;
991 }
992
993 // Verify that the Target mask and Insert mask together form a full word mask
994 // and that the Insert mask is a run of set bits (which implies both are runs
995 // of set bits). Given that, Select the arguments and generate the rlwimi
996 // instruction.
997 unsigned MB, ME;
998 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
999 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001000 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1001 // where both bitfield halves are sourced from the same value.
1002 if (IsRotate &&
1003 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001004 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1005 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1006 .addImm(0).addImm(31);
1007 return true;
1008 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001009 if (Op0Opc == ISD::AND)
1010 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1011 else
1012 Tmp1 = SelectExpr(OR.getOperand(0));
1013 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
1014 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1015 .addImm(Amount).addImm(MB).addImm(ME);
1016 return true;
1017 }
1018 return false;
1019}
1020
Nate Begeman3664cef2005-04-13 22:14:14 +00001021/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1022/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1023/// wider than the implicit mask, then we can get rid of the AND and let the
1024/// shift do the mask.
1025unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1026 unsigned C;
1027 if (N.getOpcode() == ISD::AND &&
1028 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1029 31 == (C & 0xFFFF) && // ME
1030 26 >= (C >> 16)) // MB
1031 return SelectExpr(N.getOperand(0));
1032 else
1033 return SelectExpr(N);
1034}
1035
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001036unsigned ISel::SelectCC(SDOperand CC, unsigned &Opc) {
1037 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001038 bool AlreadySelected = false;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001039 static const unsigned CompareOpcodes[] =
1040 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1041
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001042 // Allocate a condition register for this expression
1043 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1044
Nate Begemandffcfcc2005-04-01 00:32:34 +00001045 // If the first operand to the select is a SETCC node, then we can fold it
1046 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001047 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001048 bool U;
1049 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001050
Nate Begeman439b4442005-04-05 04:22:58 +00001051 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001052 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +00001053 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
1054 Tmp2, U)) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001055 // For comparisons against zero, we can implicity set CR0 if a recording
1056 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1057 // operand zero of the SetCC node is available.
1058 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001059 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1060 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001061 RecordSuccess = false;
1062 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1063 if (RecordSuccess) {
1064 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001065 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1066 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001067 }
1068 AlreadySelected = true;
1069 }
1070 // If we could not implicitly set CR0, then emit a compare immediate
1071 // instead.
1072 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001073 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001074 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001075 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001076 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001077 } else {
1078 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1079 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001080 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001081 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001082 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001083 }
1084 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001085 if (PPCCRopts)
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001086 if (CC.getOpcode() == ISD::AND || CC.getOpcode() == ISD::OR)
1087 if (CC.getOperand(0).Val->hasOneUse() &&
1088 CC.getOperand(1).Val->hasOneUse()) {
1089 SetCCSDNode* Op0CC = dyn_cast<SetCCSDNode>(CC.getOperand(0).Val);
1090 SetCCSDNode* Op1CC = dyn_cast<SetCCSDNode>(CC.getOperand(1).Val);
1091 if (Op0CC && Op1CC) {
1092 ++MultiBranch;
1093 bool Inv0, Inv1;
1094 unsigned Opc1;
1095 unsigned Idx0 = getCRIdxForSetCC(Op0CC->getCondition(), Inv0);
1096 unsigned Idx1 = getCRIdxForSetCC(Op1CC->getCondition(), Inv1);
1097 unsigned CROpc = getCROpForSetCC(CC.getOpcode(), Inv0, Inv1);
1098 Tmp1 = SelectCC(CC.getOperand(0), Opc);
1099 Tmp2 = SelectCC(CC.getOperand(1), Opc1);
1100 if (Inv0 && !Inv1) {
1101 std::swap(Tmp1, Tmp2);
1102 std::swap(Idx0, Idx1);
1103 Opc = Opc1;
1104 }
1105 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1106 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1107 .addReg(Tmp2).addImm(Idx1);
1108 return Result;
1109 }
1110 }
Nate Begeman9765c252005-04-12 21:22:28 +00001111 Opc = PPC::BNE;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001112 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001113 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001114 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001115 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001116}
1117
1118/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001119bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001120{
Nate Begeman96fc6812005-03-31 02:05:53 +00001121 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001122 if (N.getOpcode() == ISD::ADD) {
1123 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001124 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001125 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001126 return false;
1127 }
1128 offset = SelectExpr(N.getOperand(1));
1129 return true;
1130 }
Nate Begemana9795f82005-03-24 04:41:43 +00001131 Reg = SelectExpr(N);
1132 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001133 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001134}
1135
1136void ISel::SelectBranchCC(SDOperand N)
1137{
Nate Begemana9795f82005-03-24 04:41:43 +00001138 MachineBasicBlock *Dest =
1139 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001140
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001141 unsigned Opc, CCReg;
Nate Begemana9795f82005-03-24 04:41:43 +00001142 Select(N.getOperand(0)); //chain
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001143 CCReg = SelectCC(N.getOperand(1), Opc);
Nate Begemanf8b02942005-04-15 22:12:16 +00001144
Nate Begemancd08e4c2005-04-09 20:09:12 +00001145 // Iterate to the next basic block, unless we're already at the end of the
1146 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001147 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001148
1149 // If this is a two way branch, then grab the fallthrough basic block argument
1150 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1151 // if necessary by the branch selection pass. Otherwise, emit a standard
1152 // conditional branch.
1153 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1154 MachineBasicBlock *Fallthrough =
1155 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1156 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001157 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001158 .addMBB(Dest).addMBB(Fallthrough);
1159 if (Fallthrough != It)
1160 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1161 } else {
1162 if (Fallthrough != It) {
1163 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001164 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001165 .addMBB(Fallthrough).addMBB(Dest);
1166 }
1167 }
1168 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001169 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001170 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001171 }
Nate Begemana9795f82005-03-24 04:41:43 +00001172 return;
1173}
1174
1175unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1176{
1177 unsigned Tmp1, Tmp2, Tmp3;
1178 unsigned Opc = 0;
1179 SDNode *Node = N.Val;
1180 MVT::ValueType DestType = N.getValueType();
1181 unsigned opcode = N.getOpcode();
1182
1183 switch (opcode) {
1184 default:
1185 Node->dump();
1186 assert(0 && "Node not handled!\n");
1187
Nate Begeman23afcfb2005-03-29 22:48:55 +00001188 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001189 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1190 // and an FP comparison in the SetCC node.
1191 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1192 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1193 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1194 SetCC->getCondition() != ISD::SETEQ &&
1195 SetCC->getCondition() != ISD::SETNE) {
1196 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001197 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1198 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1199
1200 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1201 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1202 switch(SetCC->getCondition()) {
1203 default: assert(0 && "Invalid FSEL condition"); abort();
1204 case ISD::SETULT:
1205 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001206 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001207 case ISD::SETUGE:
1208 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001209 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001210 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1211 return Result;
1212 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001213 case ISD::SETGT:
1214 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001215 case ISD::SETULE:
1216 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001217 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1218 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1219 } else {
1220 Tmp2 = MakeReg(VT);
1221 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1222 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1223 }
Nate Begeman3e897162005-03-31 23:55:40 +00001224 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1225 return Result;
1226 }
1227 }
1228 } else {
1229 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001230 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001231 Tmp2 = SelectExpr(SetCC->getOperand(1));
1232 Tmp3 = MakeReg(VT);
1233 switch(SetCC->getCondition()) {
1234 default: assert(0 && "Invalid FSEL condition"); abort();
1235 case ISD::SETULT:
1236 case ISD::SETLT:
1237 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1238 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1239 return Result;
1240 case ISD::SETUGE:
1241 case ISD::SETGE:
1242 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1243 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1244 return Result;
1245 case ISD::SETUGT:
1246 case ISD::SETGT:
1247 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1248 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1249 return Result;
1250 case ISD::SETULE:
1251 case ISD::SETLE:
1252 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1253 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1254 return Result;
1255 }
1256 }
1257 assert(0 && "Should never get here");
1258 return 0;
1259 }
1260
Nate Begeman31318e42005-04-01 07:21:30 +00001261 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1262 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001263 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00001264
Nate Begeman23afcfb2005-03-29 22:48:55 +00001265 // Create an iterator with which to insert the MBB for copying the false
1266 // value and the MBB to hold the PHI instruction for this SetCC.
1267 MachineBasicBlock *thisMBB = BB;
1268 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1269 ilist<MachineBasicBlock>::iterator It = BB;
1270 ++It;
1271
1272 // thisMBB:
1273 // ...
1274 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001275 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001276 // bCC copy1MBB
1277 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001278 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1279 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001280 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001281 MachineFunction *F = BB->getParent();
1282 F->getBasicBlockList().insert(It, copy0MBB);
1283 F->getBasicBlockList().insert(It, sinkMBB);
1284 // Update machine-CFG edges
1285 BB->addSuccessor(copy0MBB);
1286 BB->addSuccessor(sinkMBB);
1287
1288 // copy0MBB:
1289 // %FalseValue = ...
1290 // # fallthrough to sinkMBB
1291 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001292 // Update machine-CFG edges
1293 BB->addSuccessor(sinkMBB);
1294
1295 // sinkMBB:
1296 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1297 // ...
1298 BB = sinkMBB;
1299 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1300 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1301 return Result;
1302 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001303
1304 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001305 if (!NoExcessFPPrecision &&
1306 ISD::ADD == N.getOperand(0).getOpcode() &&
1307 N.getOperand(0).Val->hasOneUse() &&
1308 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1309 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001310 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001311 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1312 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1313 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1314 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1315 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1316 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001317 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001318 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001319 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1320 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001321 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001322 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1323 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1324 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1325 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001326 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1327 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001328 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1329 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1330 } else {
1331 Tmp1 = SelectExpr(N.getOperand(0));
1332 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1333 }
1334 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001335
Nate Begeman27eeb002005-04-02 05:59:34 +00001336 case ISD::FABS:
1337 Tmp1 = SelectExpr(N.getOperand(0));
1338 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1339 return Result;
1340
Nate Begemana9795f82005-03-24 04:41:43 +00001341 case ISD::FP_ROUND:
1342 assert (DestType == MVT::f32 &&
1343 N.getOperand(0).getValueType() == MVT::f64 &&
1344 "only f64 to f32 conversion supported here");
1345 Tmp1 = SelectExpr(N.getOperand(0));
1346 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1347 return Result;
1348
1349 case ISD::FP_EXTEND:
1350 assert (DestType == MVT::f64 &&
1351 N.getOperand(0).getValueType() == MVT::f32 &&
1352 "only f32 to f64 conversion supported here");
1353 Tmp1 = SelectExpr(N.getOperand(0));
1354 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1355 return Result;
1356
1357 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001358 if (Result == 1)
1359 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1360 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1361 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1362 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001363
Nate Begeman6d369cc2005-04-01 01:08:07 +00001364 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001365 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001366 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001367 return Result;
1368 }
Nate Begemana9795f82005-03-24 04:41:43 +00001369
Nate Begemana9795f82005-03-24 04:41:43 +00001370 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001371 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1372 N.getOperand(0).Val->hasOneUse()) {
1373 ++FusedFP; // Statistic
1374 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1375 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1376 Tmp3 = SelectExpr(N.getOperand(1));
1377 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1378 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1379 return Result;
1380 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001381 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1382 N.getOperand(1).Val->hasOneUse()) {
1383 ++FusedFP; // Statistic
1384 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1385 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1386 Tmp3 = SelectExpr(N.getOperand(0));
1387 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1388 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1389 return Result;
1390 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001391 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1392 Tmp1 = SelectExpr(N.getOperand(0));
1393 Tmp2 = SelectExpr(N.getOperand(1));
1394 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1395 return Result;
1396
Nate Begemana9795f82005-03-24 04:41:43 +00001397 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001398 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1399 N.getOperand(0).Val->hasOneUse()) {
1400 ++FusedFP; // Statistic
1401 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1402 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1403 Tmp3 = SelectExpr(N.getOperand(1));
1404 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1405 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1406 return Result;
1407 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001408 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1409 N.getOperand(1).Val->hasOneUse()) {
1410 ++FusedFP; // Statistic
1411 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1412 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1413 Tmp3 = SelectExpr(N.getOperand(0));
1414 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1415 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1416 return Result;
1417 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001418 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1419 Tmp1 = SelectExpr(N.getOperand(0));
1420 Tmp2 = SelectExpr(N.getOperand(1));
1421 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1422 return Result;
1423
1424 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001425 case ISD::SDIV:
1426 switch( opcode ) {
1427 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001428 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1429 };
Nate Begemana9795f82005-03-24 04:41:43 +00001430 Tmp1 = SelectExpr(N.getOperand(0));
1431 Tmp2 = SelectExpr(N.getOperand(1));
1432 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1433 return Result;
1434
Nate Begemana9795f82005-03-24 04:41:43 +00001435 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001436 case ISD::SINT_TO_FP: {
1437 assert (N.getOperand(0).getValueType() == MVT::i32
1438 && "int to float must operate on i32");
1439 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1440 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1441 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1442 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001443
1444 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1445 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1446
Nate Begemanfdcf3412005-03-30 19:38:35 +00001447 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001448 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001449 // Store the hi & low halves of the fp value, currently in int regs
1450 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1451 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1452 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1453 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1454 // Generate the return value with a subtract
1455 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1456 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001457 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001458 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001459 // Store the hi & low halves of the fp value, currently in int regs
1460 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1461 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1462 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1463 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1464 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1465 // Generate the return value with a subtract
1466 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1467 }
1468 return Result;
1469 }
Nate Begemana9795f82005-03-24 04:41:43 +00001470 }
Nate Begeman6b559972005-04-01 02:59:27 +00001471 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001472 return 0;
1473}
1474
Nate Begemanc7bd4822005-04-11 06:34:10 +00001475unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001476 unsigned Result;
1477 unsigned Tmp1, Tmp2, Tmp3;
1478 unsigned Opc = 0;
1479 unsigned opcode = N.getOpcode();
1480
1481 SDNode *Node = N.Val;
1482 MVT::ValueType DestType = N.getValueType();
1483
1484 unsigned &Reg = ExprMap[N];
1485 if (Reg) return Reg;
1486
Nate Begeman27eeb002005-04-02 05:59:34 +00001487 switch (N.getOpcode()) {
1488 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001489 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001490 MakeReg(N.getValueType()) : 1;
1491 break;
1492 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001493 // If this is a call instruction, make sure to prepare ALL of the result
1494 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001495 if (Node->getNumValues() == 1)
1496 Reg = Result = 1; // Void call, just a chain.
1497 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001498 Result = MakeReg(Node->getValueType(0));
1499 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001500 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001501 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001502 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001503 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001504 break;
1505 case ISD::ADD_PARTS:
1506 case ISD::SUB_PARTS:
1507 case ISD::SHL_PARTS:
1508 case ISD::SRL_PARTS:
1509 case ISD::SRA_PARTS:
1510 Result = MakeReg(Node->getValueType(0));
1511 ExprMap[N.getValue(0)] = Result;
1512 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1513 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1514 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001515 }
1516
Nate Begemane5846682005-04-04 06:52:38 +00001517 if (ISD::CopyFromReg == opcode)
1518 DestType = N.getValue(0).getValueType();
1519
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001520 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001521 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1522 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001523 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001524
1525 switch (opcode) {
1526 default:
1527 Node->dump();
1528 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001529 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001530 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1531 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001532 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001533 // Generate both result values. FIXME: Need a better commment here?
1534 if (Result != 1)
1535 ExprMap[N.getValue(1)] = 1;
1536 else
1537 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1538
1539 // FIXME: We are currently ignoring the requested alignment for handling
1540 // greater than the stack alignment. This will need to be revisited at some
1541 // point. Align = N.getOperand(2);
1542 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1543 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1544 std::cerr << "Cannot allocate stack object with greater alignment than"
1545 << " the stack alignment yet!";
1546 abort();
1547 }
1548 Select(N.getOperand(0));
1549 Tmp1 = SelectExpr(N.getOperand(1));
1550 // Subtract size from stack pointer, thereby allocating some space.
1551 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1552 // Put a pointer to the space into the result register by copying the SP
1553 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1554 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001555
1556 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001557 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1558 Tmp2 = MakeReg(MVT::i32);
1559 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1560 .addConstantPoolIndex(Tmp1);
1561 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1562 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001563
1564 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001565 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001566 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001567 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001568
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001569 case ISD::GlobalAddress: {
1570 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001571 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001572 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1573 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001574 if (GV->hasWeakLinkage() || GV->isExternal()) {
1575 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1576 } else {
1577 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1578 }
1579 return Result;
1580 }
1581
Nate Begeman5e966612005-03-24 06:28:42 +00001582 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001583 case ISD::EXTLOAD:
1584 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001585 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001586 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1587 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001588 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001589
Nate Begeman5e966612005-03-24 06:28:42 +00001590 // Make sure we generate both values.
1591 if (Result != 1)
1592 ExprMap[N.getValue(1)] = 1; // Generate the token
1593 else
1594 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1595
1596 SDOperand Chain = N.getOperand(0);
1597 SDOperand Address = N.getOperand(1);
1598 Select(Chain);
1599
Nate Begeman9db505c2005-03-28 19:36:43 +00001600 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001601 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001602 case MVT::i1: Opc = PPC::LBZ; break;
1603 case MVT::i8: Opc = PPC::LBZ; break;
1604 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1605 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001606 case MVT::f32: Opc = PPC::LFS; break;
1607 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001608 }
1609
Nate Begeman74d73452005-03-31 00:15:26 +00001610 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1611 Tmp1 = MakeReg(MVT::i32);
1612 int CPI = CP->getIndex();
1613 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1614 .addConstantPoolIndex(CPI);
1615 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001616 }
Nate Begeman74d73452005-03-31 00:15:26 +00001617 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001618 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1619 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001620 } else {
1621 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001622 bool idx = SelectAddr(Address, Tmp1, offset);
1623 if (idx) {
1624 Opc = IndexedOpForOp(Opc);
1625 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1626 } else {
1627 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1628 }
Nate Begeman5e966612005-03-24 06:28:42 +00001629 }
1630 return Result;
1631 }
1632
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001633 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001634 unsigned GPR_idx = 0, FPR_idx = 0;
1635 static const unsigned GPR[] = {
1636 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1637 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1638 };
1639 static const unsigned FPR[] = {
1640 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1641 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1642 };
1643
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001644 // Lower the chain for this call.
1645 Select(N.getOperand(0));
1646 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001647
Nate Begemand860aa62005-04-04 22:17:48 +00001648 MachineInstr *CallMI;
1649 // Emit the correct call instruction based on the type of symbol called.
1650 if (GlobalAddressSDNode *GASD =
1651 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1652 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1653 true);
1654 } else if (ExternalSymbolSDNode *ESSDN =
1655 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1656 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1657 true);
1658 } else {
1659 Tmp1 = SelectExpr(N.getOperand(1));
1660 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1661 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1662 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1663 .addReg(PPC::R12);
1664 }
1665
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001666 // Load the register args to virtual regs
1667 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001668 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001669 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1670
1671 // Copy the virtual registers into the appropriate argument register
1672 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1673 switch(N.getOperand(i+2).getValueType()) {
1674 default: Node->dump(); assert(0 && "Unknown value type for call");
1675 case MVT::i1:
1676 case MVT::i8:
1677 case MVT::i16:
1678 case MVT::i32:
1679 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001680 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001681 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001682 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1683 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001684 ++GPR_idx;
1685 break;
1686 case MVT::f64:
1687 case MVT::f32:
1688 assert(FPR_idx < 13 && "Too many fp args");
1689 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001690 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001691 ++FPR_idx;
1692 break;
1693 }
1694 }
Nate Begemand860aa62005-04-04 22:17:48 +00001695
1696 // Put the call instruction in the correct place in the MachineBasicBlock
1697 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001698
1699 switch (Node->getValueType(0)) {
1700 default: assert(0 && "Unknown value type for call result!");
1701 case MVT::Other: return 1;
1702 case MVT::i1:
1703 case MVT::i8:
1704 case MVT::i16:
1705 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001706 if (Node->getValueType(1) == MVT::i32) {
1707 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1708 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1709 } else {
1710 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1711 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001712 break;
1713 case MVT::f32:
1714 case MVT::f64:
1715 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1716 break;
1717 }
1718 return Result+N.ResNo;
1719 }
Nate Begemana9795f82005-03-24 04:41:43 +00001720
1721 case ISD::SIGN_EXTEND:
1722 case ISD::SIGN_EXTEND_INREG:
1723 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001724 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1725 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001726 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001727 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1728 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001729 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001730 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1731 break;
Nate Begeman74747862005-03-29 22:24:51 +00001732 case MVT::i1:
1733 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1734 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001735 }
Nate Begemana9795f82005-03-24 04:41:43 +00001736 return Result;
1737
Nate Begemana9795f82005-03-24 04:41:43 +00001738 case ISD::CopyFromReg:
1739 if (Result == 1)
1740 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1741 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1742 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1743 return Result;
1744
1745 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001746 Tmp1 = SelectExpr(N.getOperand(0));
1747 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1748 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001749 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001750 .addImm(31-Tmp2);
1751 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001752 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001753 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1754 }
1755 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001756
Nate Begeman5e966612005-03-24 06:28:42 +00001757 case ISD::SRL:
1758 Tmp1 = SelectExpr(N.getOperand(0));
1759 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1760 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001761 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001762 .addImm(Tmp2).addImm(31);
1763 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001764 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001765 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1766 }
1767 return Result;
1768
1769 case ISD::SRA:
1770 Tmp1 = SelectExpr(N.getOperand(0));
1771 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1772 Tmp2 = CN->getValue() & 0x1F;
1773 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1774 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001775 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001776 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1777 }
1778 return Result;
1779
Nate Begemana9795f82005-03-24 04:41:43 +00001780 case ISD::ADD:
1781 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1782 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001783 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001784 default: assert(0 && "unhandled result code");
1785 case 0: // No immediate
1786 Tmp2 = SelectExpr(N.getOperand(1));
1787 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1788 break;
1789 case 1: // Low immediate
1790 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1791 break;
1792 case 2: // Shifted immediate
1793 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1794 break;
1795 }
1796 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001797
Nate Begemana9795f82005-03-24 04:41:43 +00001798 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001799 Tmp1 = SelectExpr(N.getOperand(0));
1800 // FIXME: should add check in getImmediateForOpcode to return a value
1801 // indicating the immediate is a run of set bits so we can emit a bitfield
1802 // clear with RLWINM instead.
1803 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1804 default: assert(0 && "unhandled result code");
1805 case 0: // No immediate
1806 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001807 Opc = Recording ? PPC::ANDo : PPC::AND;
1808 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001809 break;
1810 case 1: // Low immediate
1811 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1812 break;
1813 case 2: // Shifted immediate
1814 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1815 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001816 case 5: // Bitfield mask
1817 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1818 Tmp3 = Tmp2 >> 16; // MB
1819 Tmp2 &= 0xFFFF; // ME
1820 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1821 .addImm(Tmp3).addImm(Tmp2);
1822 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001823 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001824 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001825 return Result;
1826
Nate Begemana9795f82005-03-24 04:41:43 +00001827 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001828 if (SelectBitfieldInsert(N, Result))
1829 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001830 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001831 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001832 default: assert(0 && "unhandled result code");
1833 case 0: // No immediate
1834 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001835 Opc = Recording ? PPC::ORo : PPC::OR;
1836 RecordSuccess = true;
1837 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001838 break;
1839 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001840 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001841 break;
1842 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001843 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001844 break;
1845 }
1846 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001847
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001848 case ISD::XOR: {
1849 // Check for EQV: xor, (xor a, -1), b
1850 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1851 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1852 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001853 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1854 Tmp2 = SelectExpr(N.getOperand(1));
1855 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1856 return Result;
1857 }
1858 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1859 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1860 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001861 switch(N.getOperand(0).getOpcode()) {
1862 case ISD::OR:
1863 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1864 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1865 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1866 break;
1867 case ISD::AND:
1868 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1869 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1870 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 break;
1872 default:
1873 Tmp1 = SelectExpr(N.getOperand(0));
1874 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1875 break;
1876 }
1877 return Result;
1878 }
1879 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001880 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001881 default: assert(0 && "unhandled result code");
1882 case 0: // No immediate
1883 Tmp2 = SelectExpr(N.getOperand(1));
1884 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1885 break;
1886 case 1: // Low immediate
1887 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1888 break;
1889 case 2: // Shifted immediate
1890 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1891 break;
1892 }
1893 return Result;
1894 }
1895
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001896 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001897 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001898 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001899 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1900 else {
1901 Tmp1 = SelectExpr(N.getOperand(0));
1902 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1903 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001904 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001905
Nate Begeman5e966612005-03-24 06:28:42 +00001906 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001907 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001908 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001909 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1910 else {
1911 Tmp2 = SelectExpr(N.getOperand(1));
1912 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1913 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001914 return Result;
1915
Nate Begeman815d6da2005-04-06 00:25:27 +00001916 case ISD::MULHS:
1917 case ISD::MULHU:
1918 Tmp1 = SelectExpr(N.getOperand(0));
1919 Tmp2 = SelectExpr(N.getOperand(1));
1920 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1921 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1922 return Result;
1923
Nate Begemanf3d08f32005-03-29 00:03:27 +00001924 case ISD::SDIV:
1925 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001926 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1927 default: break;
1928 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1929 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001930 Tmp1 = MakeReg(MVT::i32);
1931 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001932 if ((int)Tmp3 < 0) {
1933 unsigned Tmp4 = MakeReg(MVT::i32);
1934 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1935 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1936 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1937 } else {
1938 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1939 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1940 }
Nate Begeman80196b12005-04-05 00:15:08 +00001941 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001942 // If this is a divide by constant, we can emit code using some magic
1943 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001944 case 4:
1945 ExprMap.erase(N);
1946 if (opcode == ISD::SDIV)
1947 return SelectExpr(BuildSDIVSequence(N));
1948 else
1949 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001950 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001951 Tmp1 = SelectExpr(N.getOperand(0));
1952 Tmp2 = SelectExpr(N.getOperand(1));
1953 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1954 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1955 return Result;
1956
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001957 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001958 case ISD::SUB_PARTS: {
1959 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1960 "Not an i64 add/sub!");
1961 // Emit all of the operands.
1962 std::vector<unsigned> InVals;
1963 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1964 InVals.push_back(SelectExpr(N.getOperand(i)));
1965 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001966 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1967 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001968 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001969 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1970 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1971 }
1972 return Result+N.ResNo;
1973 }
1974
1975 case ISD::SHL_PARTS:
1976 case ISD::SRA_PARTS:
1977 case ISD::SRL_PARTS: {
1978 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1979 "Not an i64 shift!");
1980 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1981 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001982 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1983 Tmp1 = MakeReg(MVT::i32);
1984 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001985 Tmp3 = MakeReg(MVT::i32);
1986 unsigned Tmp4 = MakeReg(MVT::i32);
1987 unsigned Tmp5 = MakeReg(MVT::i32);
1988 unsigned Tmp6 = MakeReg(MVT::i32);
1989 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1990 if (ISD::SHL_PARTS == opcode) {
1991 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1992 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1993 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1994 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001995 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001996 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1997 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1998 } else if (ISD::SRL_PARTS == opcode) {
1999 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2000 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2001 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2002 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2003 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2004 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2005 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2006 } else {
2007 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2008 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2009 MachineBasicBlock *OldMBB = BB;
2010 MachineFunction *F = BB->getParent();
2011 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2012 F->getBasicBlockList().insert(It, TmpMBB);
2013 F->getBasicBlockList().insert(It, PhiMBB);
2014 BB->addSuccessor(TmpMBB);
2015 BB->addSuccessor(PhiMBB);
2016 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2017 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2018 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2019 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2020 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2021 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2022 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2023 // Select correct least significant half if the shift amount > 32
2024 BB = TmpMBB;
2025 unsigned Tmp7 = MakeReg(MVT::i32);
2026 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2027 TmpMBB->addSuccessor(PhiMBB);
2028 BB = PhiMBB;
2029 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2030 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002031 }
2032 return Result+N.ResNo;
2033 }
2034
Nate Begemana9795f82005-03-24 04:41:43 +00002035 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002036 case ISD::FP_TO_SINT: {
2037 bool U = (ISD::FP_TO_UINT == opcode);
2038 Tmp1 = SelectExpr(N.getOperand(0));
2039 if (!U) {
2040 Tmp2 = MakeReg(MVT::f64);
2041 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2042 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2043 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2044 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2045 return Result;
2046 } else {
2047 unsigned Zero = getConstDouble(0.0);
2048 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2049 unsigned Border = getConstDouble(1LL << 31);
2050 unsigned UseZero = MakeReg(MVT::f64);
2051 unsigned UseMaxInt = MakeReg(MVT::f64);
2052 unsigned UseChoice = MakeReg(MVT::f64);
2053 unsigned TmpReg = MakeReg(MVT::f64);
2054 unsigned TmpReg2 = MakeReg(MVT::f64);
2055 unsigned ConvReg = MakeReg(MVT::f64);
2056 unsigned IntTmp = MakeReg(MVT::i32);
2057 unsigned XorReg = MakeReg(MVT::i32);
2058 MachineFunction *F = BB->getParent();
2059 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2060 // Update machine-CFG edges
2061 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2062 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2063 MachineBasicBlock *OldMBB = BB;
2064 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2065 F->getBasicBlockList().insert(It, XorMBB);
2066 F->getBasicBlockList().insert(It, PhiMBB);
2067 BB->addSuccessor(XorMBB);
2068 BB->addSuccessor(PhiMBB);
2069 // Convert from floating point to unsigned 32-bit value
2070 // Use 0 if incoming value is < 0.0
2071 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2072 // Use 2**32 - 1 if incoming value is >= 2**32
2073 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2074 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2075 .addReg(MaxInt);
2076 // Subtract 2**31
2077 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2078 // Use difference if >= 2**31
2079 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2080 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2081 .addReg(UseChoice);
2082 // Convert to integer
2083 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2084 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2085 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2086 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2087 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2088
2089 // XorMBB:
2090 // add 2**31 if input was >= 2**31
2091 BB = XorMBB;
2092 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2093 XorMBB->addSuccessor(PhiMBB);
2094
2095 // PhiMBB:
2096 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2097 BB = PhiMBB;
2098 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2099 .addReg(XorReg).addMBB(XorMBB);
2100 return Result;
2101 }
2102 assert(0 && "Should never get here");
2103 return 0;
2104 }
Nate Begemana9795f82005-03-24 04:41:43 +00002105
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002106 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002107 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002108 if (ConstantSDNode *CN =
2109 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002110 // We can codegen setcc op, imm very efficiently compared to a brcond.
2111 // Check for those cases here.
2112 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002113 if (CN->getValue() == 0) {
2114 Tmp1 = SelectExpr(SetCC->getOperand(0));
2115 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002116 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002117 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002118 Tmp2 = MakeReg(MVT::i32);
2119 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2120 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2121 .addImm(5).addImm(31);
2122 break;
2123 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002124 Tmp2 = MakeReg(MVT::i32);
2125 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2126 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2127 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002128 case ISD::SETLT:
2129 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2130 .addImm(31).addImm(31);
2131 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002132 case ISD::SETGT:
2133 Tmp2 = MakeReg(MVT::i32);
2134 Tmp3 = MakeReg(MVT::i32);
2135 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2136 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2137 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2138 .addImm(31).addImm(31);
2139 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002140 }
2141 return Result;
2142 }
2143 // setcc op, -1
2144 if (CN->isAllOnesValue()) {
2145 Tmp1 = SelectExpr(SetCC->getOperand(0));
2146 switch (SetCC->getCondition()) {
2147 default: assert(0 && "Unhandled SetCC condition"); abort();
2148 case ISD::SETEQ:
2149 Tmp2 = MakeReg(MVT::i32);
2150 Tmp3 = MakeReg(MVT::i32);
2151 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2152 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2153 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002154 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002155 case ISD::SETNE:
2156 Tmp2 = MakeReg(MVT::i32);
2157 Tmp3 = MakeReg(MVT::i32);
2158 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2159 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2160 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2161 break;
2162 case ISD::SETLT:
2163 Tmp2 = MakeReg(MVT::i32);
2164 Tmp3 = MakeReg(MVT::i32);
2165 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2166 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2167 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2168 .addImm(31).addImm(31);
2169 break;
2170 case ISD::SETGT:
2171 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002172 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2173 .addImm(31).addImm(31);
2174 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2175 break;
2176 }
2177 return Result;
2178 }
2179 }
2180
Nate Begeman16ac7092005-04-18 02:43:24 +00002181 bool Inv = false;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002182 unsigned CCReg = SelectCC(N, Opc);
Nate Begeman16ac7092005-04-18 02:43:24 +00002183 unsigned IntCR = MakeReg(MVT::i32);
2184 unsigned ShAmt = getCRIdxForBCC(Opc, Inv);
2185 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
2186 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
2187 if (Inv) {
2188 Tmp1 = MakeReg(MVT::i32);
2189 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(ShAmt)
2190 .addImm(31).addImm(31);
2191 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
2192 } else {
2193 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(ShAmt)
2194 .addImm(31).addImm(31);
2195 }
Nate Begeman33162522005-03-29 21:54:38 +00002196 return Result;
2197 }
2198 assert(0 && "Is this legal?");
2199 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002200
Nate Begeman74747862005-03-29 22:24:51 +00002201 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00002202 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2203 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002204 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Chris Lattner30710192005-04-01 07:10:02 +00002205
Nate Begeman74747862005-03-29 22:24:51 +00002206 // Create an iterator with which to insert the MBB for copying the false
2207 // value and the MBB to hold the PHI instruction for this SetCC.
2208 MachineBasicBlock *thisMBB = BB;
2209 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2210 ilist<MachineBasicBlock>::iterator It = BB;
2211 ++It;
2212
2213 // thisMBB:
2214 // ...
2215 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002216 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002217 // bCC copy1MBB
2218 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002219 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2220 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002221 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002222 MachineFunction *F = BB->getParent();
2223 F->getBasicBlockList().insert(It, copy0MBB);
2224 F->getBasicBlockList().insert(It, sinkMBB);
2225 // Update machine-CFG edges
2226 BB->addSuccessor(copy0MBB);
2227 BB->addSuccessor(sinkMBB);
2228
2229 // copy0MBB:
2230 // %FalseValue = ...
2231 // # fallthrough to sinkMBB
2232 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002233 // Update machine-CFG edges
2234 BB->addSuccessor(sinkMBB);
2235
2236 // sinkMBB:
2237 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2238 // ...
2239 BB = sinkMBB;
2240 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2241 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002242 return Result;
2243 }
Nate Begemana9795f82005-03-24 04:41:43 +00002244
2245 case ISD::Constant:
2246 switch (N.getValueType()) {
2247 default: assert(0 && "Cannot use constants of this type!");
2248 case MVT::i1:
2249 BuildMI(BB, PPC::LI, 1, Result)
2250 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2251 break;
2252 case MVT::i32:
2253 {
2254 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2255 if (v < 32768 && v >= -32768) {
2256 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2257 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002258 Tmp1 = MakeReg(MVT::i32);
2259 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2260 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002261 }
2262 }
2263 }
2264 return Result;
2265 }
2266
2267 return 0;
2268}
2269
2270void ISel::Select(SDOperand N) {
2271 unsigned Tmp1, Tmp2, Opc;
2272 unsigned opcode = N.getOpcode();
2273
2274 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2275 return; // Already selected.
2276
2277 SDNode *Node = N.Val;
2278
2279 switch (Node->getOpcode()) {
2280 default:
2281 Node->dump(); std::cerr << "\n";
2282 assert(0 && "Node not handled yet!");
2283 case ISD::EntryToken: return; // Noop
2284 case ISD::TokenFactor:
2285 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2286 Select(Node->getOperand(i));
2287 return;
2288 case ISD::ADJCALLSTACKDOWN:
2289 case ISD::ADJCALLSTACKUP:
2290 Select(N.getOperand(0));
2291 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2292 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2293 PPC::ADJCALLSTACKUP;
2294 BuildMI(BB, Opc, 1).addImm(Tmp1);
2295 return;
2296 case ISD::BR: {
2297 MachineBasicBlock *Dest =
2298 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002299 Select(N.getOperand(0));
2300 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2301 return;
2302 }
2303 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002304 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002305 SelectBranchCC(N);
2306 return;
2307 case ISD::CopyToReg:
2308 Select(N.getOperand(0));
2309 Tmp1 = SelectExpr(N.getOperand(1));
2310 Tmp2 = cast<RegSDNode>(N)->getReg();
2311
2312 if (Tmp1 != Tmp2) {
2313 if (N.getOperand(1).getValueType() == MVT::f64 ||
2314 N.getOperand(1).getValueType() == MVT::f32)
2315 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2316 else
2317 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2318 }
2319 return;
2320 case ISD::ImplicitDef:
2321 Select(N.getOperand(0));
2322 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2323 return;
2324 case ISD::RET:
2325 switch (N.getNumOperands()) {
2326 default:
2327 assert(0 && "Unknown return instruction!");
2328 case 3:
2329 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2330 N.getOperand(2).getValueType() == MVT::i32 &&
2331 "Unknown two-register value!");
2332 Select(N.getOperand(0));
2333 Tmp1 = SelectExpr(N.getOperand(1));
2334 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002335 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2336 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002337 break;
2338 case 2:
2339 Select(N.getOperand(0));
2340 Tmp1 = SelectExpr(N.getOperand(1));
2341 switch (N.getOperand(1).getValueType()) {
2342 default:
2343 assert(0 && "Unknown return type!");
2344 case MVT::f64:
2345 case MVT::f32:
2346 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2347 break;
2348 case MVT::i32:
2349 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2350 break;
2351 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002352 case 1:
2353 Select(N.getOperand(0));
2354 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002355 }
2356 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2357 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002358 case ISD::TRUNCSTORE:
2359 case ISD::STORE:
2360 {
2361 SDOperand Chain = N.getOperand(0);
2362 SDOperand Value = N.getOperand(1);
2363 SDOperand Address = N.getOperand(2);
2364 Select(Chain);
2365
2366 Tmp1 = SelectExpr(Value); //value
2367
2368 if (opcode == ISD::STORE) {
2369 switch(Value.getValueType()) {
2370 default: assert(0 && "unknown Type in store");
2371 case MVT::i32: Opc = PPC::STW; break;
2372 case MVT::f64: Opc = PPC::STFD; break;
2373 case MVT::f32: Opc = PPC::STFS; break;
2374 }
2375 } else { //ISD::TRUNCSTORE
2376 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2377 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002378 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002379 case MVT::i8: Opc = PPC::STB; break;
2380 case MVT::i16: Opc = PPC::STH; break;
2381 }
2382 }
2383
Nate Begemana7e11a42005-04-01 05:57:17 +00002384 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002385 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002386 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2387 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002388 }
2389 else
2390 {
2391 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002392 bool idx = SelectAddr(Address, Tmp2, offset);
2393 if (idx) {
2394 Opc = IndexedOpForOp(Opc);
2395 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2396 } else {
2397 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2398 }
Nate Begemana9795f82005-03-24 04:41:43 +00002399 }
2400 return;
2401 }
2402 case ISD::EXTLOAD:
2403 case ISD::SEXTLOAD:
2404 case ISD::ZEXTLOAD:
2405 case ISD::LOAD:
2406 case ISD::CopyFromReg:
2407 case ISD::CALL:
2408 case ISD::DYNAMIC_STACKALLOC:
2409 ExprMap.erase(N);
2410 SelectExpr(N);
2411 return;
2412 }
2413 assert(0 && "Should not be reached!");
2414}
2415
2416
2417/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2418/// into a machine code representation using pattern matching and a machine
2419/// description file.
2420///
2421FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2422 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002423}
2424