blob: a516a8b3c8c8a56117746dd97f077a6bdaa48491 [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemana9795f82005-03-24 04:41:43 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
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);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000050
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);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000059
Nate Begeman815d6da2005-04-06 00:25:27 +000060 // 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
Misha Brukmanb5f662f2005-04-21 23:30:14 +000066 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000067
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);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000075
Nate Begemana9795f82005-03-24 04:41:43 +000076 /// 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);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000081
Nate Begemana9795f82005-03-24 04:41:43 +000082 virtual std::pair<SDOperand, SDOperand>
83 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000084
Nate Begemana9795f82005-03-24 04:41:43 +000085 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;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000105
106 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000107 // 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;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000114 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000115 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());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132
Nate Begemana9795f82005-03-24 04:41:43 +0000133 switch (ObjectVT) {
134 default: assert(0 && "Unhandled argument type!");
135 case MVT::i1:
136 case MVT::i8:
137 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000138 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000139 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 {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000173 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000174 }
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]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000182 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000183 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000184 --FPR_remaining;
185 ++FPR_idx;
186 } else {
187 needsLoad = true;
188 }
189 break;
190 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000191
Nate Begemana9795f82005-03-24 04:41:43 +0000192 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000193 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000194 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);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000200 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000201 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000202 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
203 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000204
Nate Begemana9795f82005-03-24 04:41:43 +0000205 // 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));
Misha Brukmanb5f662f2005-04-21 23:30:14 +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());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000231 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Nate Begeman6644d4c2005-04-03 23:11:17 +0000232 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,
Misha Brukman7847fca2005-04-22 17:54:37 +0000266 const Type *RetTy, bool isVarArg,
267 SDOperand Callee, ArgListTy &Args,
268 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000269 // args_to_use will accumulate outgoing args for the ISD::CALL case in
270 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000271 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000272
273 // Count how many bytes are to be pushed on the stack, including the linkage
274 // area, and parameter passing area.
275 unsigned NumBytes = 24;
276
277 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000278 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
279 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000280 } else {
281 for (unsigned i = 0, e = Args.size(); i != e; ++i)
282 switch (getValueType(Args[i].second)) {
283 default: assert(0 && "Unknown value type!");
284 case MVT::i1:
285 case MVT::i8:
286 case MVT::i16:
287 case MVT::i32:
288 case MVT::f32:
289 NumBytes += 4;
290 break;
291 case MVT::i64:
292 case MVT::f64:
293 NumBytes += 8;
294 break;
295 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000296
297 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000298 // plus 32 bytes of argument space in case any called code gets funky on us.
299 if (NumBytes < 56) NumBytes = 56;
300
301 // Adjust the stack pointer for the new arguments...
302 // These operations are automatically eliminated by the prolog/epilog pass
303 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
304 DAG.getConstant(NumBytes, getPointerTy()));
305
306 // Set up a copy of the stack pointer for use loading and storing any
307 // arguments that may not fit in the registers available for argument
308 // passing.
309 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
310 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000311
Nate Begeman307e7442005-03-26 01:28:53 +0000312 // Figure out which arguments are going to go in registers, and which in
313 // memory. Also, if this is a vararg function, floating point operations
314 // must be stored to our stack, and loaded into integer regs as well, if
315 // any integer regs are available for argument passing.
316 unsigned ArgOffset = 24;
317 unsigned GPR_remaining = 8;
318 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000319
Nate Begeman74d73452005-03-31 00:15:26 +0000320 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000321 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
322 // PtrOff will be used to store the current argument to the stack if a
323 // register cannot be found for it.
324 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
325 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000326 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000327
Nate Begemanf7e43382005-03-26 07:46:36 +0000328 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000329 default: assert(0 && "Unexpected ValueType for argument!");
330 case MVT::i1:
331 case MVT::i8:
332 case MVT::i16:
333 // Promote the integer to 32 bits. If the input type is signed use a
334 // sign extend, otherwise use a zero extend.
335 if (Args[i].second->isSigned())
336 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
337 else
338 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
339 // FALL THROUGH
340 case MVT::i32:
341 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000342 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000343 --GPR_remaining;
344 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000345 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
346 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000347 }
348 ArgOffset += 4;
349 break;
350 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000351 // If we have one free GPR left, we can place the upper half of the i64
352 // in it, and store the other half to the stack. If we have two or more
353 // free GPRs, then we can pass both halves of the i64 in registers.
354 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000355 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000356 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000357 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000358 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000359 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000360 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000361 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000362 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000363 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000364 } else {
365 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
366 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000367 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
368 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000369 }
Nate Begeman307e7442005-03-26 01:28:53 +0000370 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000371 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
372 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000373 }
374 ArgOffset += 8;
375 break;
376 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000377 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000378 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000379 args_to_use.push_back(Args[i].first);
380 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000381 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000382 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
383 Args[i].first, PtrOff);
384 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 // Float varargs are always shadowed in available integer registers
386 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000387 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000388 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000389 args_to_use.push_back(Load);
390 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000391 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000392 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
394 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000395 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000396 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000397 args_to_use.push_back(Load);
398 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000399 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000400 } else {
401 // If we have any FPRs remaining, we may also have GPRs remaining.
402 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
403 // GPRs.
404 if (GPR_remaining > 0) {
405 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
406 --GPR_remaining;
407 }
408 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
409 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
410 --GPR_remaining;
411 }
Nate Begeman74d73452005-03-31 00:15:26 +0000412 }
Nate Begeman307e7442005-03-26 01:28:53 +0000413 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000414 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
415 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000416 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000417 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000418 break;
419 }
Nate Begemana9795f82005-03-24 04:41:43 +0000420 }
Nate Begeman74d73452005-03-31 00:15:26 +0000421 if (!MemOps.empty())
422 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000423 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000424
Nate Begemana9795f82005-03-24 04:41:43 +0000425 std::vector<MVT::ValueType> RetVals;
426 MVT::ValueType RetTyVT = getValueType(RetTy);
427 if (RetTyVT != MVT::isVoid)
428 RetVals.push_back(RetTyVT);
429 RetVals.push_back(MVT::Other);
430
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000431 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000432 Chain, Callee, args_to_use), 0);
433 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
434 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
435 DAG.getConstant(NumBytes, getPointerTy()));
436 return std::make_pair(TheCall, Chain);
437}
438
439std::pair<SDOperand, SDOperand>
440PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
441 //vastart just returns the address of the VarArgsFrameIndex slot.
442 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
443}
444
445std::pair<SDOperand,SDOperand> PPC32TargetLowering::
446LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
447 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000448 MVT::ValueType ArgVT = getValueType(ArgTy);
449 SDOperand Result;
450 if (!isVANext) {
451 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
452 } else {
453 unsigned Amt;
454 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
455 Amt = 4;
456 else {
457 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
458 "Other types should have been promoted for varargs!");
459 Amt = 8;
460 }
461 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
462 DAG.getConstant(Amt, VAList.getValueType()));
463 }
464 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000465}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000466
Nate Begemana9795f82005-03-24 04:41:43 +0000467
468std::pair<SDOperand, SDOperand> PPC32TargetLowering::
469LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
470 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000471 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000472 abort();
473}
474
475namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000476Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000477Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000478Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000479//===--------------------------------------------------------------------===//
480/// ISel - PPC32 specific code to select PPC32 machine instructions for
481/// SelectionDAG operations.
482//===--------------------------------------------------------------------===//
483class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000484 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000485 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
486 // for sdiv and udiv until it is put into the future
487 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000488
Nate Begemana9795f82005-03-24 04:41:43 +0000489 /// ExprMap - As shared expressions are codegen'd, we keep track of which
490 /// vreg the value is produced in, so we only emit one copy of each compiled
491 /// tree.
492 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000493
494 unsigned GlobalBaseReg;
495 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000496 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000497public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000498 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
499 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000500
Nate Begemanc7b09f12005-03-25 08:34:25 +0000501 /// runOnFunction - Override this function in order to reset our per-function
502 /// variables.
503 virtual bool runOnFunction(Function &Fn) {
504 // Make sure we re-emit a set of the global base reg if necessary
505 GlobalBaseInitialized = false;
506 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000507 }
508
Nate Begemana9795f82005-03-24 04:41:43 +0000509 /// InstructionSelectBasicBlock - This callback is invoked by
510 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
511 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
512 DEBUG(BB->dump());
513 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000514 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000515 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000516
Nate Begemana9795f82005-03-24 04:41:43 +0000517 // Clear state used for selection.
518 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000519 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000520 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000521
522 // dag -> dag expanders for integer divide by constant
523 SDOperand BuildSDIVSequence(SDOperand N);
524 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000525
Nate Begemandffcfcc2005-04-01 00:32:34 +0000526 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000527 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000528 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000529 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000530 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000531 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
532 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000533 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000534 unsigned SelectExprFP(SDOperand N, unsigned Result);
535 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000536
Nate Begeman04730362005-04-01 04:45:11 +0000537 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000538 void SelectBranchCC(SDOperand N);
539};
540
Nate Begeman80196b12005-04-05 00:15:08 +0000541/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
542/// returns zero when the input is not exactly a power of two.
543static unsigned ExactLog2(unsigned Val) {
544 if (Val == 0 || (Val & (Val-1))) return 0;
545 unsigned Count = 0;
546 while (Val != 1) {
547 Val >>= 1;
548 ++Count;
549 }
550 return Count;
551}
552
Nate Begeman7ddecb42005-04-06 23:51:40 +0000553// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
554// any number of 0's on either side. the 1's are allowed to wrap from LSB to
555// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
556// not, since all 1's are not contiguous.
557static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
558 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000559 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000560 ME = 0;
561
562 // look for first set bit
563 int i = 0;
564 for (; i < 32; i++) {
565 if ((Val & (1 << (31 - i))) != 0) {
566 MB = i;
567 ME = i;
568 break;
569 }
570 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000571
Nate Begeman7ddecb42005-04-06 23:51:40 +0000572 // look for last set bit
573 for (; i < 32; i++) {
574 if ((Val & (1 << (31 - i))) == 0)
575 break;
576 ME = i;
577 }
578
579 // look for next set bit
580 for (; i < 32; i++) {
581 if ((Val & (1 << (31 - i))) != 0)
582 break;
583 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000584
Nate Begeman7ddecb42005-04-06 23:51:40 +0000585 // if we exhausted all the bits, we found a match at this point for 0*1*0*
586 if (i == 32)
587 return true;
588
589 // since we just encountered more 1's, if it doesn't wrap around to the
590 // most significant bit of the word, then we did not find a match to 1*0*1* so
591 // exit.
592 if (MB != 0)
593 return false;
594
595 // look for last set bit
596 for (MB = i; i < 32; i++) {
597 if ((Val & (1 << (31 - i))) == 0)
598 break;
599 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000600
Nate Begeman7ddecb42005-04-06 23:51:40 +0000601 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
602 // the value is not a run of ones.
603 if (i == 32)
604 return true;
605 return false;
606}
607
Nate Begeman439b4442005-04-05 04:22:58 +0000608/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000609/// the ConstantSDNode N can be used as an immediate to Opcode. The return
610/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000611/// ConstantSDNode, or is not suitable for use by that opcode.
612/// Return value codes for turning into an enum someday:
613/// 1: constant may be used in normal immediate form.
614/// 2: constant may be used in shifted immediate form.
615/// 3: log base 2 of the constant may be used.
616/// 4: constant is suitable for integer division conversion
617/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000618///
Nate Begeman439b4442005-04-05 04:22:58 +0000619static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
620 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000621 if (N.getOpcode() != ISD::Constant) return 0;
622
623 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000624
Nate Begemana9795f82005-03-24 04:41:43 +0000625 switch(Opcode) {
626 default: return 0;
627 case ISD::ADD:
628 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
629 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
630 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000631 case ISD::AND: {
632 unsigned MB, ME;
633 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
634 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
635 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
636 break;
637 }
Nate Begemana9795f82005-03-24 04:41:43 +0000638 case ISD::XOR:
639 case ISD::OR:
640 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
641 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
642 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000643 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000644 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000645 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
646 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000647 case ISD::SETCC:
648 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
649 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
650 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000651 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000652 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000653 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000654 if (v <= -2 || v >= 2) { return 4; }
655 break;
656 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000657 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000658 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000659 }
660 return 0;
661}
Nate Begeman3e897162005-03-31 23:55:40 +0000662
Nate Begemanc7bd4822005-04-11 06:34:10 +0000663/// NodeHasRecordingVariant - If SelectExpr can always produce code for
664/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
665/// return false.
666static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
667 switch(NodeOpcode) {
668 default: return false;
669 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000670 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000671 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000672 }
673}
674
Nate Begeman3e897162005-03-31 23:55:40 +0000675/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
676/// to Condition. If the Condition is unordered or unsigned, the bool argument
677/// U is set to true, otherwise it is set to false.
678static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
679 U = false;
680 switch (Condition) {
681 default: assert(0 && "Unknown condition!"); abort();
682 case ISD::SETEQ: return PPC::BEQ;
683 case ISD::SETNE: return PPC::BNE;
684 case ISD::SETULT: U = true;
685 case ISD::SETLT: return PPC::BLT;
686 case ISD::SETULE: U = true;
687 case ISD::SETLE: return PPC::BLE;
688 case ISD::SETUGT: U = true;
689 case ISD::SETGT: return PPC::BGT;
690 case ISD::SETUGE: U = true;
691 case ISD::SETGE: return PPC::BGE;
692 }
Nate Begeman04730362005-04-01 04:45:11 +0000693 return 0;
694}
695
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000696/// getCROpForOp - Return the condition register opcode (or inverted opcode)
697/// associated with the SelectionDAG opcode.
698static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
699 switch (Opcode) {
700 default: assert(0 && "Unknown opcode!"); abort();
701 case ISD::AND:
702 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
703 if (!Inv1 && !Inv2) return PPC::CRAND;
704 if (Inv1 ^ Inv2) return PPC::CRANDC;
705 case ISD::OR:
706 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
707 if (!Inv1 && !Inv2) return PPC::CROR;
708 if (Inv1 ^ Inv2) return PPC::CRORC;
709 }
710 return 0;
711}
712
713/// getCRIdxForSetCC - Return the index of the condition register field
714/// associated with the SetCC condition, and whether or not the field is
715/// treated as inverted. That is, lt = 0; ge = 0 inverted.
716static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
717 switch (Condition) {
718 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000719 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000720 case ISD::SETLT: Inv = false; return 0;
721 case ISD::SETUGE:
722 case ISD::SETGE: Inv = true; return 0;
723 case ISD::SETUGT:
724 case ISD::SETGT: Inv = false; return 1;
725 case ISD::SETULE:
726 case ISD::SETLE: Inv = true; return 1;
727 case ISD::SETEQ: Inv = false; return 2;
728 case ISD::SETNE: Inv = true; return 2;
729 }
730 return 0;
731}
732
Nate Begeman04730362005-04-01 04:45:11 +0000733/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
734/// and store immediate instructions.
735static unsigned IndexedOpForOp(unsigned Opcode) {
736 switch(Opcode) {
737 default: assert(0 && "Unknown opcode!"); abort();
738 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
739 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
740 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
741 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
742 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
743 case PPC::LFD: return PPC::LFDX;
744 }
745 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000746}
Nate Begeman815d6da2005-04-06 00:25:27 +0000747
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000748// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000749// a multiply.
750struct ms {
751 int m; // magic number
752 int s; // shift amount
753};
754
755struct mu {
756 unsigned int m; // magic number
757 int a; // add indicator
758 int s; // shift amount
759};
760
761/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000762/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000763/// or -1.
764static struct ms magic(int d) {
765 int p;
766 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
767 const unsigned int two31 = 2147483648U; // 2^31
768 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000769
Nate Begeman815d6da2005-04-06 00:25:27 +0000770 ad = abs(d);
771 t = two31 + ((unsigned int)d >> 31);
772 anc = t - 1 - t%ad; // absolute value of nc
773 p = 31; // initialize p
774 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
775 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
776 q2 = two31/ad; // initialize q2 = 2p/abs(d)
777 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
778 do {
779 p = p + 1;
780 q1 = 2*q1; // update q1 = 2p/abs(nc)
781 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
782 if (r1 >= anc) { // must be unsigned comparison
783 q1 = q1 + 1;
784 r1 = r1 - anc;
785 }
786 q2 = 2*q2; // update q2 = 2p/abs(d)
787 r2 = 2*r2; // update r2 = rem(2p/abs(d))
788 if (r2 >= ad) { // must be unsigned comparison
789 q2 = q2 + 1;
790 r2 = r2 - ad;
791 }
792 delta = ad - r2;
793 } while (q1 < delta || (q1 == delta && r1 == 0));
794
795 mag.m = q2 + 1;
796 if (d < 0) mag.m = -mag.m; // resulting magic number
797 mag.s = p - 32; // resulting shift
798 return mag;
799}
800
801/// magicu - calculate the magic numbers required to codegen an integer udiv as
802/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
803static struct mu magicu(unsigned d)
804{
805 int p;
806 unsigned int nc, delta, q1, r1, q2, r2;
807 struct mu magu;
808 magu.a = 0; // initialize "add" indicator
809 nc = - 1 - (-d)%d;
810 p = 31; // initialize p
811 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
812 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
813 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
814 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
815 do {
816 p = p + 1;
817 if (r1 >= nc - r1 ) {
818 q1 = 2*q1 + 1; // update q1
819 r1 = 2*r1 - nc; // update r1
820 }
821 else {
822 q1 = 2*q1; // update q1
823 r1 = 2*r1; // update r1
824 }
825 if (r2 + 1 >= d - r2) {
826 if (q2 >= 0x7FFFFFFF) magu.a = 1;
827 q2 = 2*q2 + 1; // update q2
828 r2 = 2*r2 + 1 - d; // update r2
829 }
830 else {
831 if (q2 >= 0x80000000) magu.a = 1;
832 q2 = 2*q2; // update q2
833 r2 = 2*r2 + 1; // update r2
834 }
835 delta = d - 1 - r2;
836 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
837 magu.m = q2 + 1; // resulting magic number
838 magu.s = p - 32; // resulting shift
839 return magu;
840}
841}
842
843/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
844/// return a DAG expression to select that will generate the same value by
845/// multiplying by a magic number. See:
846/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
847SDOperand ISel::BuildSDIVSequence(SDOperand N) {
848 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
849 ms magics = magic(d);
850 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000851 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000852 ISelDAG->getConstant(magics.m, MVT::i32));
853 // If d > 0 and m < 0, add the numerator
854 if (d > 0 && magics.m < 0)
855 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
856 // If d < 0 and m > 0, subtract the numerator.
857 if (d < 0 && magics.m > 0)
858 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
859 // Shift right algebraic if shift value is nonzero
860 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000861 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000862 ISelDAG->getConstant(magics.s, MVT::i32));
863 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000864 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000865 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000866 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000867}
868
869/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
870/// return a DAG expression to select that will generate the same value by
871/// multiplying by a magic number. See:
872/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
873SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000874 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000875 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
876 mu magics = magicu(d);
877 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000878 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000879 ISelDAG->getConstant(magics.m, MVT::i32));
880 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000881 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000882 ISelDAG->getConstant(magics.s, MVT::i32));
883 } else {
884 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000885 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000886 ISelDAG->getConstant(1, MVT::i32));
887 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000888 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000889 ISelDAG->getConstant(magics.s-1, MVT::i32));
890 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000891 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000892}
893
Nate Begemanc7b09f12005-03-25 08:34:25 +0000894/// getGlobalBaseReg - Output the instructions required to put the
895/// base address to use for accessing globals into a register.
896///
897unsigned ISel::getGlobalBaseReg() {
898 if (!GlobalBaseInitialized) {
899 // Insert the set of GlobalBaseReg into the first MBB of the function
900 MachineBasicBlock &FirstMBB = BB->getParent()->front();
901 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
902 GlobalBaseReg = MakeReg(MVT::i32);
903 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
904 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
905 GlobalBaseInitialized = true;
906 }
907 return GlobalBaseReg;
908}
909
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000910/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000911/// Constant Pool. Optionally takes a register in which to load the value.
912unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
913 unsigned Tmp1 = MakeReg(MVT::i32);
914 if (0 == Result) Result = MakeReg(MVT::f64);
915 MachineConstantPool *CP = BB->getParent()->getConstantPool();
916 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
917 unsigned CPI = CP->getConstantPoolIndex(CFP);
918 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
919 .addConstantPoolIndex(CPI);
920 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
921 return Result;
922}
923
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000924/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000925/// Inv is true, then invert the result.
926void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
927 unsigned IntCR = MakeReg(MVT::i32);
928 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
929 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
930 if (Inv) {
931 unsigned Tmp1 = MakeReg(MVT::i32);
932 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
933 .addImm(31).addImm(31);
934 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
935 } else {
936 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
937 .addImm(31).addImm(31);
938 }
939}
940
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000941/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000942/// the rotate left word immediate then mask insert (rlwimi) instruction.
943/// Returns true on success, false if the caller still needs to select OR.
944///
945/// Patterns matched:
946/// 1. or shl, and 5. or and, and
947/// 2. or and, shl 6. or shl, shr
948/// 3. or shr, and 7. or shr, shl
949/// 4. or and, shr
950bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000951 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000952 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
953 unsigned Op0Opc = OR.getOperand(0).getOpcode();
954 unsigned Op1Opc = OR.getOperand(1).getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000955
Nate Begeman7ddecb42005-04-06 23:51:40 +0000956 // Verify that we have the correct opcodes
957 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
958 return false;
959 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
960 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000961
Nate Begeman7ddecb42005-04-06 23:51:40 +0000962 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000963 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000964 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
965 switch(Op0Opc) {
966 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
967 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
968 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
969 }
970 } else {
971 return false;
972 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000973
Nate Begeman7ddecb42005-04-06 23:51:40 +0000974 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000975 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000976 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
977 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000978 case ISD::SHL:
979 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000980 InsMask <<= Amount;
981 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000982 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000983 case ISD::SRL:
984 Amount = CN->getValue();
985 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000986 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000987 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000988 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000989 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +0000990 InsMask &= (unsigned)CN->getValue();
991 break;
992 }
993 } else {
994 return false;
995 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000996
Nate Begeman7ddecb42005-04-06 23:51:40 +0000997 // Verify that the Target mask and Insert mask together form a full word mask
998 // and that the Insert mask is a run of set bits (which implies both are runs
999 // of set bits). Given that, Select the arguments and generate the rlwimi
1000 // instruction.
1001 unsigned MB, ME;
1002 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
1003 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001004 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1005 // where both bitfield halves are sourced from the same value.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001006 if (IsRotate &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001007 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001008 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1009 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1010 .addImm(0).addImm(31);
1011 return true;
1012 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001013 if (Op0Opc == ISD::AND)
1014 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1015 else
1016 Tmp1 = SelectExpr(OR.getOperand(0));
1017 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
1018 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1019 .addImm(Amount).addImm(MB).addImm(ME);
1020 return true;
1021 }
1022 return false;
1023}
1024
Nate Begeman3664cef2005-04-13 22:14:14 +00001025/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1026/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1027/// wider than the implicit mask, then we can get rid of the AND and let the
1028/// shift do the mask.
1029unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1030 unsigned C;
1031 if (N.getOpcode() == ISD::AND &&
1032 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1033 31 == (C & 0xFFFF) && // ME
1034 26 >= (C >> 16)) // MB
1035 return SelectExpr(N.getOperand(0));
1036 else
1037 return SelectExpr(N);
1038}
1039
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001040unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001041 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001042 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001043 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001044 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001045
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001046 // Allocate a condition register for this expression
1047 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001048
Nate Begemandffcfcc2005-04-01 00:32:34 +00001049 // If the first operand to the select is a SETCC node, then we can fold it
1050 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001051 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001052 bool U;
1053 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001054 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001055
Nate Begeman439b4442005-04-05 04:22:58 +00001056 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001057 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001058 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001059 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001060 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001061 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1062 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001063 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001064 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1065 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001066 RecordSuccess = false;
1067 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1068 if (RecordSuccess) {
1069 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001070 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1071 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001072 }
1073 AlreadySelected = true;
1074 }
1075 // If we could not implicitly set CR0, then emit a compare immediate
1076 // instead.
1077 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001078 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001079 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001080 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001081 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001082 } else {
1083 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1084 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001085 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001086 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001087 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001088 }
1089 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001090 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001091 return SelectCCExpr(CC, Opc, Inv, Idx);
1092 // If this isn't a SetCC, then select the value and compare it against zero,
1093 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001094 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001095 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001096 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001097 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001098 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001099 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001100}
1101
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001102unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001103 unsigned &Idx) {
1104 bool Inv0, Inv1;
1105 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1106
1107 // Allocate a condition register for this expression
1108 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1109
1110 // Check for the operations we support:
1111 switch(N.getOpcode()) {
1112 default:
1113 Opc = PPC::BNE;
1114 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1115 Tmp1 = SelectExpr(N);
1116 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1117 break;
1118 case ISD::OR:
1119 case ISD::AND:
1120 ++MultiBranch;
1121 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1122 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1123 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1124 if (Inv0 && !Inv1) {
1125 std::swap(Tmp1, Tmp2);
1126 std::swap(Idx0, Idx1);
1127 Opc = Opc1;
1128 }
1129 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1130 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1131 .addReg(Tmp2).addImm(Idx1);
1132 Inv = false;
1133 Idx = Idx0;
1134 break;
1135 case ISD::SETCC:
1136 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1137 Result = Tmp1;
1138 break;
1139 }
1140 return Result;
1141}
1142
Nate Begemandffcfcc2005-04-01 00:32:34 +00001143/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001144bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001145{
Nate Begeman96fc6812005-03-31 02:05:53 +00001146 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001147 if (N.getOpcode() == ISD::ADD) {
1148 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001149 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001150 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001151 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001152 }
Nate Begeman04730362005-04-01 04:45:11 +00001153 offset = SelectExpr(N.getOperand(1));
1154 return true;
1155 }
Nate Begemana9795f82005-03-24 04:41:43 +00001156 Reg = SelectExpr(N);
1157 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001158 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001159}
1160
1161void ISel::SelectBranchCC(SDOperand N)
1162{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001163 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001164 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001165
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001166 bool Inv;
1167 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001168 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001169 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001170
Nate Begemancd08e4c2005-04-09 20:09:12 +00001171 // Iterate to the next basic block, unless we're already at the end of the
1172 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001173 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001174
1175 // If this is a two way branch, then grab the fallthrough basic block argument
1176 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1177 // if necessary by the branch selection pass. Otherwise, emit a standard
1178 // conditional branch.
1179 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001180 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001181 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1182 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001183 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001184 .addMBB(Dest).addMBB(Fallthrough);
1185 if (Fallthrough != It)
1186 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1187 } else {
1188 if (Fallthrough != It) {
1189 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001190 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001191 .addMBB(Fallthrough).addMBB(Dest);
1192 }
1193 }
1194 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001195 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001196 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001197 }
Nate Begemana9795f82005-03-24 04:41:43 +00001198 return;
1199}
1200
1201unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1202{
1203 unsigned Tmp1, Tmp2, Tmp3;
1204 unsigned Opc = 0;
1205 SDNode *Node = N.Val;
1206 MVT::ValueType DestType = N.getValueType();
1207 unsigned opcode = N.getOpcode();
1208
1209 switch (opcode) {
1210 default:
1211 Node->dump();
1212 assert(0 && "Node not handled!\n");
1213
Nate Begeman23afcfb2005-03-29 22:48:55 +00001214 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001215 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1216 // and an FP comparison in the SetCC node.
1217 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1218 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1219 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1220 SetCC->getCondition() != ISD::SETEQ &&
1221 SetCC->getCondition() != ISD::SETNE) {
1222 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001223 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1224 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001225
Nate Begeman3e897162005-03-31 23:55:40 +00001226 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1227 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1228 switch(SetCC->getCondition()) {
1229 default: assert(0 && "Invalid FSEL condition"); abort();
1230 case ISD::SETULT:
1231 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001232 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001233 case ISD::SETUGE:
1234 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001235 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001236 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1237 return Result;
1238 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001239 case ISD::SETGT:
1240 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001241 case ISD::SETULE:
1242 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001243 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1244 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1245 } else {
1246 Tmp2 = MakeReg(VT);
1247 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1248 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1249 }
Nate Begeman3e897162005-03-31 23:55:40 +00001250 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1251 return Result;
1252 }
1253 }
1254 } else {
1255 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001256 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001257 Tmp2 = SelectExpr(SetCC->getOperand(1));
1258 Tmp3 = MakeReg(VT);
1259 switch(SetCC->getCondition()) {
1260 default: assert(0 && "Invalid FSEL condition"); abort();
1261 case ISD::SETULT:
1262 case ISD::SETLT:
1263 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1264 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1265 return Result;
1266 case ISD::SETUGE:
1267 case ISD::SETGE:
1268 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1269 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1270 return Result;
1271 case ISD::SETUGT:
1272 case ISD::SETGT:
1273 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1274 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1275 return Result;
1276 case ISD::SETULE:
1277 case ISD::SETLE:
1278 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1279 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1280 return Result;
1281 }
1282 }
1283 assert(0 && "Should never get here");
1284 return 0;
1285 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001286
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001287 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001288 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1289 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001290 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001291
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001292 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001293 // value and the MBB to hold the PHI instruction for this SetCC.
1294 MachineBasicBlock *thisMBB = BB;
1295 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1296 ilist<MachineBasicBlock>::iterator It = BB;
1297 ++It;
1298
1299 // thisMBB:
1300 // ...
1301 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001302 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001303 // bCC copy1MBB
1304 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001305 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1306 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001307 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001308 MachineFunction *F = BB->getParent();
1309 F->getBasicBlockList().insert(It, copy0MBB);
1310 F->getBasicBlockList().insert(It, sinkMBB);
1311 // Update machine-CFG edges
1312 BB->addSuccessor(copy0MBB);
1313 BB->addSuccessor(sinkMBB);
1314
1315 // copy0MBB:
1316 // %FalseValue = ...
1317 // # fallthrough to sinkMBB
1318 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001319 // Update machine-CFG edges
1320 BB->addSuccessor(sinkMBB);
1321
1322 // sinkMBB:
1323 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1324 // ...
1325 BB = sinkMBB;
1326 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1327 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1328 return Result;
1329 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001330
1331 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001332 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001333 ISD::ADD == N.getOperand(0).getOpcode() &&
1334 N.getOperand(0).Val->hasOneUse() &&
1335 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1336 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001337 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001338 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1339 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1340 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1341 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1342 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001343 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001344 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001345 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001346 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1347 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001348 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001349 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1350 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1351 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1352 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001353 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1354 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001355 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1356 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1357 } else {
1358 Tmp1 = SelectExpr(N.getOperand(0));
1359 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1360 }
1361 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001362
Nate Begeman27eeb002005-04-02 05:59:34 +00001363 case ISD::FABS:
1364 Tmp1 = SelectExpr(N.getOperand(0));
1365 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1366 return Result;
1367
Nate Begemana9795f82005-03-24 04:41:43 +00001368 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001369 assert (DestType == MVT::f32 &&
1370 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001371 "only f64 to f32 conversion supported here");
1372 Tmp1 = SelectExpr(N.getOperand(0));
1373 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1374 return Result;
1375
1376 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001377 assert (DestType == MVT::f64 &&
1378 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001379 "only f32 to f64 conversion supported here");
1380 Tmp1 = SelectExpr(N.getOperand(0));
1381 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1382 return Result;
1383
1384 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001385 if (Result == 1)
1386 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1387 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1388 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1389 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001390
Nate Begeman6d369cc2005-04-01 01:08:07 +00001391 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001392 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001393 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001394 return Result;
1395 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001396
Nate Begemana9795f82005-03-24 04:41:43 +00001397 case ISD::ADD:
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::FMADD : PPC::FMADDS;
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::FMADD : PPC::FMADDS;
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::FADD : PPC::FADDS;
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
Nate Begemana9795f82005-03-24 04:41:43 +00001424 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001425 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1426 N.getOperand(0).Val->hasOneUse()) {
1427 ++FusedFP; // Statistic
1428 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1429 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1430 Tmp3 = SelectExpr(N.getOperand(1));
1431 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1432 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1433 return Result;
1434 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001435 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1436 N.getOperand(1).Val->hasOneUse()) {
1437 ++FusedFP; // Statistic
1438 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1439 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1440 Tmp3 = SelectExpr(N.getOperand(0));
1441 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1442 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1443 return Result;
1444 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001445 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1446 Tmp1 = SelectExpr(N.getOperand(0));
1447 Tmp2 = SelectExpr(N.getOperand(1));
1448 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1449 return Result;
1450
1451 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001452 case ISD::SDIV:
1453 switch( opcode ) {
1454 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001455 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1456 };
Nate Begemana9795f82005-03-24 04:41:43 +00001457 Tmp1 = SelectExpr(N.getOperand(0));
1458 Tmp2 = SelectExpr(N.getOperand(1));
1459 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1460 return Result;
1461
Nate Begemana9795f82005-03-24 04:41:43 +00001462 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001463 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001464 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001465 && "int to float must operate on i32");
1466 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1467 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1468 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1469 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001470
Nate Begemanfdcf3412005-03-30 19:38:35 +00001471 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1472 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001473
Nate Begemanfdcf3412005-03-30 19:38:35 +00001474 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001475 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001476 // Store the hi & low halves of the fp value, currently in int regs
1477 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1478 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1479 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1480 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1481 // Generate the return value with a subtract
1482 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1483 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001484 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001485 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001486 // Store the hi & low halves of the fp value, currently in int regs
1487 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1488 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1489 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1490 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1491 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1492 // Generate the return value with a subtract
1493 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1494 }
1495 return Result;
1496 }
Nate Begemana9795f82005-03-24 04:41:43 +00001497 }
Nate Begeman6b559972005-04-01 02:59:27 +00001498 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001499 return 0;
1500}
1501
Nate Begemanc7bd4822005-04-11 06:34:10 +00001502unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001503 unsigned Result;
1504 unsigned Tmp1, Tmp2, Tmp3;
1505 unsigned Opc = 0;
1506 unsigned opcode = N.getOpcode();
1507
1508 SDNode *Node = N.Val;
1509 MVT::ValueType DestType = N.getValueType();
1510
1511 unsigned &Reg = ExprMap[N];
1512 if (Reg) return Reg;
1513
Nate Begeman27eeb002005-04-02 05:59:34 +00001514 switch (N.getOpcode()) {
1515 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001516 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001517 MakeReg(N.getValueType()) : 1;
1518 break;
1519 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001520 // If this is a call instruction, make sure to prepare ALL of the result
1521 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001522 if (Node->getNumValues() == 1)
1523 Reg = Result = 1; // Void call, just a chain.
1524 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001525 Result = MakeReg(Node->getValueType(0));
1526 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001527 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001528 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001529 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001530 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001531 break;
1532 case ISD::ADD_PARTS:
1533 case ISD::SUB_PARTS:
1534 case ISD::SHL_PARTS:
1535 case ISD::SRL_PARTS:
1536 case ISD::SRA_PARTS:
1537 Result = MakeReg(Node->getValueType(0));
1538 ExprMap[N.getValue(0)] = Result;
1539 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1540 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1541 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001542 }
1543
Nate Begemane5846682005-04-04 06:52:38 +00001544 if (ISD::CopyFromReg == opcode)
1545 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001546
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001547 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001548 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Nate Begemana0e3e942005-04-10 01:14:13 +00001549 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001550 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001551
1552 switch (opcode) {
1553 default:
1554 Node->dump();
1555 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001556 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001557 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1558 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001559 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001560 // Generate both result values. FIXME: Need a better commment here?
1561 if (Result != 1)
1562 ExprMap[N.getValue(1)] = 1;
1563 else
1564 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1565
1566 // FIXME: We are currently ignoring the requested alignment for handling
1567 // greater than the stack alignment. This will need to be revisited at some
1568 // point. Align = N.getOperand(2);
1569 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1570 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1571 std::cerr << "Cannot allocate stack object with greater alignment than"
1572 << " the stack alignment yet!";
1573 abort();
1574 }
1575 Select(N.getOperand(0));
1576 Tmp1 = SelectExpr(N.getOperand(1));
1577 // Subtract size from stack pointer, thereby allocating some space.
1578 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1579 // Put a pointer to the space into the result register by copying the SP
1580 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1581 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001582
1583 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001584 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1585 Tmp2 = MakeReg(MVT::i32);
1586 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1587 .addConstantPoolIndex(Tmp1);
1588 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1589 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001590
1591 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001592 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001593 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001594 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001595
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001596 case ISD::GlobalAddress: {
1597 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001598 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001599 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1600 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001601 if (GV->hasWeakLinkage() || GV->isExternal()) {
1602 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1603 } else {
1604 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1605 }
1606 return Result;
1607 }
1608
Nate Begeman5e966612005-03-24 06:28:42 +00001609 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001610 case ISD::EXTLOAD:
1611 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001612 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001613 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1614 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001615 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001616
Nate Begeman5e966612005-03-24 06:28:42 +00001617 // Make sure we generate both values.
1618 if (Result != 1)
1619 ExprMap[N.getValue(1)] = 1; // Generate the token
1620 else
1621 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1622
1623 SDOperand Chain = N.getOperand(0);
1624 SDOperand Address = N.getOperand(1);
1625 Select(Chain);
1626
Nate Begeman9db505c2005-03-28 19:36:43 +00001627 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001628 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001629 case MVT::i1: Opc = PPC::LBZ; break;
1630 case MVT::i8: Opc = PPC::LBZ; break;
1631 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1632 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001633 case MVT::f32: Opc = PPC::LFS; break;
1634 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001635 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001636
Nate Begeman74d73452005-03-31 00:15:26 +00001637 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1638 Tmp1 = MakeReg(MVT::i32);
1639 int CPI = CP->getIndex();
1640 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1641 .addConstantPoolIndex(CPI);
1642 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001643 }
Nate Begeman74d73452005-03-31 00:15:26 +00001644 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001645 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1646 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001647 } else {
1648 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001649 bool idx = SelectAddr(Address, Tmp1, offset);
1650 if (idx) {
1651 Opc = IndexedOpForOp(Opc);
1652 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1653 } else {
1654 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1655 }
Nate Begeman5e966612005-03-24 06:28:42 +00001656 }
1657 return Result;
1658 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001659
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001660 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001661 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001662 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001663 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1664 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1665 };
1666 static const unsigned FPR[] = {
1667 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1668 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1669 };
1670
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001671 // Lower the chain for this call.
1672 Select(N.getOperand(0));
1673 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001674
Nate Begemand860aa62005-04-04 22:17:48 +00001675 MachineInstr *CallMI;
1676 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001677 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001678 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001679 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001680 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001681 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001682 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001683 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001684 true);
1685 } else {
1686 Tmp1 = SelectExpr(N.getOperand(1));
1687 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1688 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1689 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1690 .addReg(PPC::R12);
1691 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001692
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001693 // Load the register args to virtual regs
1694 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001695 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001696 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1697
1698 // Copy the virtual registers into the appropriate argument register
1699 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1700 switch(N.getOperand(i+2).getValueType()) {
1701 default: Node->dump(); assert(0 && "Unknown value type for call");
1702 case MVT::i1:
1703 case MVT::i8:
1704 case MVT::i16:
1705 case MVT::i32:
1706 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001707 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001708 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001709 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1710 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001711 ++GPR_idx;
1712 break;
1713 case MVT::f64:
1714 case MVT::f32:
1715 assert(FPR_idx < 13 && "Too many fp args");
1716 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001717 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001718 ++FPR_idx;
1719 break;
1720 }
1721 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001722
Nate Begemand860aa62005-04-04 22:17:48 +00001723 // Put the call instruction in the correct place in the MachineBasicBlock
1724 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001725
1726 switch (Node->getValueType(0)) {
1727 default: assert(0 && "Unknown value type for call result!");
1728 case MVT::Other: return 1;
1729 case MVT::i1:
1730 case MVT::i8:
1731 case MVT::i16:
1732 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001733 if (Node->getValueType(1) == MVT::i32) {
1734 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1735 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1736 } else {
1737 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1738 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001739 break;
1740 case MVT::f32:
1741 case MVT::f64:
1742 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1743 break;
1744 }
1745 return Result+N.ResNo;
1746 }
Nate Begemana9795f82005-03-24 04:41:43 +00001747
1748 case ISD::SIGN_EXTEND:
1749 case ISD::SIGN_EXTEND_INREG:
1750 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001751 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1752 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001753 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001754 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001755 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001756 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001757 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001758 break;
Nate Begeman74747862005-03-29 22:24:51 +00001759 case MVT::i1:
1760 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1761 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001762 }
Nate Begemana9795f82005-03-24 04:41:43 +00001763 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001764
Nate Begemana9795f82005-03-24 04:41:43 +00001765 case ISD::CopyFromReg:
1766 if (Result == 1)
1767 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1768 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1769 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1770 return Result;
1771
1772 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001773 Tmp1 = SelectExpr(N.getOperand(0));
1774 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1775 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001776 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001777 .addImm(31-Tmp2);
1778 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001779 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001780 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1781 }
1782 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001783
Nate Begeman5e966612005-03-24 06:28:42 +00001784 case ISD::SRL:
1785 Tmp1 = SelectExpr(N.getOperand(0));
1786 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1787 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001788 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001789 .addImm(Tmp2).addImm(31);
1790 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001791 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001792 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1793 }
1794 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001795
Nate Begeman5e966612005-03-24 06:28:42 +00001796 case ISD::SRA:
1797 Tmp1 = SelectExpr(N.getOperand(0));
1798 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1799 Tmp2 = CN->getValue() & 0x1F;
1800 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1801 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001802 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001803 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1804 }
1805 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001806
Nate Begemana9795f82005-03-24 04:41:43 +00001807 case ISD::ADD:
1808 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1809 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001810 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001811 default: assert(0 && "unhandled result code");
1812 case 0: // No immediate
1813 Tmp2 = SelectExpr(N.getOperand(1));
1814 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1815 break;
1816 case 1: // Low immediate
1817 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1818 break;
1819 case 2: // Shifted immediate
1820 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1821 break;
1822 }
1823 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001824
Nate Begemana9795f82005-03-24 04:41:43 +00001825 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001826 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001827 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001828 N.getOperand(1).getOpcode() == ISD::SETCC) {
1829 bool Inv;
1830 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1831 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1832 return Result;
1833 }
1834 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001835 Tmp1 = SelectExpr(N.getOperand(0));
1836 // FIXME: should add check in getImmediateForOpcode to return a value
1837 // indicating the immediate is a run of set bits so we can emit a bitfield
1838 // clear with RLWINM instead.
1839 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1840 default: assert(0 && "unhandled result code");
1841 case 0: // No immediate
1842 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001843 Opc = Recording ? PPC::ANDo : PPC::AND;
1844 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001845 break;
1846 case 1: // Low immediate
1847 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1848 break;
1849 case 2: // Shifted immediate
1850 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1851 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001852 case 5: // Bitfield mask
1853 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1854 Tmp3 = Tmp2 >> 16; // MB
1855 Tmp2 &= 0xFFFF; // ME
1856 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1857 .addImm(Tmp3).addImm(Tmp2);
1858 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001859 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001860 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001861 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001862
Nate Begemana9795f82005-03-24 04:41:43 +00001863 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001864 if (SelectBitfieldInsert(N, Result))
1865 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001866 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001867 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001868 N.getOperand(1).getOpcode() == ISD::SETCC) {
1869 bool Inv;
1870 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1871 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1872 return Result;
1873 }
1874 }
Nate Begemana9795f82005-03-24 04:41:43 +00001875 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001876 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001877 default: assert(0 && "unhandled result code");
1878 case 0: // No immediate
1879 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001880 Opc = Recording ? PPC::ORo : PPC::OR;
1881 RecordSuccess = true;
1882 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001883 break;
1884 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001885 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001886 break;
1887 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001888 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001889 break;
1890 }
1891 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001892
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001893 case ISD::XOR: {
1894 // Check for EQV: xor, (xor a, -1), b
1895 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1896 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1897 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001898 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1899 Tmp2 = SelectExpr(N.getOperand(1));
1900 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1901 return Result;
1902 }
Chris Lattner837a5212005-04-21 21:09:11 +00001903 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001904 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1905 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001906 switch(N.getOperand(0).getOpcode()) {
1907 case ISD::OR:
1908 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1909 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1910 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1911 break;
1912 case ISD::AND:
1913 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1914 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1915 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1916 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001917 case ISD::XOR:
1918 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1919 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1920 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1921 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001922 default:
1923 Tmp1 = SelectExpr(N.getOperand(0));
1924 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1925 break;
1926 }
1927 return Result;
1928 }
1929 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001930 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001931 default: assert(0 && "unhandled result code");
1932 case 0: // No immediate
1933 Tmp2 = SelectExpr(N.getOperand(1));
1934 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1935 break;
1936 case 1: // Low immediate
1937 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1938 break;
1939 case 2: // Shifted immediate
1940 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1941 break;
1942 }
1943 return Result;
1944 }
1945
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001946 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001947 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001948 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001949 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1950 else {
1951 Tmp1 = SelectExpr(N.getOperand(0));
1952 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1953 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001954 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001955
Nate Begeman5e966612005-03-24 06:28:42 +00001956 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001957 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001958 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001959 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1960 else {
1961 Tmp2 = SelectExpr(N.getOperand(1));
1962 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1963 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001964 return Result;
1965
Nate Begeman815d6da2005-04-06 00:25:27 +00001966 case ISD::MULHS:
1967 case ISD::MULHU:
1968 Tmp1 = SelectExpr(N.getOperand(0));
1969 Tmp2 = SelectExpr(N.getOperand(1));
1970 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1971 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1972 return Result;
1973
Nate Begemanf3d08f32005-03-29 00:03:27 +00001974 case ISD::SDIV:
1975 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001976 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1977 default: break;
1978 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1979 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001980 Tmp1 = MakeReg(MVT::i32);
1981 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001982 if ((int)Tmp3 < 0) {
1983 unsigned Tmp4 = MakeReg(MVT::i32);
1984 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1985 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1986 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1987 } else {
1988 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1989 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1990 }
Nate Begeman80196b12005-04-05 00:15:08 +00001991 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001992 // If this is a divide by constant, we can emit code using some magic
1993 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001994 case 4:
1995 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001996 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00001997 return SelectExpr(BuildSDIVSequence(N));
1998 else
1999 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002000 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002001 Tmp1 = SelectExpr(N.getOperand(0));
2002 Tmp2 = SelectExpr(N.getOperand(1));
2003 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2004 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2005 return Result;
2006
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002007 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002008 case ISD::SUB_PARTS: {
2009 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2010 "Not an i64 add/sub!");
2011 // Emit all of the operands.
2012 std::vector<unsigned> InVals;
2013 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2014 InVals.push_back(SelectExpr(N.getOperand(i)));
2015 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002016 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2017 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002018 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002019 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2020 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2021 }
2022 return Result+N.ResNo;
2023 }
2024
2025 case ISD::SHL_PARTS:
2026 case ISD::SRA_PARTS:
2027 case ISD::SRL_PARTS: {
2028 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2029 "Not an i64 shift!");
2030 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2031 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002032 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2033 Tmp1 = MakeReg(MVT::i32);
2034 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002035 Tmp3 = MakeReg(MVT::i32);
2036 unsigned Tmp4 = MakeReg(MVT::i32);
2037 unsigned Tmp5 = MakeReg(MVT::i32);
2038 unsigned Tmp6 = MakeReg(MVT::i32);
2039 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2040 if (ISD::SHL_PARTS == opcode) {
2041 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2042 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2043 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2044 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002045 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002046 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2047 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2048 } else if (ISD::SRL_PARTS == opcode) {
2049 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2050 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2051 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2052 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2053 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2054 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2055 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2056 } else {
2057 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2058 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2059 MachineBasicBlock *OldMBB = BB;
2060 MachineFunction *F = BB->getParent();
2061 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2062 F->getBasicBlockList().insert(It, TmpMBB);
2063 F->getBasicBlockList().insert(It, PhiMBB);
2064 BB->addSuccessor(TmpMBB);
2065 BB->addSuccessor(PhiMBB);
2066 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2067 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2068 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2069 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2070 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2071 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2072 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2073 // Select correct least significant half if the shift amount > 32
2074 BB = TmpMBB;
2075 unsigned Tmp7 = MakeReg(MVT::i32);
2076 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2077 TmpMBB->addSuccessor(PhiMBB);
2078 BB = PhiMBB;
2079 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2080 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002081 }
2082 return Result+N.ResNo;
2083 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002084
Nate Begemana9795f82005-03-24 04:41:43 +00002085 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002086 case ISD::FP_TO_SINT: {
2087 bool U = (ISD::FP_TO_UINT == opcode);
2088 Tmp1 = SelectExpr(N.getOperand(0));
2089 if (!U) {
2090 Tmp2 = MakeReg(MVT::f64);
2091 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2092 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2093 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2094 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2095 return Result;
2096 } else {
2097 unsigned Zero = getConstDouble(0.0);
2098 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2099 unsigned Border = getConstDouble(1LL << 31);
2100 unsigned UseZero = MakeReg(MVT::f64);
2101 unsigned UseMaxInt = MakeReg(MVT::f64);
2102 unsigned UseChoice = MakeReg(MVT::f64);
2103 unsigned TmpReg = MakeReg(MVT::f64);
2104 unsigned TmpReg2 = MakeReg(MVT::f64);
2105 unsigned ConvReg = MakeReg(MVT::f64);
2106 unsigned IntTmp = MakeReg(MVT::i32);
2107 unsigned XorReg = MakeReg(MVT::i32);
2108 MachineFunction *F = BB->getParent();
2109 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2110 // Update machine-CFG edges
2111 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2112 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2113 MachineBasicBlock *OldMBB = BB;
2114 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2115 F->getBasicBlockList().insert(It, XorMBB);
2116 F->getBasicBlockList().insert(It, PhiMBB);
2117 BB->addSuccessor(XorMBB);
2118 BB->addSuccessor(PhiMBB);
2119 // Convert from floating point to unsigned 32-bit value
2120 // Use 0 if incoming value is < 0.0
2121 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2122 // Use 2**32 - 1 if incoming value is >= 2**32
2123 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2124 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2125 .addReg(MaxInt);
2126 // Subtract 2**31
2127 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2128 // Use difference if >= 2**31
2129 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2130 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2131 .addReg(UseChoice);
2132 // Convert to integer
2133 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2134 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2135 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2136 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2137 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2138
2139 // XorMBB:
2140 // add 2**31 if input was >= 2**31
2141 BB = XorMBB;
2142 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2143 XorMBB->addSuccessor(PhiMBB);
2144
2145 // PhiMBB:
2146 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2147 BB = PhiMBB;
2148 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2149 .addReg(XorReg).addMBB(XorMBB);
2150 return Result;
2151 }
2152 assert(0 && "Should never get here");
2153 return 0;
2154 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002155
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002156 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002157 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002158 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002159 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002160 // We can codegen setcc op, imm very efficiently compared to a brcond.
2161 // Check for those cases here.
2162 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002163 if (CN->getValue() == 0) {
2164 Tmp1 = SelectExpr(SetCC->getOperand(0));
2165 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002166 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002167 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002168 Tmp2 = MakeReg(MVT::i32);
2169 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2170 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2171 .addImm(5).addImm(31);
2172 break;
2173 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002174 Tmp2 = MakeReg(MVT::i32);
2175 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2176 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2177 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002178 case ISD::SETLT:
2179 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2180 .addImm(31).addImm(31);
2181 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002182 case ISD::SETGT:
2183 Tmp2 = MakeReg(MVT::i32);
2184 Tmp3 = MakeReg(MVT::i32);
2185 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2186 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2187 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2188 .addImm(31).addImm(31);
2189 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002190 }
2191 return Result;
2192 }
2193 // setcc op, -1
2194 if (CN->isAllOnesValue()) {
2195 Tmp1 = SelectExpr(SetCC->getOperand(0));
2196 switch (SetCC->getCondition()) {
2197 default: assert(0 && "Unhandled SetCC condition"); abort();
2198 case ISD::SETEQ:
2199 Tmp2 = MakeReg(MVT::i32);
2200 Tmp3 = MakeReg(MVT::i32);
2201 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2202 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2203 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002204 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002205 case ISD::SETNE:
2206 Tmp2 = MakeReg(MVT::i32);
2207 Tmp3 = MakeReg(MVT::i32);
2208 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2209 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2210 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2211 break;
2212 case ISD::SETLT:
2213 Tmp2 = MakeReg(MVT::i32);
2214 Tmp3 = MakeReg(MVT::i32);
2215 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2216 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2217 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2218 .addImm(31).addImm(31);
2219 break;
2220 case ISD::SETGT:
2221 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002222 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2223 .addImm(31).addImm(31);
2224 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2225 break;
2226 }
2227 return Result;
2228 }
2229 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002230
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002231 bool Inv;
2232 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2233 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002234 return Result;
2235 }
2236 assert(0 && "Is this legal?");
2237 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002238
Nate Begeman74747862005-03-29 22:24:51 +00002239 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002240 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002241 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2242 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002243 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002244
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002245 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002246 // value and the MBB to hold the PHI instruction for this SetCC.
2247 MachineBasicBlock *thisMBB = BB;
2248 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2249 ilist<MachineBasicBlock>::iterator It = BB;
2250 ++It;
2251
2252 // thisMBB:
2253 // ...
2254 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002255 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002256 // bCC copy1MBB
2257 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002258 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2259 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002260 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002261 MachineFunction *F = BB->getParent();
2262 F->getBasicBlockList().insert(It, copy0MBB);
2263 F->getBasicBlockList().insert(It, sinkMBB);
2264 // Update machine-CFG edges
2265 BB->addSuccessor(copy0MBB);
2266 BB->addSuccessor(sinkMBB);
2267
2268 // copy0MBB:
2269 // %FalseValue = ...
2270 // # fallthrough to sinkMBB
2271 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002272 // Update machine-CFG edges
2273 BB->addSuccessor(sinkMBB);
2274
2275 // sinkMBB:
2276 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2277 // ...
2278 BB = sinkMBB;
2279 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2280 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002281 return Result;
2282 }
Nate Begemana9795f82005-03-24 04:41:43 +00002283
2284 case ISD::Constant:
2285 switch (N.getValueType()) {
2286 default: assert(0 && "Cannot use constants of this type!");
2287 case MVT::i1:
2288 BuildMI(BB, PPC::LI, 1, Result)
2289 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2290 break;
2291 case MVT::i32:
2292 {
2293 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2294 if (v < 32768 && v >= -32768) {
2295 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2296 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002297 Tmp1 = MakeReg(MVT::i32);
2298 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2299 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002300 }
2301 }
2302 }
2303 return Result;
2304 }
2305
2306 return 0;
2307}
2308
2309void ISel::Select(SDOperand N) {
2310 unsigned Tmp1, Tmp2, Opc;
2311 unsigned opcode = N.getOpcode();
2312
2313 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2314 return; // Already selected.
2315
2316 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002317
Nate Begemana9795f82005-03-24 04:41:43 +00002318 switch (Node->getOpcode()) {
2319 default:
2320 Node->dump(); std::cerr << "\n";
2321 assert(0 && "Node not handled yet!");
2322 case ISD::EntryToken: return; // Noop
2323 case ISD::TokenFactor:
2324 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2325 Select(Node->getOperand(i));
2326 return;
2327 case ISD::ADJCALLSTACKDOWN:
2328 case ISD::ADJCALLSTACKUP:
2329 Select(N.getOperand(0));
2330 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2331 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2332 PPC::ADJCALLSTACKUP;
2333 BuildMI(BB, Opc, 1).addImm(Tmp1);
2334 return;
2335 case ISD::BR: {
2336 MachineBasicBlock *Dest =
2337 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002338 Select(N.getOperand(0));
2339 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2340 return;
2341 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002342 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002343 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002344 SelectBranchCC(N);
2345 return;
2346 case ISD::CopyToReg:
2347 Select(N.getOperand(0));
2348 Tmp1 = SelectExpr(N.getOperand(1));
2349 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002350
Nate Begemana9795f82005-03-24 04:41:43 +00002351 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002352 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002353 N.getOperand(1).getValueType() == MVT::f32)
2354 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2355 else
2356 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2357 }
2358 return;
2359 case ISD::ImplicitDef:
2360 Select(N.getOperand(0));
2361 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2362 return;
2363 case ISD::RET:
2364 switch (N.getNumOperands()) {
2365 default:
2366 assert(0 && "Unknown return instruction!");
2367 case 3:
2368 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2369 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002370 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002371 Select(N.getOperand(0));
2372 Tmp1 = SelectExpr(N.getOperand(1));
2373 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002374 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2375 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002376 break;
2377 case 2:
2378 Select(N.getOperand(0));
2379 Tmp1 = SelectExpr(N.getOperand(1));
2380 switch (N.getOperand(1).getValueType()) {
2381 default:
2382 assert(0 && "Unknown return type!");
2383 case MVT::f64:
2384 case MVT::f32:
2385 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2386 break;
2387 case MVT::i32:
2388 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2389 break;
2390 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002391 case 1:
2392 Select(N.getOperand(0));
2393 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002394 }
2395 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2396 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002397 case ISD::TRUNCSTORE:
2398 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002399 {
2400 SDOperand Chain = N.getOperand(0);
2401 SDOperand Value = N.getOperand(1);
2402 SDOperand Address = N.getOperand(2);
2403 Select(Chain);
2404
2405 Tmp1 = SelectExpr(Value); //value
2406
2407 if (opcode == ISD::STORE) {
2408 switch(Value.getValueType()) {
2409 default: assert(0 && "unknown Type in store");
2410 case MVT::i32: Opc = PPC::STW; break;
2411 case MVT::f64: Opc = PPC::STFD; break;
2412 case MVT::f32: Opc = PPC::STFS; break;
2413 }
2414 } else { //ISD::TRUNCSTORE
2415 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2416 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002417 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002418 case MVT::i8: Opc = PPC::STB; break;
2419 case MVT::i16: Opc = PPC::STH; break;
2420 }
2421 }
2422
Nate Begemana7e11a42005-04-01 05:57:17 +00002423 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002424 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002425 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2426 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002427 }
2428 else
2429 {
2430 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002431 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002432 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002433 Opc = IndexedOpForOp(Opc);
2434 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2435 } else {
2436 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2437 }
Nate Begemana9795f82005-03-24 04:41:43 +00002438 }
2439 return;
2440 }
2441 case ISD::EXTLOAD:
2442 case ISD::SEXTLOAD:
2443 case ISD::ZEXTLOAD:
2444 case ISD::LOAD:
2445 case ISD::CopyFromReg:
2446 case ISD::CALL:
2447 case ISD::DYNAMIC_STACKALLOC:
2448 ExprMap.erase(N);
2449 SelectExpr(N);
2450 return;
2451 }
2452 assert(0 && "Should not be reached!");
2453}
2454
2455
2456/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2457/// into a machine code representation using pattern matching and a machine
2458/// description file.
2459///
2460FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002461 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002462}
2463