blob: 6d79c9c7a44525ccc0ee119c8ca2cf65c247aec4 [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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC32RegisterInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/ADT/Statistic.h"
31#include <set>
32#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
37namespace {
38 class PPC32TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
40 int ReturnAddrIndex; // FrameIndex for return slot.
41 public:
42 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000043 // Set up the register classes.
44 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000045 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000046 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
47
Nate Begeman74d73452005-03-31 00:15:26 +000048 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000049 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
50 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
51 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
52
Nate Begeman74d73452005-03-31 00:15:26 +000053 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
54 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
55 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000056
Nate Begeman27eeb002005-04-02 05:59:34 +000057 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Nate Begeman3e897162005-03-31 23:55:40 +000058 addLegalFPImmediate(+0.0); // Necessary for FSEL
59 addLegalFPImmediate(-0.0); //
60
Nate Begemana9795f82005-03-24 04:41:43 +000061 computeRegisterProperties();
62 }
63
64 /// LowerArguments - This hook must be implemented to indicate how we should
65 /// lower the arguments for the specified function, into the specified DAG.
66 virtual std::vector<SDOperand>
67 LowerArguments(Function &F, SelectionDAG &DAG);
68
69 /// LowerCallTo - This hook lowers an abstract call to a function into an
70 /// actual call.
71 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000072 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
73 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000074
75 virtual std::pair<SDOperand, SDOperand>
76 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
77
78 virtual std::pair<SDOperand,SDOperand>
79 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
80 const Type *ArgTy, SelectionDAG &DAG);
81
82 virtual std::pair<SDOperand, SDOperand>
83 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
84 SelectionDAG &DAG);
85 };
86}
87
88
89std::vector<SDOperand>
90PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
91 //
92 // add beautiful description of PPC stack frame format, or at least some docs
93 //
94 MachineFunction &MF = DAG.getMachineFunction();
95 MachineFrameInfo *MFI = MF.getFrameInfo();
96 MachineBasicBlock& BB = MF.front();
97 std::vector<SDOperand> ArgValues;
98
99 // Due to the rather complicated nature of the PowerPC ABI, rather than a
100 // fixed size array of physical args, for the sake of simplicity let the STL
101 // handle tracking them for us.
102 std::vector<unsigned> argVR, argPR, argOp;
103 unsigned ArgOffset = 24;
104 unsigned GPR_remaining = 8;
105 unsigned FPR_remaining = 13;
106 unsigned GPR_idx = 0, FPR_idx = 0;
107 static const unsigned GPR[] = {
108 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
109 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
110 };
111 static const unsigned FPR[] = {
112 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
113 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
114 };
115
116 // Add DAG nodes to load the arguments... On entry to a function on PPC,
117 // the arguments start at offset 24, although they are likely to be passed
118 // in registers.
119 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
120 SDOperand newroot, argt;
121 unsigned ObjSize;
122 bool needsLoad = false;
123 MVT::ValueType ObjectVT = getValueType(I->getType());
124
125 switch (ObjectVT) {
126 default: assert(0 && "Unhandled argument type!");
127 case MVT::i1:
128 case MVT::i8:
129 case MVT::i16:
130 case MVT::i32:
131 ObjSize = 4;
132 if (GPR_remaining > 0) {
133 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000134 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
135 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000136 if (ObjectVT != MVT::i32)
137 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000138 } else {
139 needsLoad = true;
140 }
141 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000142 case MVT::i64: ObjSize = 8;
143 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000144 if (GPR_remaining > 1) {
145 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
146 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000147 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000148 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
149 DAG.getRoot());
150 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000151 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000152 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
153 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000154 } else {
155 needsLoad = true;
156 }
157 break;
158 case MVT::f32: ObjSize = 4;
159 case MVT::f64: ObjSize = 8;
160 if (FPR_remaining > 0) {
161 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000162 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
163 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000164 --FPR_remaining;
165 ++FPR_idx;
166 } else {
167 needsLoad = true;
168 }
169 break;
170 }
171
172 // We need to load the argument to a virtual register if we determined above
173 // that we ran out of physical registers of the appropriate type
174 if (needsLoad) {
175 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
176 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
177 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
178 }
179
180 // Every 4 bytes of argument space consumes one of the GPRs available for
181 // argument passing.
182 if (GPR_remaining > 0) {
183 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
184 GPR_remaining -= delta;
185 GPR_idx += delta;
186 }
187 ArgOffset += ObjSize;
188
189 DAG.setRoot(newroot.getValue(1));
190 ArgValues.push_back(argt);
191 }
192
Nate Begemana9795f82005-03-24 04:41:43 +0000193 // If the function takes variable number of arguments, make a frame index for
194 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000195 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000196 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000197 // If this function is vararg, store r4-r10 to their spots on the stack so
198 // that they may be loaded by dereferencing va_next
199 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
200 SDOperand Val = DAG.getCopyFromReg(PPC::R4, MVT::i32, DAG.getRoot());
Nate Begeman4ec0cbd2005-04-03 22:22:56 +0000201 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), Val, FIN);
202 DAG.setRoot(Store);
Nate Begemanfa554702005-04-03 22:13:27 +0000203 }
Nate Begemana9795f82005-03-24 04:41:43 +0000204
205 return ArgValues;
206}
207
208std::pair<SDOperand, SDOperand>
209PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000210 const Type *RetTy, bool isVarArg,
211 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
212 // args_to_use will accumulate outgoing args for the ISD::CALL case in
213 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000214 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000215
216 // Count how many bytes are to be pushed on the stack, including the linkage
217 // area, and parameter passing area.
218 unsigned NumBytes = 24;
219
220 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000221 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
222 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000223 } else {
224 for (unsigned i = 0, e = Args.size(); i != e; ++i)
225 switch (getValueType(Args[i].second)) {
226 default: assert(0 && "Unknown value type!");
227 case MVT::i1:
228 case MVT::i8:
229 case MVT::i16:
230 case MVT::i32:
231 case MVT::f32:
232 NumBytes += 4;
233 break;
234 case MVT::i64:
235 case MVT::f64:
236 NumBytes += 8;
237 break;
238 }
239
240 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
241 // plus 32 bytes of argument space in case any called code gets funky on us.
242 if (NumBytes < 56) NumBytes = 56;
243
244 // Adjust the stack pointer for the new arguments...
245 // These operations are automatically eliminated by the prolog/epilog pass
246 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
247 DAG.getConstant(NumBytes, getPointerTy()));
248
249 // Set up a copy of the stack pointer for use loading and storing any
250 // arguments that may not fit in the registers available for argument
251 // passing.
252 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
253 DAG.getEntryNode());
254
255 // Figure out which arguments are going to go in registers, and which in
256 // memory. Also, if this is a vararg function, floating point operations
257 // must be stored to our stack, and loaded into integer regs as well, if
258 // any integer regs are available for argument passing.
259 unsigned ArgOffset = 24;
260 unsigned GPR_remaining = 8;
261 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000262
263 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000264 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
265 // PtrOff will be used to store the current argument to the stack if a
266 // register cannot be found for it.
267 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
268 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000269 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000270
Nate Begemanf7e43382005-03-26 07:46:36 +0000271 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000272 default: assert(0 && "Unexpected ValueType for argument!");
273 case MVT::i1:
274 case MVT::i8:
275 case MVT::i16:
276 // Promote the integer to 32 bits. If the input type is signed use a
277 // sign extend, otherwise use a zero extend.
278 if (Args[i].second->isSigned())
279 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
280 else
281 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
282 // FALL THROUGH
283 case MVT::i32:
284 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000285 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000286 --GPR_remaining;
287 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000288 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
289 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000290 }
291 ArgOffset += 4;
292 break;
293 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000294 // If we have one free GPR left, we can place the upper half of the i64
295 // in it, and store the other half to the stack. If we have two or more
296 // free GPRs, then we can pass both halves of the i64 in registers.
297 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000298 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
299 Args[i].first, DAG.getConstant(1, MVT::i32));
300 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
301 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000302 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000303 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000304 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000305 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000306 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000307 } else {
308 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
309 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000310 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
311 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000312 }
Nate Begeman307e7442005-03-26 01:28:53 +0000313 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000314 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
315 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000316 }
317 ArgOffset += 8;
318 break;
319 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000320 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000321 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000322 args_to_use.push_back(Args[i].first);
323 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000324 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000325 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
326 Args[i].first, PtrOff);
327 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000328 // Float varargs are always shadowed in available integer registers
329 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000330 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000331 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000332 args_to_use.push_back(Load);
333 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000334 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000335 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000336 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
337 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000338 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000339 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000340 args_to_use.push_back(Load);
341 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000342 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000343 } else {
344 // If we have any FPRs remaining, we may also have GPRs remaining.
345 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
346 // GPRs.
347 if (GPR_remaining > 0) {
348 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
349 --GPR_remaining;
350 }
351 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
352 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
353 --GPR_remaining;
354 }
Nate Begeman74d73452005-03-31 00:15:26 +0000355 }
Nate Begeman307e7442005-03-26 01:28:53 +0000356 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000357 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
358 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000359 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000360 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000361 break;
362 }
Nate Begemana9795f82005-03-24 04:41:43 +0000363 }
Nate Begeman74d73452005-03-31 00:15:26 +0000364 if (!MemOps.empty())
365 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000366 }
367
368 std::vector<MVT::ValueType> RetVals;
369 MVT::ValueType RetTyVT = getValueType(RetTy);
370 if (RetTyVT != MVT::isVoid)
371 RetVals.push_back(RetTyVT);
372 RetVals.push_back(MVT::Other);
373
374 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
375 Chain, Callee, args_to_use), 0);
376 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
377 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
378 DAG.getConstant(NumBytes, getPointerTy()));
379 return std::make_pair(TheCall, Chain);
380}
381
382std::pair<SDOperand, SDOperand>
383PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
384 //vastart just returns the address of the VarArgsFrameIndex slot.
385 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
386}
387
388std::pair<SDOperand,SDOperand> PPC32TargetLowering::
389LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
390 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000391 MVT::ValueType ArgVT = getValueType(ArgTy);
392 SDOperand Result;
393 if (!isVANext) {
394 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
395 } else {
396 unsigned Amt;
397 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
398 Amt = 4;
399 else {
400 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
401 "Other types should have been promoted for varargs!");
402 Amt = 8;
403 }
404 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
405 DAG.getConstant(Amt, VAList.getValueType()));
406 }
407 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000408}
409
410
411std::pair<SDOperand, SDOperand> PPC32TargetLowering::
412LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
413 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000414 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000415 abort();
416}
417
418namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000419Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begemana9795f82005-03-24 04:41:43 +0000420//===--------------------------------------------------------------------===//
421/// ISel - PPC32 specific code to select PPC32 machine instructions for
422/// SelectionDAG operations.
423//===--------------------------------------------------------------------===//
424class ISel : public SelectionDAGISel {
425
426 /// Comment Here.
427 PPC32TargetLowering PPC32Lowering;
428
429 /// ExprMap - As shared expressions are codegen'd, we keep track of which
430 /// vreg the value is produced in, so we only emit one copy of each compiled
431 /// tree.
432 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000433
434 unsigned GlobalBaseReg;
435 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000436
437public:
438 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
439 {}
440
Nate Begemanc7b09f12005-03-25 08:34:25 +0000441 /// runOnFunction - Override this function in order to reset our per-function
442 /// variables.
443 virtual bool runOnFunction(Function &Fn) {
444 // Make sure we re-emit a set of the global base reg if necessary
445 GlobalBaseInitialized = false;
446 return SelectionDAGISel::runOnFunction(Fn);
447 }
448
Nate Begemana9795f82005-03-24 04:41:43 +0000449 /// InstructionSelectBasicBlock - This callback is invoked by
450 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
451 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
452 DEBUG(BB->dump());
453 // Codegen the basic block.
454 Select(DAG.getRoot());
455
456 // Clear state used for selection.
457 ExprMap.clear();
458 }
459
Nate Begemandffcfcc2005-04-01 00:32:34 +0000460 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000461 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000462 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000463 unsigned SelectExpr(SDOperand N);
464 unsigned SelectExprFP(SDOperand N, unsigned Result);
465 void Select(SDOperand N);
466
Nate Begeman04730362005-04-01 04:45:11 +0000467 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000468 void SelectBranchCC(SDOperand N);
469};
470
471/// canUseAsImmediateForOpcode - This method returns a value indicating whether
472/// the ConstantSDNode N can be used as an immediate to Opcode. The return
473/// values are either 0, 1 or 2. 0 indicates that either N is not a
474/// ConstantSDNode, or is not suitable for use by that opcode. A return value
475/// of 1 indicates that the constant may be used in normal immediate form. A
476/// return value of 2 indicates that the constant may be used in shifted
477/// immediate form. If the return value is nonzero, the constant value is
478/// placed in Imm.
479///
480static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000481 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000482 if (N.getOpcode() != ISD::Constant) return 0;
483
484 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
485
486 switch(Opcode) {
487 default: return 0;
488 case ISD::ADD:
489 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
490 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
491 break;
492 case ISD::AND:
493 case ISD::XOR:
494 case ISD::OR:
495 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
496 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
497 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000498 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000499 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000500 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
501 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000502 case ISD::SETCC:
503 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
504 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
505 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000506 }
507 return 0;
508}
Nate Begeman3e897162005-03-31 23:55:40 +0000509
510/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
511/// to Condition. If the Condition is unordered or unsigned, the bool argument
512/// U is set to true, otherwise it is set to false.
513static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
514 U = false;
515 switch (Condition) {
516 default: assert(0 && "Unknown condition!"); abort();
517 case ISD::SETEQ: return PPC::BEQ;
518 case ISD::SETNE: return PPC::BNE;
519 case ISD::SETULT: U = true;
520 case ISD::SETLT: return PPC::BLT;
521 case ISD::SETULE: U = true;
522 case ISD::SETLE: return PPC::BLE;
523 case ISD::SETUGT: U = true;
524 case ISD::SETGT: return PPC::BGT;
525 case ISD::SETUGE: U = true;
526 case ISD::SETGE: return PPC::BGE;
527 }
Nate Begeman04730362005-04-01 04:45:11 +0000528 return 0;
529}
530
531/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
532/// and store immediate instructions.
533static unsigned IndexedOpForOp(unsigned Opcode) {
534 switch(Opcode) {
535 default: assert(0 && "Unknown opcode!"); abort();
536 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
537 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
538 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
539 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
540 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
541 case PPC::LFD: return PPC::LFDX;
542 }
543 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000544}
Nate Begemana9795f82005-03-24 04:41:43 +0000545}
546
Nate Begemanc7b09f12005-03-25 08:34:25 +0000547/// getGlobalBaseReg - Output the instructions required to put the
548/// base address to use for accessing globals into a register.
549///
550unsigned ISel::getGlobalBaseReg() {
551 if (!GlobalBaseInitialized) {
552 // Insert the set of GlobalBaseReg into the first MBB of the function
553 MachineBasicBlock &FirstMBB = BB->getParent()->front();
554 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
555 GlobalBaseReg = MakeReg(MVT::i32);
556 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
557 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
558 GlobalBaseInitialized = true;
559 }
560 return GlobalBaseReg;
561}
562
Nate Begeman6b559972005-04-01 02:59:27 +0000563/// getConstDouble - Loads a floating point value into a register, via the
564/// Constant Pool. Optionally takes a register in which to load the value.
565unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
566 unsigned Tmp1 = MakeReg(MVT::i32);
567 if (0 == Result) Result = MakeReg(MVT::f64);
568 MachineConstantPool *CP = BB->getParent()->getConstantPool();
569 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
570 unsigned CPI = CP->getConstantPoolIndex(CFP);
571 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
572 .addConstantPoolIndex(CPI);
573 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
574 return Result;
575}
576
Nate Begemandffcfcc2005-04-01 00:32:34 +0000577unsigned ISel::SelectSetCR0(SDOperand CC) {
578 unsigned Opc, Tmp1, Tmp2;
579 static const unsigned CompareOpcodes[] =
580 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
581
582 // If the first operand to the select is a SETCC node, then we can fold it
583 // into the branch that selects which value to return.
584 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
585 if (SetCC && CC.getOpcode() == ISD::SETCC) {
586 bool U;
587 Opc = getBCCForSetCC(SetCC->getCondition(), U);
588 Tmp1 = SelectExpr(SetCC->getOperand(0));
589
590 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
591 // so that it knows whether the SETCC immediate range is signed or not.
592 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
593 Tmp2, U)) {
594 if (U)
595 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
596 else
597 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
598 } else {
599 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
600 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
601 Tmp2 = SelectExpr(SetCC->getOperand(1));
602 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
603 }
604 } else {
605 Tmp1 = SelectExpr(CC);
606 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
607 Opc = PPC::BNE;
608 }
609 return Opc;
610}
611
612/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000613bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000614{
Nate Begeman96fc6812005-03-31 02:05:53 +0000615 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000616 if (N.getOpcode() == ISD::ADD) {
617 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000618 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000619 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000620 return false;
621 }
622 offset = SelectExpr(N.getOperand(1));
623 return true;
624 }
Nate Begemana9795f82005-03-24 04:41:43 +0000625 Reg = SelectExpr(N);
626 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000627 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000628}
629
630void ISel::SelectBranchCC(SDOperand N)
631{
632 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
633 MachineBasicBlock *Dest =
634 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000635
Nate Begemana9795f82005-03-24 04:41:43 +0000636 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000637 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000638 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000639 return;
640}
641
642unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
643{
644 unsigned Tmp1, Tmp2, Tmp3;
645 unsigned Opc = 0;
646 SDNode *Node = N.Val;
647 MVT::ValueType DestType = N.getValueType();
648 unsigned opcode = N.getOpcode();
649
650 switch (opcode) {
651 default:
652 Node->dump();
653 assert(0 && "Node not handled!\n");
654
Nate Begeman23afcfb2005-03-29 22:48:55 +0000655 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000656 // Attempt to generate FSEL. We can do this whenever we have an FP result,
657 // and an FP comparison in the SetCC node.
658 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
659 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
660 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
661 SetCC->getCondition() != ISD::SETEQ &&
662 SetCC->getCondition() != ISD::SETNE) {
663 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
664 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
665 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
666 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
667
668 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
669 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
670 switch(SetCC->getCondition()) {
671 default: assert(0 && "Invalid FSEL condition"); abort();
672 case ISD::SETULT:
673 case ISD::SETLT:
674 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
675 return Result;
676 case ISD::SETUGE:
677 case ISD::SETGE:
678 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
679 return Result;
680 case ISD::SETUGT:
681 case ISD::SETGT: {
682 Tmp2 = MakeReg(VT);
683 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
684 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
685 return Result;
686 }
687 case ISD::SETULE:
688 case ISD::SETLE: {
689 Tmp2 = MakeReg(VT);
690 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
691 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
692 return Result;
693 }
694 }
695 } else {
696 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
697 Tmp2 = SelectExpr(SetCC->getOperand(1));
698 Tmp3 = MakeReg(VT);
699 switch(SetCC->getCondition()) {
700 default: assert(0 && "Invalid FSEL condition"); abort();
701 case ISD::SETULT:
702 case ISD::SETLT:
703 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
704 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
705 return Result;
706 case ISD::SETUGE:
707 case ISD::SETGE:
708 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
709 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
710 return Result;
711 case ISD::SETUGT:
712 case ISD::SETGT:
713 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
714 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
715 return Result;
716 case ISD::SETULE:
717 case ISD::SETLE:
718 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
719 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
720 return Result;
721 }
722 }
723 assert(0 && "Should never get here");
724 return 0;
725 }
726
Nate Begeman31318e42005-04-01 07:21:30 +0000727 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
728 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000729 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000730
Nate Begeman23afcfb2005-03-29 22:48:55 +0000731 // Create an iterator with which to insert the MBB for copying the false
732 // value and the MBB to hold the PHI instruction for this SetCC.
733 MachineBasicBlock *thisMBB = BB;
734 const BasicBlock *LLVM_BB = BB->getBasicBlock();
735 ilist<MachineBasicBlock>::iterator It = BB;
736 ++It;
737
738 // thisMBB:
739 // ...
740 // TrueVal = ...
741 // cmpTY cr0, r1, r2
742 // bCC copy1MBB
743 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000744 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
745 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000746 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000747 MachineFunction *F = BB->getParent();
748 F->getBasicBlockList().insert(It, copy0MBB);
749 F->getBasicBlockList().insert(It, sinkMBB);
750 // Update machine-CFG edges
751 BB->addSuccessor(copy0MBB);
752 BB->addSuccessor(sinkMBB);
753
754 // copy0MBB:
755 // %FalseValue = ...
756 // # fallthrough to sinkMBB
757 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000758 // Update machine-CFG edges
759 BB->addSuccessor(sinkMBB);
760
761 // sinkMBB:
762 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
763 // ...
764 BB = sinkMBB;
765 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
766 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
767 return Result;
768 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000769
770 case ISD::FNEG:
771 if (ISD::FABS == N.getOperand(0).getOpcode()) {
772 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
773 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
774 } else {
775 Tmp1 = SelectExpr(N.getOperand(0));
776 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
777 }
778 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000779
Nate Begeman27eeb002005-04-02 05:59:34 +0000780 case ISD::FABS:
781 Tmp1 = SelectExpr(N.getOperand(0));
782 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
783 return Result;
784
Nate Begemana9795f82005-03-24 04:41:43 +0000785 case ISD::FP_ROUND:
786 assert (DestType == MVT::f32 &&
787 N.getOperand(0).getValueType() == MVT::f64 &&
788 "only f64 to f32 conversion supported here");
789 Tmp1 = SelectExpr(N.getOperand(0));
790 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
791 return Result;
792
793 case ISD::FP_EXTEND:
794 assert (DestType == MVT::f64 &&
795 N.getOperand(0).getValueType() == MVT::f32 &&
796 "only f32 to f64 conversion supported here");
797 Tmp1 = SelectExpr(N.getOperand(0));
798 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
799 return Result;
800
801 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000802 if (Result == 1)
803 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
804 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
805 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
806 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000807
Nate Begeman6d369cc2005-04-01 01:08:07 +0000808 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000809 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000810 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000811 return Result;
812 }
Nate Begemana9795f82005-03-24 04:41:43 +0000813
814 case ISD::MUL:
815 case ISD::ADD:
816 case ISD::SUB:
817 case ISD::SDIV:
818 switch( opcode ) {
819 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
820 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
821 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
822 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
823 };
Nate Begemana9795f82005-03-24 04:41:43 +0000824 Tmp1 = SelectExpr(N.getOperand(0));
825 Tmp2 = SelectExpr(N.getOperand(1));
826 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
827 return Result;
828
Nate Begemana9795f82005-03-24 04:41:43 +0000829 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000830 case ISD::SINT_TO_FP: {
831 assert (N.getOperand(0).getValueType() == MVT::i32
832 && "int to float must operate on i32");
833 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
834 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
835 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
836 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
837 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
838
839 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
840 MachineConstantPool *CP = BB->getParent()->getConstantPool();
841
842 // FIXME: pull this FP constant generation stuff out into something like
843 // the simple ISel's getReg.
844 if (IsUnsigned) {
845 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
846 unsigned CPI = CP->getConstantPoolIndex(CFP);
847 // Load constant fp value
848 unsigned Tmp4 = MakeReg(MVT::i32);
849 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
850 .addConstantPoolIndex(CPI);
851 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
852 // Store the hi & low halves of the fp value, currently in int regs
853 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
854 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
855 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
856 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
857 // Generate the return value with a subtract
858 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
859 } else {
860 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
861 unsigned CPI = CP->getConstantPoolIndex(CFP);
862 // Load constant fp value
863 unsigned Tmp4 = MakeReg(MVT::i32);
864 unsigned TmpL = MakeReg(MVT::i32);
865 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
866 .addConstantPoolIndex(CPI);
867 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
868 // Store the hi & low halves of the fp value, currently in int regs
869 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
870 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
871 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
872 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
873 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
874 // Generate the return value with a subtract
875 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
876 }
877 return Result;
878 }
Nate Begemana9795f82005-03-24 04:41:43 +0000879 }
Nate Begeman6b559972005-04-01 02:59:27 +0000880 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000881 return 0;
882}
883
884unsigned ISel::SelectExpr(SDOperand N) {
885 unsigned Result;
886 unsigned Tmp1, Tmp2, Tmp3;
887 unsigned Opc = 0;
888 unsigned opcode = N.getOpcode();
889
890 SDNode *Node = N.Val;
891 MVT::ValueType DestType = N.getValueType();
892
893 unsigned &Reg = ExprMap[N];
894 if (Reg) return Reg;
895
Nate Begeman27eeb002005-04-02 05:59:34 +0000896 switch (N.getOpcode()) {
897 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000898 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000899 MakeReg(N.getValueType()) : 1;
900 break;
901 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000902 // If this is a call instruction, make sure to prepare ALL of the result
903 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000904 if (Node->getNumValues() == 1)
905 Reg = Result = 1; // Void call, just a chain.
906 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000907 Result = MakeReg(Node->getValueType(0));
908 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000909 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000910 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000911 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000912 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000913 break;
914 case ISD::ADD_PARTS:
915 case ISD::SUB_PARTS:
916 case ISD::SHL_PARTS:
917 case ISD::SRL_PARTS:
918 case ISD::SRA_PARTS:
919 Result = MakeReg(Node->getValueType(0));
920 ExprMap[N.getValue(0)] = Result;
921 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
922 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
923 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000924 }
925
926 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000927 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000928 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000929
930 switch (opcode) {
931 default:
932 Node->dump();
933 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000934 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000935 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
936 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000937 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000938 // Generate both result values. FIXME: Need a better commment here?
939 if (Result != 1)
940 ExprMap[N.getValue(1)] = 1;
941 else
942 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
943
944 // FIXME: We are currently ignoring the requested alignment for handling
945 // greater than the stack alignment. This will need to be revisited at some
946 // point. Align = N.getOperand(2);
947 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
948 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
949 std::cerr << "Cannot allocate stack object with greater alignment than"
950 << " the stack alignment yet!";
951 abort();
952 }
953 Select(N.getOperand(0));
954 Tmp1 = SelectExpr(N.getOperand(1));
955 // Subtract size from stack pointer, thereby allocating some space.
956 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
957 // Put a pointer to the space into the result register by copying the SP
958 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
959 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000960
961 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000962 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
963 Tmp2 = MakeReg(MVT::i32);
964 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
965 .addConstantPoolIndex(Tmp1);
966 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
967 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000968
969 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000970 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000971 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000972 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000973
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000974 case ISD::GlobalAddress: {
975 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000976 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000977 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
978 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000979 if (GV->hasWeakLinkage() || GV->isExternal()) {
980 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
981 } else {
982 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
983 }
984 return Result;
985 }
986
Nate Begeman5e966612005-03-24 06:28:42 +0000987 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000988 case ISD::EXTLOAD:
989 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000990 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000991 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
992 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000993 bool sext = (ISD::SEXTLOAD == opcode);
994 bool byte = (MVT::i8 == TypeBeingLoaded);
995
Nate Begeman5e966612005-03-24 06:28:42 +0000996 // Make sure we generate both values.
997 if (Result != 1)
998 ExprMap[N.getValue(1)] = 1; // Generate the token
999 else
1000 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1001
1002 SDOperand Chain = N.getOperand(0);
1003 SDOperand Address = N.getOperand(1);
1004 Select(Chain);
1005
Nate Begeman9db505c2005-03-28 19:36:43 +00001006 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001007 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001008 case MVT::i1: Opc = PPC::LBZ; break;
1009 case MVT::i8: Opc = PPC::LBZ; break;
1010 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1011 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001012 case MVT::f32: Opc = PPC::LFS; break;
1013 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001014 }
1015
Nate Begeman74d73452005-03-31 00:15:26 +00001016 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1017 Tmp1 = MakeReg(MVT::i32);
1018 int CPI = CP->getIndex();
1019 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1020 .addConstantPoolIndex(CPI);
1021 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001022 }
Nate Begeman74d73452005-03-31 00:15:26 +00001023 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001024 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1025 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001026 } else {
1027 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001028 bool idx = SelectAddr(Address, Tmp1, offset);
1029 if (idx) {
1030 Opc = IndexedOpForOp(Opc);
1031 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1032 } else {
1033 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1034 }
Nate Begeman5e966612005-03-24 06:28:42 +00001035 }
1036 return Result;
1037 }
1038
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001039 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001040 unsigned GPR_idx = 0, FPR_idx = 0;
1041 static const unsigned GPR[] = {
1042 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1043 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1044 };
1045 static const unsigned FPR[] = {
1046 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1047 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1048 };
1049
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001050 // Lower the chain for this call.
1051 Select(N.getOperand(0));
1052 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001053
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001054 // Load the register args to virtual regs
1055 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001056 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001057 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1058
1059 // Copy the virtual registers into the appropriate argument register
1060 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1061 switch(N.getOperand(i+2).getValueType()) {
1062 default: Node->dump(); assert(0 && "Unknown value type for call");
1063 case MVT::i1:
1064 case MVT::i8:
1065 case MVT::i16:
1066 case MVT::i32:
1067 assert(GPR_idx < 8 && "Too many int args");
1068 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1069 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1070 ++GPR_idx;
1071 break;
1072 case MVT::f64:
1073 case MVT::f32:
1074 assert(FPR_idx < 13 && "Too many fp args");
1075 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1076 ++FPR_idx;
1077 break;
1078 }
1079 }
1080
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001081 // Emit the correct call instruction based on the type of symbol called.
1082 if (GlobalAddressSDNode *GASD =
1083 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1084 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1085 } else if (ExternalSymbolSDNode *ESSDN =
1086 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1087 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1088 } else {
1089 Tmp1 = SelectExpr(N.getOperand(1));
1090 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1091 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1092 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1093 }
1094
1095 switch (Node->getValueType(0)) {
1096 default: assert(0 && "Unknown value type for call result!");
1097 case MVT::Other: return 1;
1098 case MVT::i1:
1099 case MVT::i8:
1100 case MVT::i16:
1101 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001102 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001103 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001104 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001105 break;
1106 case MVT::f32:
1107 case MVT::f64:
1108 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1109 break;
1110 }
1111 return Result+N.ResNo;
1112 }
Nate Begemana9795f82005-03-24 04:41:43 +00001113
1114 case ISD::SIGN_EXTEND:
1115 case ISD::SIGN_EXTEND_INREG:
1116 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001117 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1118 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1119 case MVT::i16:
1120 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1121 break;
1122 case MVT::i8:
1123 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1124 break;
Nate Begeman74747862005-03-29 22:24:51 +00001125 case MVT::i1:
1126 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1127 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001128 }
Nate Begemana9795f82005-03-24 04:41:43 +00001129 return Result;
1130
1131 case ISD::ZERO_EXTEND_INREG:
1132 Tmp1 = SelectExpr(N.getOperand(0));
1133 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001134 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001135 case MVT::i16: Tmp2 = 16; break;
1136 case MVT::i8: Tmp2 = 24; break;
1137 case MVT::i1: Tmp2 = 31; break;
1138 }
Nate Begeman33162522005-03-29 21:54:38 +00001139 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1140 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001141 return Result;
1142
Nate Begemana9795f82005-03-24 04:41:43 +00001143 case ISD::CopyFromReg:
1144 if (Result == 1)
1145 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1146 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1147 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1148 return Result;
1149
1150 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001151 Tmp1 = SelectExpr(N.getOperand(0));
1152 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1153 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001154 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001155 .addImm(31-Tmp2);
1156 } else {
1157 Tmp2 = SelectExpr(N.getOperand(1));
1158 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1159 }
1160 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001161
Nate Begeman5e966612005-03-24 06:28:42 +00001162 case ISD::SRL:
1163 Tmp1 = SelectExpr(N.getOperand(0));
1164 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1165 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001166 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001167 .addImm(Tmp2).addImm(31);
1168 } else {
1169 Tmp2 = SelectExpr(N.getOperand(1));
1170 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1171 }
1172 return Result;
1173
1174 case ISD::SRA:
1175 Tmp1 = SelectExpr(N.getOperand(0));
1176 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1177 Tmp2 = CN->getValue() & 0x1F;
1178 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1179 } else {
1180 Tmp2 = SelectExpr(N.getOperand(1));
1181 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1182 }
1183 return Result;
1184
Nate Begemana9795f82005-03-24 04:41:43 +00001185 case ISD::ADD:
1186 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1187 Tmp1 = SelectExpr(N.getOperand(0));
1188 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1189 default: assert(0 && "unhandled result code");
1190 case 0: // No immediate
1191 Tmp2 = SelectExpr(N.getOperand(1));
1192 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1193 break;
1194 case 1: // Low immediate
1195 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1196 break;
1197 case 2: // Shifted immediate
1198 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1199 break;
1200 }
1201 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001202
Nate Begemana9795f82005-03-24 04:41:43 +00001203 case ISD::AND:
1204 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001205 Tmp1 = SelectExpr(N.getOperand(0));
1206 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1207 default: assert(0 && "unhandled result code");
1208 case 0: // No immediate
1209 Tmp2 = SelectExpr(N.getOperand(1));
1210 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001211 case ISD::AND: Opc = PPC::AND; break;
1212 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001213 }
Nate Begeman5e966612005-03-24 06:28:42 +00001214 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001215 break;
1216 case 1: // Low immediate
1217 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001218 case ISD::AND: Opc = PPC::ANDIo; break;
1219 case ISD::OR: Opc = PPC::ORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001220 }
Nate Begeman5e966612005-03-24 06:28:42 +00001221 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001222 break;
1223 case 2: // Shifted immediate
1224 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001225 case ISD::AND: Opc = PPC::ANDISo; break;
1226 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001227 }
Nate Begeman5e966612005-03-24 06:28:42 +00001228 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001229 break;
1230 }
1231 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001232
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001233 case ISD::XOR: {
1234 // Check for EQV: xor, (xor a, -1), b
1235 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1236 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1237 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1238 ++NotLogic;
1239 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1240 Tmp2 = SelectExpr(N.getOperand(1));
1241 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1242 return Result;
1243 }
1244 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1245 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1246 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1247 ++NotLogic;
1248 switch(N.getOperand(0).getOpcode()) {
1249 case ISD::OR:
1250 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1251 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1252 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1253 break;
1254 case ISD::AND:
1255 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1256 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1257 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1258 break;
1259 default:
1260 Tmp1 = SelectExpr(N.getOperand(0));
1261 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1262 break;
1263 }
1264 return Result;
1265 }
1266 Tmp1 = SelectExpr(N.getOperand(0));
1267 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1268 default: assert(0 && "unhandled result code");
1269 case 0: // No immediate
1270 Tmp2 = SelectExpr(N.getOperand(1));
1271 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1272 break;
1273 case 1: // Low immediate
1274 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1275 break;
1276 case 2: // Shifted immediate
1277 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1278 break;
1279 }
1280 return Result;
1281 }
1282
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001283 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001284 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001285 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1286 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1287 else {
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1290 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001291 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001292
Nate Begeman5e966612005-03-24 06:28:42 +00001293 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001294 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001295 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1296 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1297 else {
1298 Tmp2 = SelectExpr(N.getOperand(1));
1299 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1300 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001301 return Result;
1302
Nate Begemanf3d08f32005-03-29 00:03:27 +00001303 case ISD::SDIV:
1304 case ISD::UDIV:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001305 Tmp1 = SelectExpr(N.getOperand(0));
1306 Tmp2 = SelectExpr(N.getOperand(1));
1307 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1308 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1309 return Result;
1310
1311 case ISD::UREM:
1312 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001313 Tmp1 = SelectExpr(N.getOperand(0));
1314 Tmp2 = SelectExpr(N.getOperand(1));
1315 Tmp3 = MakeReg(MVT::i32);
1316 unsigned Tmp4 = MakeReg(MVT::i32);
1317 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1318 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1319 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1320 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1321 return Result;
1322 }
1323
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001324 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001325 case ISD::SUB_PARTS: {
1326 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1327 "Not an i64 add/sub!");
1328 // Emit all of the operands.
1329 std::vector<unsigned> InVals;
1330 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1331 InVals.push_back(SelectExpr(N.getOperand(i)));
1332 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001333 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1334 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001335 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001336 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1337 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1338 }
1339 return Result+N.ResNo;
1340 }
1341
1342 case ISD::SHL_PARTS:
1343 case ISD::SRA_PARTS:
1344 case ISD::SRL_PARTS: {
1345 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1346 "Not an i64 shift!");
1347 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1348 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1349 unsigned SHReg = SelectExpr(N.getOperand(2));
1350 Tmp1 = MakeReg(MVT::i32);
1351 Tmp2 = MakeReg(MVT::i32);
1352 Tmp3 = MakeReg(MVT::i32);
1353 unsigned Tmp4 = MakeReg(MVT::i32);
1354 unsigned Tmp5 = MakeReg(MVT::i32);
1355 unsigned Tmp6 = MakeReg(MVT::i32);
1356 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1357 if (ISD::SHL_PARTS == opcode) {
1358 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1359 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1360 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1361 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001362 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001363 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1364 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1365 } else if (ISD::SRL_PARTS == opcode) {
1366 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1367 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1368 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1369 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1370 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1371 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1372 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1373 } else {
1374 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1375 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1376 MachineBasicBlock *OldMBB = BB;
1377 MachineFunction *F = BB->getParent();
1378 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1379 F->getBasicBlockList().insert(It, TmpMBB);
1380 F->getBasicBlockList().insert(It, PhiMBB);
1381 BB->addSuccessor(TmpMBB);
1382 BB->addSuccessor(PhiMBB);
1383 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1384 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1385 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1386 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1387 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1388 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1389 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1390 // Select correct least significant half if the shift amount > 32
1391 BB = TmpMBB;
1392 unsigned Tmp7 = MakeReg(MVT::i32);
1393 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1394 TmpMBB->addSuccessor(PhiMBB);
1395 BB = PhiMBB;
1396 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1397 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001398 }
1399 return Result+N.ResNo;
1400 }
1401
Nate Begemana9795f82005-03-24 04:41:43 +00001402 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001403 case ISD::FP_TO_SINT: {
1404 bool U = (ISD::FP_TO_UINT == opcode);
1405 Tmp1 = SelectExpr(N.getOperand(0));
1406 if (!U) {
1407 Tmp2 = MakeReg(MVT::f64);
1408 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1409 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1410 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1411 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1412 return Result;
1413 } else {
1414 unsigned Zero = getConstDouble(0.0);
1415 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1416 unsigned Border = getConstDouble(1LL << 31);
1417 unsigned UseZero = MakeReg(MVT::f64);
1418 unsigned UseMaxInt = MakeReg(MVT::f64);
1419 unsigned UseChoice = MakeReg(MVT::f64);
1420 unsigned TmpReg = MakeReg(MVT::f64);
1421 unsigned TmpReg2 = MakeReg(MVT::f64);
1422 unsigned ConvReg = MakeReg(MVT::f64);
1423 unsigned IntTmp = MakeReg(MVT::i32);
1424 unsigned XorReg = MakeReg(MVT::i32);
1425 MachineFunction *F = BB->getParent();
1426 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1427 // Update machine-CFG edges
1428 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1429 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1430 MachineBasicBlock *OldMBB = BB;
1431 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1432 F->getBasicBlockList().insert(It, XorMBB);
1433 F->getBasicBlockList().insert(It, PhiMBB);
1434 BB->addSuccessor(XorMBB);
1435 BB->addSuccessor(PhiMBB);
1436 // Convert from floating point to unsigned 32-bit value
1437 // Use 0 if incoming value is < 0.0
1438 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1439 // Use 2**32 - 1 if incoming value is >= 2**32
1440 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1441 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1442 .addReg(MaxInt);
1443 // Subtract 2**31
1444 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1445 // Use difference if >= 2**31
1446 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1447 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1448 .addReg(UseChoice);
1449 // Convert to integer
1450 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1451 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1452 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1453 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1454 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1455
1456 // XorMBB:
1457 // add 2**31 if input was >= 2**31
1458 BB = XorMBB;
1459 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1460 XorMBB->addSuccessor(PhiMBB);
1461
1462 // PhiMBB:
1463 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1464 BB = PhiMBB;
1465 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1466 .addReg(XorReg).addMBB(XorMBB);
1467 return Result;
1468 }
1469 assert(0 && "Should never get here");
1470 return 0;
1471 }
Nate Begemana9795f82005-03-24 04:41:43 +00001472
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001473 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001474 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001475 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001476
Nate Begeman31318e42005-04-01 07:21:30 +00001477 unsigned TrueValue = MakeReg(MVT::i32);
1478 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1479 unsigned FalseValue = MakeReg(MVT::i32);
1480 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1481
Nate Begeman33162522005-03-29 21:54:38 +00001482 // Create an iterator with which to insert the MBB for copying the false
1483 // value and the MBB to hold the PHI instruction for this SetCC.
1484 MachineBasicBlock *thisMBB = BB;
1485 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1486 ilist<MachineBasicBlock>::iterator It = BB;
1487 ++It;
1488
1489 // thisMBB:
1490 // ...
1491 // cmpTY cr0, r1, r2
1492 // %TrueValue = li 1
1493 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001494 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1495 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1496 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1497 MachineFunction *F = BB->getParent();
1498 F->getBasicBlockList().insert(It, copy0MBB);
1499 F->getBasicBlockList().insert(It, sinkMBB);
1500 // Update machine-CFG edges
1501 BB->addSuccessor(copy0MBB);
1502 BB->addSuccessor(sinkMBB);
1503
1504 // copy0MBB:
1505 // %FalseValue = li 0
1506 // fallthrough
1507 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001508 // Update machine-CFG edges
1509 BB->addSuccessor(sinkMBB);
1510
1511 // sinkMBB:
1512 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1513 // ...
1514 BB = sinkMBB;
1515 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1516 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1517 return Result;
1518 }
1519 assert(0 && "Is this legal?");
1520 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001521
Nate Begeman74747862005-03-29 22:24:51 +00001522 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001523 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1524 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001525 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001526
Nate Begeman74747862005-03-29 22:24:51 +00001527 // Create an iterator with which to insert the MBB for copying the false
1528 // value and the MBB to hold the PHI instruction for this SetCC.
1529 MachineBasicBlock *thisMBB = BB;
1530 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1531 ilist<MachineBasicBlock>::iterator It = BB;
1532 ++It;
1533
1534 // thisMBB:
1535 // ...
1536 // TrueVal = ...
1537 // cmpTY cr0, r1, r2
1538 // bCC copy1MBB
1539 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001540 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1541 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001542 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001543 MachineFunction *F = BB->getParent();
1544 F->getBasicBlockList().insert(It, copy0MBB);
1545 F->getBasicBlockList().insert(It, sinkMBB);
1546 // Update machine-CFG edges
1547 BB->addSuccessor(copy0MBB);
1548 BB->addSuccessor(sinkMBB);
1549
1550 // copy0MBB:
1551 // %FalseValue = ...
1552 // # fallthrough to sinkMBB
1553 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001554 // Update machine-CFG edges
1555 BB->addSuccessor(sinkMBB);
1556
1557 // sinkMBB:
1558 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1559 // ...
1560 BB = sinkMBB;
1561 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1562 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1563
1564 // FIXME: Select i64?
1565 return Result;
1566 }
Nate Begemana9795f82005-03-24 04:41:43 +00001567
1568 case ISD::Constant:
1569 switch (N.getValueType()) {
1570 default: assert(0 && "Cannot use constants of this type!");
1571 case MVT::i1:
1572 BuildMI(BB, PPC::LI, 1, Result)
1573 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1574 break;
1575 case MVT::i32:
1576 {
1577 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1578 if (v < 32768 && v >= -32768) {
1579 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1580 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001581 Tmp1 = MakeReg(MVT::i32);
1582 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1583 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001584 }
1585 }
1586 }
1587 return Result;
1588 }
1589
1590 return 0;
1591}
1592
1593void ISel::Select(SDOperand N) {
1594 unsigned Tmp1, Tmp2, Opc;
1595 unsigned opcode = N.getOpcode();
1596
1597 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1598 return; // Already selected.
1599
1600 SDNode *Node = N.Val;
1601
1602 switch (Node->getOpcode()) {
1603 default:
1604 Node->dump(); std::cerr << "\n";
1605 assert(0 && "Node not handled yet!");
1606 case ISD::EntryToken: return; // Noop
1607 case ISD::TokenFactor:
1608 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1609 Select(Node->getOperand(i));
1610 return;
1611 case ISD::ADJCALLSTACKDOWN:
1612 case ISD::ADJCALLSTACKUP:
1613 Select(N.getOperand(0));
1614 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1615 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1616 PPC::ADJCALLSTACKUP;
1617 BuildMI(BB, Opc, 1).addImm(Tmp1);
1618 return;
1619 case ISD::BR: {
1620 MachineBasicBlock *Dest =
1621 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001622 Select(N.getOperand(0));
1623 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1624 return;
1625 }
1626 case ISD::BRCOND:
1627 SelectBranchCC(N);
1628 return;
1629 case ISD::CopyToReg:
1630 Select(N.getOperand(0));
1631 Tmp1 = SelectExpr(N.getOperand(1));
1632 Tmp2 = cast<RegSDNode>(N)->getReg();
1633
1634 if (Tmp1 != Tmp2) {
1635 if (N.getOperand(1).getValueType() == MVT::f64 ||
1636 N.getOperand(1).getValueType() == MVT::f32)
1637 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1638 else
1639 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1640 }
1641 return;
1642 case ISD::ImplicitDef:
1643 Select(N.getOperand(0));
1644 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1645 return;
1646 case ISD::RET:
1647 switch (N.getNumOperands()) {
1648 default:
1649 assert(0 && "Unknown return instruction!");
1650 case 3:
1651 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1652 N.getOperand(2).getValueType() == MVT::i32 &&
1653 "Unknown two-register value!");
1654 Select(N.getOperand(0));
1655 Tmp1 = SelectExpr(N.getOperand(1));
1656 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001657 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1658 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001659 break;
1660 case 2:
1661 Select(N.getOperand(0));
1662 Tmp1 = SelectExpr(N.getOperand(1));
1663 switch (N.getOperand(1).getValueType()) {
1664 default:
1665 assert(0 && "Unknown return type!");
1666 case MVT::f64:
1667 case MVT::f32:
1668 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1669 break;
1670 case MVT::i32:
1671 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1672 break;
1673 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001674 case 1:
1675 Select(N.getOperand(0));
1676 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001677 }
1678 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1679 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001680 case ISD::TRUNCSTORE:
1681 case ISD::STORE:
1682 {
1683 SDOperand Chain = N.getOperand(0);
1684 SDOperand Value = N.getOperand(1);
1685 SDOperand Address = N.getOperand(2);
1686 Select(Chain);
1687
1688 Tmp1 = SelectExpr(Value); //value
1689
1690 if (opcode == ISD::STORE) {
1691 switch(Value.getValueType()) {
1692 default: assert(0 && "unknown Type in store");
1693 case MVT::i32: Opc = PPC::STW; break;
1694 case MVT::f64: Opc = PPC::STFD; break;
1695 case MVT::f32: Opc = PPC::STFS; break;
1696 }
1697 } else { //ISD::TRUNCSTORE
1698 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1699 default: assert(0 && "unknown Type in store");
1700 case MVT::i1: //FIXME: DAG does not promote this load
1701 case MVT::i8: Opc = PPC::STB; break;
1702 case MVT::i16: Opc = PPC::STH; break;
1703 }
1704 }
1705
Nate Begemana7e11a42005-04-01 05:57:17 +00001706 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001707 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001708 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1709 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001710 }
1711 else
1712 {
1713 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001714 bool idx = SelectAddr(Address, Tmp2, offset);
1715 if (idx) {
1716 Opc = IndexedOpForOp(Opc);
1717 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1718 } else {
1719 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1720 }
Nate Begemana9795f82005-03-24 04:41:43 +00001721 }
1722 return;
1723 }
1724 case ISD::EXTLOAD:
1725 case ISD::SEXTLOAD:
1726 case ISD::ZEXTLOAD:
1727 case ISD::LOAD:
1728 case ISD::CopyFromReg:
1729 case ISD::CALL:
1730 case ISD::DYNAMIC_STACKALLOC:
1731 ExprMap.erase(N);
1732 SelectExpr(N);
1733 return;
1734 }
1735 assert(0 && "Should not be reached!");
1736}
1737
1738
1739/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1740/// into a machine code representation using pattern matching and a machine
1741/// description file.
1742///
1743FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1744 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001745}
1746