blob: a5732f05767ab639db58eb9ff6650ae1f601f7a5 [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 Lattner17234b72005-04-30 04:26:06 +000064 // We don't support sin/cos/sqrt
65 setOperationAction(ISD::FSIN , MVT::f64, Expand);
66 setOperationAction(ISD::FCOS , MVT::f64, Expand);
67 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
68 setOperationAction(ISD::FSIN , MVT::f32, Expand);
69 setOperationAction(ISD::FCOS , MVT::f32, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
71
Chris Lattnercbd06fc2005-04-07 19:41:49 +000072 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000073 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000074 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000075
Nate Begemana9795f82005-03-24 04:41:43 +000076 computeRegisterProperties();
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083
Nate Begemana9795f82005-03-24 04:41:43 +000084 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
86 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000087 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
88 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000089
Nate Begemana9795f82005-03-24 04:41:43 +000090 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092
Nate Begemana9795f82005-03-24 04:41:43 +000093 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
100 };
101}
102
103
104std::vector<SDOperand>
105PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 //
107 // add beautiful description of PPC stack frame format, or at least some docs
108 //
109 MachineFunction &MF = DAG.getMachineFunction();
110 MachineFrameInfo *MFI = MF.getFrameInfo();
111 MachineBasicBlock& BB = MF.front();
112 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000113
114 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000115 // fixed size array of physical args, for the sake of simplicity let the STL
116 // handle tracking them for us.
117 std::vector<unsigned> argVR, argPR, argOp;
118 unsigned ArgOffset = 24;
119 unsigned GPR_remaining = 8;
120 unsigned FPR_remaining = 13;
121 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000122 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000123 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
124 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
125 };
126 static const unsigned FPR[] = {
127 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
128 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
129 };
130
131 // Add DAG nodes to load the arguments... On entry to a function on PPC,
132 // the arguments start at offset 24, although they are likely to be passed
133 // in registers.
134 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
135 SDOperand newroot, argt;
136 unsigned ObjSize;
137 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000138 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000139 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000140
Nate Begemana9795f82005-03-24 04:41:43 +0000141 switch (ObjectVT) {
142 default: assert(0 && "Unhandled argument type!");
143 case MVT::i1:
144 case MVT::i8:
145 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000146 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000147 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000148 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000149 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000150 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000151 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
152 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000153 if (ObjectVT != MVT::i32)
154 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000155 } else {
156 needsLoad = true;
157 }
158 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000159 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000160 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000161 if (GPR_remaining > 0) {
162 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000163 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000164 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
165 // If we have two or more remaining argument registers, then both halves
166 // of the i64 can be sourced from there. Otherwise, the lower half will
167 // have to come off the stack. This can happen when an i64 is preceded
168 // by 28 bytes of arguments.
169 if (GPR_remaining > 1) {
170 MF.addLiveIn(GPR[GPR_idx+1]);
171 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
172 } else {
173 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
174 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000175 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000176 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000177 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000178 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
179 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000180 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000181 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000182 }
183 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000184 case MVT::f32:
185 case MVT::f64:
186 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
187 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000188 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000189 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000190 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000191 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000192 --FPR_remaining;
193 ++FPR_idx;
194 } else {
195 needsLoad = true;
196 }
197 break;
198 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000199
Nate Begemana9795f82005-03-24 04:41:43 +0000200 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000202 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000203 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000204 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000205 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000206 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
207 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000208 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000209 DAG.getConstant(SubregOffset, MVT::i32));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000210 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000211 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000212
Nate Begemana9795f82005-03-24 04:41:43 +0000213 // Every 4 bytes of argument space consumes one of the GPRs available for
214 // argument passing.
215 if (GPR_remaining > 0) {
216 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
217 GPR_remaining -= delta;
218 GPR_idx += delta;
219 }
220 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000221 if (newroot.Val)
222 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000223
Nate Begemana9795f82005-03-24 04:41:43 +0000224 ArgValues.push_back(argt);
225 }
226
Nate Begemana9795f82005-03-24 04:41:43 +0000227 // If the function takes variable number of arguments, make a frame index for
228 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000229 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000230 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000231 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000232 // If this function is vararg, store any remaining integer argument regs
233 // to their spots on the stack so that they may be loaded by deferencing the
234 // result of va_next.
235 std::vector<SDOperand> MemOps;
236 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000237 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000238 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000239 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000240 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000241 MemOps.push_back(Store);
242 // Increment the address by four for the next argument to store
243 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
244 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
245 }
246 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000247 }
Nate Begemana9795f82005-03-24 04:41:43 +0000248
Nate Begemancd08e4c2005-04-09 20:09:12 +0000249 // Finally, inform the code generator which regs we return values in.
250 switch (getValueType(F.getReturnType())) {
251 default: assert(0 && "Unknown type!");
252 case MVT::isVoid: break;
253 case MVT::i1:
254 case MVT::i8:
255 case MVT::i16:
256 case MVT::i32:
257 MF.addLiveOut(PPC::R3);
258 break;
259 case MVT::i64:
260 MF.addLiveOut(PPC::R3);
261 MF.addLiveOut(PPC::R4);
262 break;
263 case MVT::f32:
264 case MVT::f64:
265 MF.addLiveOut(PPC::F1);
266 break;
267 }
268
Nate Begemana9795f82005-03-24 04:41:43 +0000269 return ArgValues;
270}
271
272std::pair<SDOperand, SDOperand>
273PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000274 const Type *RetTy, bool isVarArg,
275 SDOperand Callee, ArgListTy &Args,
276 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000277 // args_to_use will accumulate outgoing args for the ISD::CALL case in
278 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000279 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000280
281 // Count how many bytes are to be pushed on the stack, including the linkage
282 // area, and parameter passing area.
283 unsigned NumBytes = 24;
284
285 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000286 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
287 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000288 } else {
289 for (unsigned i = 0, e = Args.size(); i != e; ++i)
290 switch (getValueType(Args[i].second)) {
291 default: assert(0 && "Unknown value type!");
292 case MVT::i1:
293 case MVT::i8:
294 case MVT::i16:
295 case MVT::i32:
296 case MVT::f32:
297 NumBytes += 4;
298 break;
299 case MVT::i64:
300 case MVT::f64:
301 NumBytes += 8;
302 break;
303 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000304
305 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000306 // plus 32 bytes of argument space in case any called code gets funky on us.
307 if (NumBytes < 56) NumBytes = 56;
308
309 // Adjust the stack pointer for the new arguments...
310 // These operations are automatically eliminated by the prolog/epilog pass
311 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
312 DAG.getConstant(NumBytes, getPointerTy()));
313
314 // Set up a copy of the stack pointer for use loading and storing any
315 // arguments that may not fit in the registers available for argument
316 // passing.
317 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
318 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000319
Nate Begeman307e7442005-03-26 01:28:53 +0000320 // Figure out which arguments are going to go in registers, and which in
321 // memory. Also, if this is a vararg function, floating point operations
322 // must be stored to our stack, and loaded into integer regs as well, if
323 // any integer regs are available for argument passing.
324 unsigned ArgOffset = 24;
325 unsigned GPR_remaining = 8;
326 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000327
Nate Begeman74d73452005-03-31 00:15:26 +0000328 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000329 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
330 // PtrOff will be used to store the current argument to the stack if a
331 // register cannot be found for it.
332 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
333 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000334 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000335
Nate Begemanf7e43382005-03-26 07:46:36 +0000336 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000337 default: assert(0 && "Unexpected ValueType for argument!");
338 case MVT::i1:
339 case MVT::i8:
340 case MVT::i16:
341 // Promote the integer to 32 bits. If the input type is signed use a
342 // sign extend, otherwise use a zero extend.
343 if (Args[i].second->isSigned())
344 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
345 else
346 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
347 // FALL THROUGH
348 case MVT::i32:
349 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000350 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000351 --GPR_remaining;
352 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000353 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000354 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000355 }
356 ArgOffset += 4;
357 break;
358 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000359 // If we have one free GPR left, we can place the upper half of the i64
360 // in it, and store the other half to the stack. If we have two or more
361 // free GPRs, then we can pass both halves of the i64 in registers.
362 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000363 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000364 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000365 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000366 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000367 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000368 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000369 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000370 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000371 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000372 } else {
373 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
374 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000375 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000376 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 }
Nate Begeman307e7442005-03-26 01:28:53 +0000378 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000379 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000380 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000381 }
382 ArgOffset += 8;
383 break;
384 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000385 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000386 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000387 args_to_use.push_back(Args[i].first);
388 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000389 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000390 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000391 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000392 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000393 // Float varargs are always shadowed in available integer registers
394 if (GPR_remaining > 0) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000395 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, DAG.getSrcValue(NULL));
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 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000401 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
402 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000403 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000404 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000405 args_to_use.push_back(Load);
406 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000407 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000408 } else {
409 // If we have any FPRs remaining, we may also have GPRs remaining.
410 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
411 // GPRs.
412 if (GPR_remaining > 0) {
413 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
414 --GPR_remaining;
415 }
416 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
417 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
418 --GPR_remaining;
419 }
Nate Begeman74d73452005-03-31 00:15:26 +0000420 }
Nate Begeman307e7442005-03-26 01:28:53 +0000421 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000422 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000423 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000424 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000425 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000426 break;
427 }
Nate Begemana9795f82005-03-24 04:41:43 +0000428 }
Nate Begeman74d73452005-03-31 00:15:26 +0000429 if (!MemOps.empty())
430 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000431 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000432
Nate Begemana9795f82005-03-24 04:41:43 +0000433 std::vector<MVT::ValueType> RetVals;
434 MVT::ValueType RetTyVT = getValueType(RetTy);
435 if (RetTyVT != MVT::isVoid)
436 RetVals.push_back(RetTyVT);
437 RetVals.push_back(MVT::Other);
438
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000439 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000440 Chain, Callee, args_to_use), 0);
441 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
442 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
443 DAG.getConstant(NumBytes, getPointerTy()));
444 return std::make_pair(TheCall, Chain);
445}
446
447std::pair<SDOperand, SDOperand>
448PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
449 //vastart just returns the address of the VarArgsFrameIndex slot.
450 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
451}
452
453std::pair<SDOperand,SDOperand> PPC32TargetLowering::
454LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
455 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000456 MVT::ValueType ArgVT = getValueType(ArgTy);
457 SDOperand Result;
458 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000459 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000460 } else {
461 unsigned Amt;
462 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
463 Amt = 4;
464 else {
465 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
466 "Other types should have been promoted for varargs!");
467 Amt = 8;
468 }
469 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
470 DAG.getConstant(Amt, VAList.getValueType()));
471 }
472 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000473}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000474
Nate Begemana9795f82005-03-24 04:41:43 +0000475
476std::pair<SDOperand, SDOperand> PPC32TargetLowering::
477LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
478 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000479 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000480 abort();
481}
482
483namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000484Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000485Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000486Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000487//===--------------------------------------------------------------------===//
488/// ISel - PPC32 specific code to select PPC32 machine instructions for
489/// SelectionDAG operations.
490//===--------------------------------------------------------------------===//
491class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000492 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000493 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
494 // for sdiv and udiv until it is put into the future
495 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000496
Nate Begemana9795f82005-03-24 04:41:43 +0000497 /// ExprMap - As shared expressions are codegen'd, we keep track of which
498 /// vreg the value is produced in, so we only emit one copy of each compiled
499 /// tree.
500 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000501
502 unsigned GlobalBaseReg;
503 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000504 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000505public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000506 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
507 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000508
Nate Begemanc7b09f12005-03-25 08:34:25 +0000509 /// runOnFunction - Override this function in order to reset our per-function
510 /// variables.
511 virtual bool runOnFunction(Function &Fn) {
512 // Make sure we re-emit a set of the global base reg if necessary
513 GlobalBaseInitialized = false;
514 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000515 }
516
Nate Begemana9795f82005-03-24 04:41:43 +0000517 /// InstructionSelectBasicBlock - This callback is invoked by
518 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
519 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
520 DEBUG(BB->dump());
521 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000522 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000523 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000524
Nate Begemana9795f82005-03-24 04:41:43 +0000525 // Clear state used for selection.
526 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000527 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000528 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000529
530 // dag -> dag expanders for integer divide by constant
531 SDOperand BuildSDIVSequence(SDOperand N);
532 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000533
Nate Begemandffcfcc2005-04-01 00:32:34 +0000534 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000535 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000536 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000537 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000538 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000539 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
540 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000541 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000542 unsigned SelectExprFP(SDOperand N, unsigned Result);
543 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000544
Nate Begeman04730362005-04-01 04:45:11 +0000545 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000546 void SelectBranchCC(SDOperand N);
547};
548
Nate Begeman80196b12005-04-05 00:15:08 +0000549/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
550/// returns zero when the input is not exactly a power of two.
551static unsigned ExactLog2(unsigned Val) {
552 if (Val == 0 || (Val & (Val-1))) return 0;
553 unsigned Count = 0;
554 while (Val != 1) {
555 Val >>= 1;
556 ++Count;
557 }
558 return Count;
559}
560
Nate Begeman7ddecb42005-04-06 23:51:40 +0000561// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
562// any number of 0's on either side. the 1's are allowed to wrap from LSB to
563// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
564// not, since all 1's are not contiguous.
565static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
566 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000567 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000568 ME = 0;
569
570 // look for first set bit
571 int i = 0;
572 for (; i < 32; i++) {
573 if ((Val & (1 << (31 - i))) != 0) {
574 MB = i;
575 ME = i;
576 break;
577 }
578 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000579
Nate Begeman7ddecb42005-04-06 23:51:40 +0000580 // look for last set bit
581 for (; i < 32; i++) {
582 if ((Val & (1 << (31 - i))) == 0)
583 break;
584 ME = i;
585 }
586
587 // look for next set bit
588 for (; i < 32; i++) {
589 if ((Val & (1 << (31 - i))) != 0)
590 break;
591 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000592
Nate Begeman7ddecb42005-04-06 23:51:40 +0000593 // if we exhausted all the bits, we found a match at this point for 0*1*0*
594 if (i == 32)
595 return true;
596
597 // since we just encountered more 1's, if it doesn't wrap around to the
598 // most significant bit of the word, then we did not find a match to 1*0*1* so
599 // exit.
600 if (MB != 0)
601 return false;
602
603 // look for last set bit
604 for (MB = i; i < 32; i++) {
605 if ((Val & (1 << (31 - i))) == 0)
606 break;
607 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000608
Nate Begeman7ddecb42005-04-06 23:51:40 +0000609 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
610 // the value is not a run of ones.
611 if (i == 32)
612 return true;
613 return false;
614}
615
Nate Begeman439b4442005-04-05 04:22:58 +0000616/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000617/// the ConstantSDNode N can be used as an immediate to Opcode. The return
618/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000619/// ConstantSDNode, or is not suitable for use by that opcode.
620/// Return value codes for turning into an enum someday:
621/// 1: constant may be used in normal immediate form.
622/// 2: constant may be used in shifted immediate form.
623/// 3: log base 2 of the constant may be used.
624/// 4: constant is suitable for integer division conversion
625/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000626///
Nate Begeman439b4442005-04-05 04:22:58 +0000627static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
628 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000629 if (N.getOpcode() != ISD::Constant) return 0;
630
631 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000632
Nate Begemana9795f82005-03-24 04:41:43 +0000633 switch(Opcode) {
634 default: return 0;
635 case ISD::ADD:
636 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
637 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
638 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000639 case ISD::AND: {
640 unsigned MB, ME;
641 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
642 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
643 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
644 break;
645 }
Nate Begemana9795f82005-03-24 04:41:43 +0000646 case ISD::XOR:
647 case ISD::OR:
648 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
649 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
650 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000651 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000652 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000653 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
654 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000655 case ISD::SETCC:
656 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
657 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
658 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000659 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000660 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000661 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000662 if (v <= -2 || v >= 2) { return 4; }
663 break;
664 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000665 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000666 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000667 }
668 return 0;
669}
Nate Begeman3e897162005-03-31 23:55:40 +0000670
Nate Begemanc7bd4822005-04-11 06:34:10 +0000671/// NodeHasRecordingVariant - If SelectExpr can always produce code for
672/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
673/// return false.
674static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
675 switch(NodeOpcode) {
676 default: return false;
677 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000678 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000679 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000680 }
681}
682
Nate Begeman3e897162005-03-31 23:55:40 +0000683/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
684/// to Condition. If the Condition is unordered or unsigned, the bool argument
685/// U is set to true, otherwise it is set to false.
686static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
687 U = false;
688 switch (Condition) {
689 default: assert(0 && "Unknown condition!"); abort();
690 case ISD::SETEQ: return PPC::BEQ;
691 case ISD::SETNE: return PPC::BNE;
692 case ISD::SETULT: U = true;
693 case ISD::SETLT: return PPC::BLT;
694 case ISD::SETULE: U = true;
695 case ISD::SETLE: return PPC::BLE;
696 case ISD::SETUGT: U = true;
697 case ISD::SETGT: return PPC::BGT;
698 case ISD::SETUGE: U = true;
699 case ISD::SETGE: return PPC::BGE;
700 }
Nate Begeman04730362005-04-01 04:45:11 +0000701 return 0;
702}
703
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000704/// getCROpForOp - Return the condition register opcode (or inverted opcode)
705/// associated with the SelectionDAG opcode.
706static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
707 switch (Opcode) {
708 default: assert(0 && "Unknown opcode!"); abort();
709 case ISD::AND:
710 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
711 if (!Inv1 && !Inv2) return PPC::CRAND;
712 if (Inv1 ^ Inv2) return PPC::CRANDC;
713 case ISD::OR:
714 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
715 if (!Inv1 && !Inv2) return PPC::CROR;
716 if (Inv1 ^ Inv2) return PPC::CRORC;
717 }
718 return 0;
719}
720
721/// getCRIdxForSetCC - Return the index of the condition register field
722/// associated with the SetCC condition, and whether or not the field is
723/// treated as inverted. That is, lt = 0; ge = 0 inverted.
724static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
725 switch (Condition) {
726 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000727 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000728 case ISD::SETLT: Inv = false; return 0;
729 case ISD::SETUGE:
730 case ISD::SETGE: Inv = true; return 0;
731 case ISD::SETUGT:
732 case ISD::SETGT: Inv = false; return 1;
733 case ISD::SETULE:
734 case ISD::SETLE: Inv = true; return 1;
735 case ISD::SETEQ: Inv = false; return 2;
736 case ISD::SETNE: Inv = true; return 2;
737 }
738 return 0;
739}
740
Nate Begeman04730362005-04-01 04:45:11 +0000741/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
742/// and store immediate instructions.
743static unsigned IndexedOpForOp(unsigned Opcode) {
744 switch(Opcode) {
745 default: assert(0 && "Unknown opcode!"); abort();
746 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
747 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
748 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
749 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
750 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
751 case PPC::LFD: return PPC::LFDX;
752 }
753 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000754}
Nate Begeman815d6da2005-04-06 00:25:27 +0000755
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000756// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000757// a multiply.
758struct ms {
759 int m; // magic number
760 int s; // shift amount
761};
762
763struct mu {
764 unsigned int m; // magic number
765 int a; // add indicator
766 int s; // shift amount
767};
768
769/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000770/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000771/// or -1.
772static struct ms magic(int d) {
773 int p;
774 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
775 const unsigned int two31 = 2147483648U; // 2^31
776 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000777
Nate Begeman815d6da2005-04-06 00:25:27 +0000778 ad = abs(d);
779 t = two31 + ((unsigned int)d >> 31);
780 anc = t - 1 - t%ad; // absolute value of nc
781 p = 31; // initialize p
782 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
783 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
784 q2 = two31/ad; // initialize q2 = 2p/abs(d)
785 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
786 do {
787 p = p + 1;
788 q1 = 2*q1; // update q1 = 2p/abs(nc)
789 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
790 if (r1 >= anc) { // must be unsigned comparison
791 q1 = q1 + 1;
792 r1 = r1 - anc;
793 }
794 q2 = 2*q2; // update q2 = 2p/abs(d)
795 r2 = 2*r2; // update r2 = rem(2p/abs(d))
796 if (r2 >= ad) { // must be unsigned comparison
797 q2 = q2 + 1;
798 r2 = r2 - ad;
799 }
800 delta = ad - r2;
801 } while (q1 < delta || (q1 == delta && r1 == 0));
802
803 mag.m = q2 + 1;
804 if (d < 0) mag.m = -mag.m; // resulting magic number
805 mag.s = p - 32; // resulting shift
806 return mag;
807}
808
809/// magicu - calculate the magic numbers required to codegen an integer udiv as
810/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
811static struct mu magicu(unsigned d)
812{
813 int p;
814 unsigned int nc, delta, q1, r1, q2, r2;
815 struct mu magu;
816 magu.a = 0; // initialize "add" indicator
817 nc = - 1 - (-d)%d;
818 p = 31; // initialize p
819 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
820 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
821 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
822 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
823 do {
824 p = p + 1;
825 if (r1 >= nc - r1 ) {
826 q1 = 2*q1 + 1; // update q1
827 r1 = 2*r1 - nc; // update r1
828 }
829 else {
830 q1 = 2*q1; // update q1
831 r1 = 2*r1; // update r1
832 }
833 if (r2 + 1 >= d - r2) {
834 if (q2 >= 0x7FFFFFFF) magu.a = 1;
835 q2 = 2*q2 + 1; // update q2
836 r2 = 2*r2 + 1 - d; // update r2
837 }
838 else {
839 if (q2 >= 0x80000000) magu.a = 1;
840 q2 = 2*q2; // update q2
841 r2 = 2*r2 + 1; // update r2
842 }
843 delta = d - 1 - r2;
844 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
845 magu.m = q2 + 1; // resulting magic number
846 magu.s = p - 32; // resulting shift
847 return magu;
848}
849}
850
851/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
852/// return a DAG expression to select that will generate the same value by
853/// multiplying by a magic number. See:
854/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
855SDOperand ISel::BuildSDIVSequence(SDOperand N) {
856 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
857 ms magics = magic(d);
858 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000859 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000860 ISelDAG->getConstant(magics.m, MVT::i32));
861 // If d > 0 and m < 0, add the numerator
862 if (d > 0 && magics.m < 0)
863 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
864 // If d < 0 and m > 0, subtract the numerator.
865 if (d < 0 && magics.m > 0)
866 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
867 // Shift right algebraic if shift value is nonzero
868 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000869 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000870 ISelDAG->getConstant(magics.s, MVT::i32));
871 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000872 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000873 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000874 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000875}
876
877/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
878/// return a DAG expression to select that will generate the same value by
879/// multiplying by a magic number. See:
880/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
881SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000882 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000883 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
884 mu magics = magicu(d);
885 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000886 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000887 ISelDAG->getConstant(magics.m, MVT::i32));
888 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000889 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000890 ISelDAG->getConstant(magics.s, MVT::i32));
891 } else {
892 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000893 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000894 ISelDAG->getConstant(1, MVT::i32));
895 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000896 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000897 ISelDAG->getConstant(magics.s-1, MVT::i32));
898 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000899 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000900}
901
Nate Begemanc7b09f12005-03-25 08:34:25 +0000902/// getGlobalBaseReg - Output the instructions required to put the
903/// base address to use for accessing globals into a register.
904///
905unsigned ISel::getGlobalBaseReg() {
906 if (!GlobalBaseInitialized) {
907 // Insert the set of GlobalBaseReg into the first MBB of the function
908 MachineBasicBlock &FirstMBB = BB->getParent()->front();
909 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
910 GlobalBaseReg = MakeReg(MVT::i32);
911 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
912 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
913 GlobalBaseInitialized = true;
914 }
915 return GlobalBaseReg;
916}
917
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000918/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000919/// Constant Pool. Optionally takes a register in which to load the value.
920unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
921 unsigned Tmp1 = MakeReg(MVT::i32);
922 if (0 == Result) Result = MakeReg(MVT::f64);
923 MachineConstantPool *CP = BB->getParent()->getConstantPool();
924 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
925 unsigned CPI = CP->getConstantPoolIndex(CFP);
926 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
927 .addConstantPoolIndex(CPI);
928 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
929 return Result;
930}
931
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000932/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000933/// Inv is true, then invert the result.
934void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
935 unsigned IntCR = MakeReg(MVT::i32);
936 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
937 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
938 if (Inv) {
939 unsigned Tmp1 = MakeReg(MVT::i32);
940 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
941 .addImm(31).addImm(31);
942 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
943 } else {
944 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
945 .addImm(31).addImm(31);
946 }
947}
948
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000949/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000950/// the rotate left word immediate then mask insert (rlwimi) instruction.
951/// Returns true on success, false if the caller still needs to select OR.
952///
953/// Patterns matched:
954/// 1. or shl, and 5. or and, and
955/// 2. or and, shl 6. or shl, shr
956/// 3. or shr, and 7. or shr, shl
957/// 4. or and, shr
958bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000959 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000960 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
961 unsigned Op0Opc = OR.getOperand(0).getOpcode();
962 unsigned Op1Opc = OR.getOperand(1).getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000963
Nate Begeman7ddecb42005-04-06 23:51:40 +0000964 // Verify that we have the correct opcodes
965 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
966 return false;
967 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
968 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000969
Nate Begeman7ddecb42005-04-06 23:51:40 +0000970 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000971 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000972 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
973 switch(Op0Opc) {
974 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
975 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
976 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
977 }
978 } else {
979 return false;
980 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000981
Nate Begeman7ddecb42005-04-06 23:51:40 +0000982 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000983 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000984 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
985 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000986 case ISD::SHL:
987 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000988 InsMask <<= Amount;
989 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000990 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000991 case ISD::SRL:
992 Amount = CN->getValue();
993 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000994 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000995 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000996 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000997 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +0000998 InsMask &= (unsigned)CN->getValue();
999 break;
1000 }
1001 } else {
1002 return false;
1003 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001004
Nate Begeman7ddecb42005-04-06 23:51:40 +00001005 // Verify that the Target mask and Insert mask together form a full word mask
1006 // and that the Insert mask is a run of set bits (which implies both are runs
1007 // of set bits). Given that, Select the arguments and generate the rlwimi
1008 // instruction.
1009 unsigned MB, ME;
1010 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
1011 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001012 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1013 // where both bitfield halves are sourced from the same value.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001014 if (IsRotate &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001015 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001016 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1017 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1018 .addImm(0).addImm(31);
1019 return true;
1020 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001021 if (Op0Opc == ISD::AND)
1022 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1023 else
1024 Tmp1 = SelectExpr(OR.getOperand(0));
1025 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
1026 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1027 .addImm(Amount).addImm(MB).addImm(ME);
1028 return true;
1029 }
1030 return false;
1031}
1032
Nate Begeman3664cef2005-04-13 22:14:14 +00001033/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1034/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1035/// wider than the implicit mask, then we can get rid of the AND and let the
1036/// shift do the mask.
1037unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1038 unsigned C;
1039 if (N.getOpcode() == ISD::AND &&
1040 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1041 31 == (C & 0xFFFF) && // ME
1042 26 >= (C >> 16)) // MB
1043 return SelectExpr(N.getOperand(0));
1044 else
1045 return SelectExpr(N);
1046}
1047
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001048unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001049 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001050 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001051 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001052 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001053
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001054 // Allocate a condition register for this expression
1055 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001056
Nate Begemandffcfcc2005-04-01 00:32:34 +00001057 // If the first operand to the select is a SETCC node, then we can fold it
1058 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001059 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001060 bool U;
1061 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001062 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001063
Nate Begeman439b4442005-04-05 04:22:58 +00001064 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001065 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001066 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001067 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001068 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001069 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1070 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001071 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001072 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1073 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001074 RecordSuccess = false;
1075 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1076 if (RecordSuccess) {
1077 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001078 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1079 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001080 }
1081 AlreadySelected = true;
1082 }
1083 // If we could not implicitly set CR0, then emit a compare immediate
1084 // instead.
1085 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001086 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001087 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001088 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001089 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001090 } else {
1091 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1092 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001093 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001094 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001095 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001096 }
1097 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001098 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001099 return SelectCCExpr(CC, Opc, Inv, Idx);
1100 // If this isn't a SetCC, then select the value and compare it against zero,
1101 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001102 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001103 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001104 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001105 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001106 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001107 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001108}
1109
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001110unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001111 unsigned &Idx) {
1112 bool Inv0, Inv1;
1113 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1114
1115 // Allocate a condition register for this expression
1116 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1117
1118 // Check for the operations we support:
1119 switch(N.getOpcode()) {
1120 default:
1121 Opc = PPC::BNE;
1122 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1123 Tmp1 = SelectExpr(N);
1124 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1125 break;
1126 case ISD::OR:
1127 case ISD::AND:
1128 ++MultiBranch;
1129 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1130 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1131 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1132 if (Inv0 && !Inv1) {
1133 std::swap(Tmp1, Tmp2);
1134 std::swap(Idx0, Idx1);
1135 Opc = Opc1;
1136 }
1137 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1138 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1139 .addReg(Tmp2).addImm(Idx1);
1140 Inv = false;
1141 Idx = Idx0;
1142 break;
1143 case ISD::SETCC:
1144 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1145 Result = Tmp1;
1146 break;
1147 }
1148 return Result;
1149}
1150
Nate Begemandffcfcc2005-04-01 00:32:34 +00001151/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001152bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001153{
Nate Begeman96fc6812005-03-31 02:05:53 +00001154 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001155 if (N.getOpcode() == ISD::ADD) {
1156 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001157 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001158 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001159 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001160 }
Nate Begeman04730362005-04-01 04:45:11 +00001161 offset = SelectExpr(N.getOperand(1));
1162 return true;
1163 }
Nate Begemana9795f82005-03-24 04:41:43 +00001164 Reg = SelectExpr(N);
1165 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001166 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001167}
1168
1169void ISel::SelectBranchCC(SDOperand N)
1170{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001171 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001172 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001173
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001174 bool Inv;
1175 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001176 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001177 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001178
Nate Begemancd08e4c2005-04-09 20:09:12 +00001179 // Iterate to the next basic block, unless we're already at the end of the
1180 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001181 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001182
1183 // If this is a two way branch, then grab the fallthrough basic block argument
1184 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1185 // if necessary by the branch selection pass. Otherwise, emit a standard
1186 // conditional branch.
1187 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001188 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001189 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1190 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001191 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001192 .addMBB(Dest).addMBB(Fallthrough);
1193 if (Fallthrough != It)
1194 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1195 } else {
1196 if (Fallthrough != It) {
1197 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001198 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001199 .addMBB(Fallthrough).addMBB(Dest);
1200 }
1201 }
1202 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001203 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001204 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001205 }
Nate Begemana9795f82005-03-24 04:41:43 +00001206 return;
1207}
1208
1209unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1210{
1211 unsigned Tmp1, Tmp2, Tmp3;
1212 unsigned Opc = 0;
1213 SDNode *Node = N.Val;
1214 MVT::ValueType DestType = N.getValueType();
1215 unsigned opcode = N.getOpcode();
1216
1217 switch (opcode) {
1218 default:
1219 Node->dump();
1220 assert(0 && "Node not handled!\n");
1221
Nate Begeman23afcfb2005-03-29 22:48:55 +00001222 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001223 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1224 // and an FP comparison in the SetCC node.
1225 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1226 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1227 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1228 SetCC->getCondition() != ISD::SETEQ &&
1229 SetCC->getCondition() != ISD::SETNE) {
1230 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001231 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1232 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001233
Nate Begeman3e897162005-03-31 23:55:40 +00001234 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1235 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1236 switch(SetCC->getCondition()) {
1237 default: assert(0 && "Invalid FSEL condition"); abort();
1238 case ISD::SETULT:
1239 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001240 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001241 case ISD::SETUGE:
1242 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001243 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001244 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1245 return Result;
1246 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001247 case ISD::SETGT:
1248 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001249 case ISD::SETULE:
1250 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001251 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1252 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1253 } else {
1254 Tmp2 = MakeReg(VT);
1255 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1256 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1257 }
Nate Begeman3e897162005-03-31 23:55:40 +00001258 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1259 return Result;
1260 }
1261 }
1262 } else {
1263 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001264 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001265 Tmp2 = SelectExpr(SetCC->getOperand(1));
1266 Tmp3 = MakeReg(VT);
1267 switch(SetCC->getCondition()) {
1268 default: assert(0 && "Invalid FSEL condition"); abort();
1269 case ISD::SETULT:
1270 case ISD::SETLT:
1271 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1272 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1273 return Result;
1274 case ISD::SETUGE:
1275 case ISD::SETGE:
1276 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1277 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1278 return Result;
1279 case ISD::SETUGT:
1280 case ISD::SETGT:
1281 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1282 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1283 return Result;
1284 case ISD::SETULE:
1285 case ISD::SETLE:
1286 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1287 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1288 return Result;
1289 }
1290 }
1291 assert(0 && "Should never get here");
1292 return 0;
1293 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001294
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001295 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001296 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1297 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001298 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001299
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001300 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001301 // value and the MBB to hold the PHI instruction for this SetCC.
1302 MachineBasicBlock *thisMBB = BB;
1303 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1304 ilist<MachineBasicBlock>::iterator It = BB;
1305 ++It;
1306
1307 // thisMBB:
1308 // ...
1309 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001310 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001311 // bCC copy1MBB
1312 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001313 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1314 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001315 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001316 MachineFunction *F = BB->getParent();
1317 F->getBasicBlockList().insert(It, copy0MBB);
1318 F->getBasicBlockList().insert(It, sinkMBB);
1319 // Update machine-CFG edges
1320 BB->addSuccessor(copy0MBB);
1321 BB->addSuccessor(sinkMBB);
1322
1323 // copy0MBB:
1324 // %FalseValue = ...
1325 // # fallthrough to sinkMBB
1326 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001327 // Update machine-CFG edges
1328 BB->addSuccessor(sinkMBB);
1329
1330 // sinkMBB:
1331 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1332 // ...
1333 BB = sinkMBB;
1334 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1335 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1336 return Result;
1337 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001338
1339 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001340 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001341 ISD::ADD == N.getOperand(0).getOpcode() &&
1342 N.getOperand(0).Val->hasOneUse() &&
1343 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1344 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001345 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001346 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1347 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1348 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1349 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1350 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001351 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001352 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001353 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001354 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1355 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001356 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001357 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1358 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1359 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1360 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001361 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1362 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001363 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1364 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1365 } else {
1366 Tmp1 = SelectExpr(N.getOperand(0));
1367 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1368 }
1369 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001370
Nate Begeman27eeb002005-04-02 05:59:34 +00001371 case ISD::FABS:
1372 Tmp1 = SelectExpr(N.getOperand(0));
1373 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1374 return Result;
1375
Nate Begemana9795f82005-03-24 04:41:43 +00001376 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001377 assert (DestType == MVT::f32 &&
1378 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001379 "only f64 to f32 conversion supported here");
1380 Tmp1 = SelectExpr(N.getOperand(0));
1381 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1382 return Result;
1383
1384 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001385 assert (DestType == MVT::f64 &&
1386 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001387 "only f32 to f64 conversion supported here");
1388 Tmp1 = SelectExpr(N.getOperand(0));
1389 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1390 return Result;
1391
1392 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001393 if (Result == 1)
1394 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1395 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1396 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1397 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001398
Nate Begeman6d369cc2005-04-01 01:08:07 +00001399 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001400 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001401 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001402 return Result;
1403 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001404
Nate Begemana9795f82005-03-24 04:41:43 +00001405 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001406 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1407 N.getOperand(0).Val->hasOneUse()) {
1408 ++FusedFP; // Statistic
1409 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1410 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1411 Tmp3 = SelectExpr(N.getOperand(1));
1412 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1413 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1414 return Result;
1415 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001416 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1417 N.getOperand(1).Val->hasOneUse()) {
1418 ++FusedFP; // Statistic
1419 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1420 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1421 Tmp3 = SelectExpr(N.getOperand(0));
1422 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1423 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1424 return Result;
1425 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001426 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1427 Tmp1 = SelectExpr(N.getOperand(0));
1428 Tmp2 = SelectExpr(N.getOperand(1));
1429 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1430 return Result;
1431
Nate Begemana9795f82005-03-24 04:41:43 +00001432 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001433 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1434 N.getOperand(0).Val->hasOneUse()) {
1435 ++FusedFP; // Statistic
1436 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1437 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1438 Tmp3 = SelectExpr(N.getOperand(1));
1439 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1440 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1441 return Result;
1442 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001443 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1444 N.getOperand(1).Val->hasOneUse()) {
1445 ++FusedFP; // Statistic
1446 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1447 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1448 Tmp3 = SelectExpr(N.getOperand(0));
1449 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1450 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1451 return Result;
1452 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001453 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1454 Tmp1 = SelectExpr(N.getOperand(0));
1455 Tmp2 = SelectExpr(N.getOperand(1));
1456 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1457 return Result;
1458
1459 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001460 case ISD::SDIV:
1461 switch( opcode ) {
1462 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001463 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1464 };
Nate Begemana9795f82005-03-24 04:41:43 +00001465 Tmp1 = SelectExpr(N.getOperand(0));
1466 Tmp2 = SelectExpr(N.getOperand(1));
1467 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1468 return Result;
1469
Nate Begemana9795f82005-03-24 04:41:43 +00001470 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001471 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001472 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001473 && "int to float must operate on i32");
1474 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1475 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1476 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1477 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001478
Nate Begemanfdcf3412005-03-30 19:38:35 +00001479 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1480 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001481
Nate Begemanfdcf3412005-03-30 19:38:35 +00001482 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001483 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001484 // Store the hi & low halves of the fp value, currently in int regs
1485 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1486 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1487 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1488 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1489 // Generate the return value with a subtract
1490 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1491 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001492 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001493 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001494 // Store the hi & low halves of the fp value, currently in int regs
1495 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1496 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1497 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1498 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1499 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1500 // Generate the return value with a subtract
1501 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1502 }
1503 return Result;
1504 }
Nate Begemana9795f82005-03-24 04:41:43 +00001505 }
Nate Begeman6b559972005-04-01 02:59:27 +00001506 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001507 return 0;
1508}
1509
Nate Begemanc7bd4822005-04-11 06:34:10 +00001510unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001511 unsigned Result;
1512 unsigned Tmp1, Tmp2, Tmp3;
1513 unsigned Opc = 0;
1514 unsigned opcode = N.getOpcode();
1515
1516 SDNode *Node = N.Val;
1517 MVT::ValueType DestType = N.getValueType();
1518
1519 unsigned &Reg = ExprMap[N];
1520 if (Reg) return Reg;
1521
Nate Begeman27eeb002005-04-02 05:59:34 +00001522 switch (N.getOpcode()) {
1523 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001524 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001525 MakeReg(N.getValueType()) : 1;
1526 break;
1527 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001528 // If this is a call instruction, make sure to prepare ALL of the result
1529 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001530 if (Node->getNumValues() == 1)
1531 Reg = Result = 1; // Void call, just a chain.
1532 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001533 Result = MakeReg(Node->getValueType(0));
1534 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001535 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001536 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001537 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001538 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001539 break;
1540 case ISD::ADD_PARTS:
1541 case ISD::SUB_PARTS:
1542 case ISD::SHL_PARTS:
1543 case ISD::SRL_PARTS:
1544 case ISD::SRA_PARTS:
1545 Result = MakeReg(Node->getValueType(0));
1546 ExprMap[N.getValue(0)] = Result;
1547 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1548 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1549 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001550 }
1551
Nate Begemane5846682005-04-04 06:52:38 +00001552 if (ISD::CopyFromReg == opcode)
1553 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001554
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001555 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001556 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Nate Begemana0e3e942005-04-10 01:14:13 +00001557 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001558 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001559
1560 switch (opcode) {
1561 default:
1562 Node->dump();
1563 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001564 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001565 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1566 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001567 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001568 // Generate both result values. FIXME: Need a better commment here?
1569 if (Result != 1)
1570 ExprMap[N.getValue(1)] = 1;
1571 else
1572 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1573
1574 // FIXME: We are currently ignoring the requested alignment for handling
1575 // greater than the stack alignment. This will need to be revisited at some
1576 // point. Align = N.getOperand(2);
1577 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1578 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1579 std::cerr << "Cannot allocate stack object with greater alignment than"
1580 << " the stack alignment yet!";
1581 abort();
1582 }
1583 Select(N.getOperand(0));
1584 Tmp1 = SelectExpr(N.getOperand(1));
1585 // Subtract size from stack pointer, thereby allocating some space.
1586 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1587 // Put a pointer to the space into the result register by copying the SP
1588 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1589 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001590
1591 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001592 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1593 Tmp2 = MakeReg(MVT::i32);
1594 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1595 .addConstantPoolIndex(Tmp1);
1596 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1597 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001598
1599 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001600 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001601 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001602 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001603
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001604 case ISD::GlobalAddress: {
1605 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001606 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001607 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1608 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001609 if (GV->hasWeakLinkage() || GV->isExternal()) {
1610 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1611 } else {
1612 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1613 }
1614 return Result;
1615 }
1616
Nate Begeman5e966612005-03-24 06:28:42 +00001617 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001618 case ISD::EXTLOAD:
1619 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001620 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001621 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1622 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001623 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001624
Nate Begeman5e966612005-03-24 06:28:42 +00001625 // Make sure we generate both values.
1626 if (Result != 1)
1627 ExprMap[N.getValue(1)] = 1; // Generate the token
1628 else
1629 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1630
1631 SDOperand Chain = N.getOperand(0);
1632 SDOperand Address = N.getOperand(1);
1633 Select(Chain);
1634
Nate Begeman9db505c2005-03-28 19:36:43 +00001635 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001636 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001637 case MVT::i1: Opc = PPC::LBZ; break;
1638 case MVT::i8: Opc = PPC::LBZ; break;
1639 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1640 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001641 case MVT::f32: Opc = PPC::LFS; break;
1642 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001643 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001644
Nate Begeman74d73452005-03-31 00:15:26 +00001645 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1646 Tmp1 = MakeReg(MVT::i32);
1647 int CPI = CP->getIndex();
1648 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1649 .addConstantPoolIndex(CPI);
1650 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001651 }
Nate Begeman74d73452005-03-31 00:15:26 +00001652 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001653 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1654 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001655 } else {
1656 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001657 bool idx = SelectAddr(Address, Tmp1, offset);
1658 if (idx) {
1659 Opc = IndexedOpForOp(Opc);
1660 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1661 } else {
1662 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1663 }
Nate Begeman5e966612005-03-24 06:28:42 +00001664 }
1665 return Result;
1666 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001667
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001668 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001669 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001670 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001671 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1672 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1673 };
1674 static const unsigned FPR[] = {
1675 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1676 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1677 };
1678
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001679 // Lower the chain for this call.
1680 Select(N.getOperand(0));
1681 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001682
Nate Begemand860aa62005-04-04 22:17:48 +00001683 MachineInstr *CallMI;
1684 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001685 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001686 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001687 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001688 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001689 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001690 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001691 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001692 true);
1693 } else {
1694 Tmp1 = SelectExpr(N.getOperand(1));
1695 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1696 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1697 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1698 .addReg(PPC::R12);
1699 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001700
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001701 // Load the register args to virtual regs
1702 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001703 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001704 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1705
1706 // Copy the virtual registers into the appropriate argument register
1707 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1708 switch(N.getOperand(i+2).getValueType()) {
1709 default: Node->dump(); assert(0 && "Unknown value type for call");
1710 case MVT::i1:
1711 case MVT::i8:
1712 case MVT::i16:
1713 case MVT::i32:
1714 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001715 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001716 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001717 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1718 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001719 ++GPR_idx;
1720 break;
1721 case MVT::f64:
1722 case MVT::f32:
1723 assert(FPR_idx < 13 && "Too many fp args");
1724 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001725 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001726 ++FPR_idx;
1727 break;
1728 }
1729 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001730
Nate Begemand860aa62005-04-04 22:17:48 +00001731 // Put the call instruction in the correct place in the MachineBasicBlock
1732 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001733
1734 switch (Node->getValueType(0)) {
1735 default: assert(0 && "Unknown value type for call result!");
1736 case MVT::Other: return 1;
1737 case MVT::i1:
1738 case MVT::i8:
1739 case MVT::i16:
1740 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001741 if (Node->getValueType(1) == MVT::i32) {
1742 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1743 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1744 } else {
1745 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1746 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001747 break;
1748 case MVT::f32:
1749 case MVT::f64:
1750 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1751 break;
1752 }
1753 return Result+N.ResNo;
1754 }
Nate Begemana9795f82005-03-24 04:41:43 +00001755
1756 case ISD::SIGN_EXTEND:
1757 case ISD::SIGN_EXTEND_INREG:
1758 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001759 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1760 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001761 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001762 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001763 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001764 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001765 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001766 break;
Nate Begeman74747862005-03-29 22:24:51 +00001767 case MVT::i1:
1768 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1769 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001770 }
Nate Begemana9795f82005-03-24 04:41:43 +00001771 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001772
Nate Begemana9795f82005-03-24 04:41:43 +00001773 case ISD::CopyFromReg:
1774 if (Result == 1)
1775 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1776 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1777 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1778 return Result;
1779
1780 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001781 Tmp1 = SelectExpr(N.getOperand(0));
1782 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1783 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001784 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001785 .addImm(31-Tmp2);
1786 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001787 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001788 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1789 }
1790 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001791
Nate Begeman5e966612005-03-24 06:28:42 +00001792 case ISD::SRL:
1793 Tmp1 = SelectExpr(N.getOperand(0));
1794 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1795 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001796 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001797 .addImm(Tmp2).addImm(31);
1798 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001799 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001800 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1801 }
1802 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001803
Nate Begeman5e966612005-03-24 06:28:42 +00001804 case ISD::SRA:
1805 Tmp1 = SelectExpr(N.getOperand(0));
1806 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1807 Tmp2 = CN->getValue() & 0x1F;
1808 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1809 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001810 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001811 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1812 }
1813 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001814
Nate Begemana9795f82005-03-24 04:41:43 +00001815 case ISD::ADD:
1816 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1817 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001818 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001819 default: assert(0 && "unhandled result code");
1820 case 0: // No immediate
1821 Tmp2 = SelectExpr(N.getOperand(1));
1822 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 break;
1824 case 1: // Low immediate
1825 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1826 break;
1827 case 2: // Shifted immediate
1828 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1829 break;
1830 }
1831 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001832
Nate Begemana9795f82005-03-24 04:41:43 +00001833 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001834 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001835 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001836 N.getOperand(1).getOpcode() == ISD::SETCC) {
1837 bool Inv;
1838 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1839 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1840 return Result;
1841 }
1842 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001843 Tmp1 = SelectExpr(N.getOperand(0));
1844 // FIXME: should add check in getImmediateForOpcode to return a value
1845 // indicating the immediate is a run of set bits so we can emit a bitfield
1846 // clear with RLWINM instead.
1847 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1848 default: assert(0 && "unhandled result code");
1849 case 0: // No immediate
1850 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001851 Opc = Recording ? PPC::ANDo : PPC::AND;
1852 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001853 break;
1854 case 1: // Low immediate
1855 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1856 break;
1857 case 2: // Shifted immediate
1858 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1859 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001860 case 5: // Bitfield mask
1861 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1862 Tmp3 = Tmp2 >> 16; // MB
1863 Tmp2 &= 0xFFFF; // ME
1864 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1865 .addImm(Tmp3).addImm(Tmp2);
1866 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001867 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001868 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001869 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001870
Nate Begemana9795f82005-03-24 04:41:43 +00001871 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001872 if (SelectBitfieldInsert(N, Result))
1873 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001874 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001875 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001876 N.getOperand(1).getOpcode() == ISD::SETCC) {
1877 bool Inv;
1878 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1879 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1880 return Result;
1881 }
1882 }
Nate Begemana9795f82005-03-24 04:41:43 +00001883 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001884 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001885 default: assert(0 && "unhandled result code");
1886 case 0: // No immediate
1887 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001888 Opc = Recording ? PPC::ORo : PPC::OR;
1889 RecordSuccess = true;
1890 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001891 break;
1892 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001893 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001894 break;
1895 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001896 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001897 break;
1898 }
1899 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001900
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001901 case ISD::XOR: {
1902 // Check for EQV: xor, (xor a, -1), b
1903 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1904 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1905 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001906 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1907 Tmp2 = SelectExpr(N.getOperand(1));
1908 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1909 return Result;
1910 }
Chris Lattner837a5212005-04-21 21:09:11 +00001911 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001912 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1913 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001914 switch(N.getOperand(0).getOpcode()) {
1915 case ISD::OR:
1916 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1917 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1918 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1919 break;
1920 case ISD::AND:
1921 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1922 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1923 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1924 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001925 case ISD::XOR:
1926 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1927 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1928 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1929 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001930 default:
1931 Tmp1 = SelectExpr(N.getOperand(0));
1932 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1933 break;
1934 }
1935 return Result;
1936 }
1937 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001938 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001939 default: assert(0 && "unhandled result code");
1940 case 0: // No immediate
1941 Tmp2 = SelectExpr(N.getOperand(1));
1942 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1943 break;
1944 case 1: // Low immediate
1945 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1946 break;
1947 case 2: // Shifted immediate
1948 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1949 break;
1950 }
1951 return Result;
1952 }
1953
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001954 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001955 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001956 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001957 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1958 else {
1959 Tmp1 = SelectExpr(N.getOperand(0));
1960 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1961 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001962 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001963
Nate Begeman5e966612005-03-24 06:28:42 +00001964 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001965 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001966 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001967 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1968 else {
1969 Tmp2 = SelectExpr(N.getOperand(1));
1970 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1971 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001972 return Result;
1973
Nate Begeman815d6da2005-04-06 00:25:27 +00001974 case ISD::MULHS:
1975 case ISD::MULHU:
1976 Tmp1 = SelectExpr(N.getOperand(0));
1977 Tmp2 = SelectExpr(N.getOperand(1));
1978 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1979 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1980 return Result;
1981
Nate Begemanf3d08f32005-03-29 00:03:27 +00001982 case ISD::SDIV:
1983 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001984 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1985 default: break;
1986 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1987 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001988 Tmp1 = MakeReg(MVT::i32);
1989 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001990 if ((int)Tmp3 < 0) {
1991 unsigned Tmp4 = MakeReg(MVT::i32);
1992 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1993 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1994 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1995 } else {
1996 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1997 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1998 }
Nate Begeman80196b12005-04-05 00:15:08 +00001999 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00002000 // If this is a divide by constant, we can emit code using some magic
2001 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00002002 case 4:
2003 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002004 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00002005 return SelectExpr(BuildSDIVSequence(N));
2006 else
2007 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002008 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002009 Tmp1 = SelectExpr(N.getOperand(0));
2010 Tmp2 = SelectExpr(N.getOperand(1));
2011 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2012 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2013 return Result;
2014
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002015 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002016 case ISD::SUB_PARTS: {
2017 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2018 "Not an i64 add/sub!");
2019 // Emit all of the operands.
2020 std::vector<unsigned> InVals;
2021 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2022 InVals.push_back(SelectExpr(N.getOperand(i)));
2023 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002024 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2025 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002026 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002027 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2028 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2029 }
2030 return Result+N.ResNo;
2031 }
2032
2033 case ISD::SHL_PARTS:
2034 case ISD::SRA_PARTS:
2035 case ISD::SRL_PARTS: {
2036 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2037 "Not an i64 shift!");
2038 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2039 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002040 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2041 Tmp1 = MakeReg(MVT::i32);
2042 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002043 Tmp3 = MakeReg(MVT::i32);
2044 unsigned Tmp4 = MakeReg(MVT::i32);
2045 unsigned Tmp5 = MakeReg(MVT::i32);
2046 unsigned Tmp6 = MakeReg(MVT::i32);
2047 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2048 if (ISD::SHL_PARTS == opcode) {
2049 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2050 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2051 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2052 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002053 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002054 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2055 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2056 } else if (ISD::SRL_PARTS == opcode) {
2057 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2058 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2059 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2060 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2061 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2062 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2063 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2064 } else {
2065 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2066 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2067 MachineBasicBlock *OldMBB = BB;
2068 MachineFunction *F = BB->getParent();
2069 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2070 F->getBasicBlockList().insert(It, TmpMBB);
2071 F->getBasicBlockList().insert(It, PhiMBB);
2072 BB->addSuccessor(TmpMBB);
2073 BB->addSuccessor(PhiMBB);
2074 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2075 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2076 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2077 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2078 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2079 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2080 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2081 // Select correct least significant half if the shift amount > 32
2082 BB = TmpMBB;
2083 unsigned Tmp7 = MakeReg(MVT::i32);
2084 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2085 TmpMBB->addSuccessor(PhiMBB);
2086 BB = PhiMBB;
2087 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2088 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002089 }
2090 return Result+N.ResNo;
2091 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002092
Nate Begemana9795f82005-03-24 04:41:43 +00002093 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002094 case ISD::FP_TO_SINT: {
2095 bool U = (ISD::FP_TO_UINT == opcode);
2096 Tmp1 = SelectExpr(N.getOperand(0));
2097 if (!U) {
2098 Tmp2 = MakeReg(MVT::f64);
2099 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2100 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2101 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2102 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2103 return Result;
2104 } else {
2105 unsigned Zero = getConstDouble(0.0);
2106 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2107 unsigned Border = getConstDouble(1LL << 31);
2108 unsigned UseZero = MakeReg(MVT::f64);
2109 unsigned UseMaxInt = MakeReg(MVT::f64);
2110 unsigned UseChoice = MakeReg(MVT::f64);
2111 unsigned TmpReg = MakeReg(MVT::f64);
2112 unsigned TmpReg2 = MakeReg(MVT::f64);
2113 unsigned ConvReg = MakeReg(MVT::f64);
2114 unsigned IntTmp = MakeReg(MVT::i32);
2115 unsigned XorReg = MakeReg(MVT::i32);
2116 MachineFunction *F = BB->getParent();
2117 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2118 // Update machine-CFG edges
2119 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2120 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2121 MachineBasicBlock *OldMBB = BB;
2122 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2123 F->getBasicBlockList().insert(It, XorMBB);
2124 F->getBasicBlockList().insert(It, PhiMBB);
2125 BB->addSuccessor(XorMBB);
2126 BB->addSuccessor(PhiMBB);
2127 // Convert from floating point to unsigned 32-bit value
2128 // Use 0 if incoming value is < 0.0
2129 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2130 // Use 2**32 - 1 if incoming value is >= 2**32
2131 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2132 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2133 .addReg(MaxInt);
2134 // Subtract 2**31
2135 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2136 // Use difference if >= 2**31
2137 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2138 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2139 .addReg(UseChoice);
2140 // Convert to integer
2141 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2142 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2143 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2144 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2145 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2146
2147 // XorMBB:
2148 // add 2**31 if input was >= 2**31
2149 BB = XorMBB;
2150 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2151 XorMBB->addSuccessor(PhiMBB);
2152
2153 // PhiMBB:
2154 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2155 BB = PhiMBB;
2156 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2157 .addReg(XorReg).addMBB(XorMBB);
2158 return Result;
2159 }
2160 assert(0 && "Should never get here");
2161 return 0;
2162 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002163
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002164 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002165 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002166 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002167 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002168 // We can codegen setcc op, imm very efficiently compared to a brcond.
2169 // Check for those cases here.
2170 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002171 if (CN->getValue() == 0) {
2172 Tmp1 = SelectExpr(SetCC->getOperand(0));
2173 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002174 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002175 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002176 Tmp2 = MakeReg(MVT::i32);
2177 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2178 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2179 .addImm(5).addImm(31);
2180 break;
2181 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002182 Tmp2 = MakeReg(MVT::i32);
2183 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2184 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2185 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002186 case ISD::SETLT:
2187 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2188 .addImm(31).addImm(31);
2189 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002190 case ISD::SETGT:
2191 Tmp2 = MakeReg(MVT::i32);
2192 Tmp3 = MakeReg(MVT::i32);
2193 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2194 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2195 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2196 .addImm(31).addImm(31);
2197 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002198 }
2199 return Result;
2200 }
2201 // setcc op, -1
2202 if (CN->isAllOnesValue()) {
2203 Tmp1 = SelectExpr(SetCC->getOperand(0));
2204 switch (SetCC->getCondition()) {
2205 default: assert(0 && "Unhandled SetCC condition"); abort();
2206 case ISD::SETEQ:
2207 Tmp2 = MakeReg(MVT::i32);
2208 Tmp3 = MakeReg(MVT::i32);
2209 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2210 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2211 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002212 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002213 case ISD::SETNE:
2214 Tmp2 = MakeReg(MVT::i32);
2215 Tmp3 = MakeReg(MVT::i32);
2216 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2217 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2218 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2219 break;
2220 case ISD::SETLT:
2221 Tmp2 = MakeReg(MVT::i32);
2222 Tmp3 = MakeReg(MVT::i32);
2223 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2224 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2225 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2226 .addImm(31).addImm(31);
2227 break;
2228 case ISD::SETGT:
2229 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002230 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2231 .addImm(31).addImm(31);
2232 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2233 break;
2234 }
2235 return Result;
2236 }
2237 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002238
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002239 bool Inv;
2240 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2241 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002242 return Result;
2243 }
2244 assert(0 && "Is this legal?");
2245 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002246
Nate Begeman74747862005-03-29 22:24:51 +00002247 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002248 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002249 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2250 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002251 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002252
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002253 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002254 // value and the MBB to hold the PHI instruction for this SetCC.
2255 MachineBasicBlock *thisMBB = BB;
2256 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2257 ilist<MachineBasicBlock>::iterator It = BB;
2258 ++It;
2259
2260 // thisMBB:
2261 // ...
2262 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002263 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002264 // bCC copy1MBB
2265 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002266 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2267 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002268 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002269 MachineFunction *F = BB->getParent();
2270 F->getBasicBlockList().insert(It, copy0MBB);
2271 F->getBasicBlockList().insert(It, sinkMBB);
2272 // Update machine-CFG edges
2273 BB->addSuccessor(copy0MBB);
2274 BB->addSuccessor(sinkMBB);
2275
2276 // copy0MBB:
2277 // %FalseValue = ...
2278 // # fallthrough to sinkMBB
2279 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002280 // Update machine-CFG edges
2281 BB->addSuccessor(sinkMBB);
2282
2283 // sinkMBB:
2284 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2285 // ...
2286 BB = sinkMBB;
2287 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2288 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002289 return Result;
2290 }
Nate Begemana9795f82005-03-24 04:41:43 +00002291
2292 case ISD::Constant:
2293 switch (N.getValueType()) {
2294 default: assert(0 && "Cannot use constants of this type!");
2295 case MVT::i1:
2296 BuildMI(BB, PPC::LI, 1, Result)
2297 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2298 break;
2299 case MVT::i32:
2300 {
2301 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2302 if (v < 32768 && v >= -32768) {
2303 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2304 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002305 Tmp1 = MakeReg(MVT::i32);
2306 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2307 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002308 }
2309 }
2310 }
2311 return Result;
2312 }
2313
2314 return 0;
2315}
2316
2317void ISel::Select(SDOperand N) {
2318 unsigned Tmp1, Tmp2, Opc;
2319 unsigned opcode = N.getOpcode();
2320
2321 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2322 return; // Already selected.
2323
2324 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002325
Nate Begemana9795f82005-03-24 04:41:43 +00002326 switch (Node->getOpcode()) {
2327 default:
2328 Node->dump(); std::cerr << "\n";
2329 assert(0 && "Node not handled yet!");
2330 case ISD::EntryToken: return; // Noop
2331 case ISD::TokenFactor:
2332 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2333 Select(Node->getOperand(i));
2334 return;
2335 case ISD::ADJCALLSTACKDOWN:
2336 case ISD::ADJCALLSTACKUP:
2337 Select(N.getOperand(0));
2338 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2339 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2340 PPC::ADJCALLSTACKUP;
2341 BuildMI(BB, Opc, 1).addImm(Tmp1);
2342 return;
2343 case ISD::BR: {
2344 MachineBasicBlock *Dest =
2345 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002346 Select(N.getOperand(0));
2347 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2348 return;
2349 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002350 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002351 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002352 SelectBranchCC(N);
2353 return;
2354 case ISD::CopyToReg:
2355 Select(N.getOperand(0));
2356 Tmp1 = SelectExpr(N.getOperand(1));
2357 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002358
Nate Begemana9795f82005-03-24 04:41:43 +00002359 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002360 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002361 N.getOperand(1).getValueType() == MVT::f32)
2362 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2363 else
2364 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2365 }
2366 return;
2367 case ISD::ImplicitDef:
2368 Select(N.getOperand(0));
2369 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2370 return;
2371 case ISD::RET:
2372 switch (N.getNumOperands()) {
2373 default:
2374 assert(0 && "Unknown return instruction!");
2375 case 3:
2376 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2377 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002378 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002379 Select(N.getOperand(0));
2380 Tmp1 = SelectExpr(N.getOperand(1));
2381 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002382 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2383 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002384 break;
2385 case 2:
2386 Select(N.getOperand(0));
2387 Tmp1 = SelectExpr(N.getOperand(1));
2388 switch (N.getOperand(1).getValueType()) {
2389 default:
2390 assert(0 && "Unknown return type!");
2391 case MVT::f64:
2392 case MVT::f32:
2393 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2394 break;
2395 case MVT::i32:
2396 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2397 break;
2398 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002399 case 1:
2400 Select(N.getOperand(0));
2401 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002402 }
2403 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2404 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002405 case ISD::TRUNCSTORE:
2406 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002407 {
2408 SDOperand Chain = N.getOperand(0);
2409 SDOperand Value = N.getOperand(1);
2410 SDOperand Address = N.getOperand(2);
2411 Select(Chain);
2412
2413 Tmp1 = SelectExpr(Value); //value
2414
2415 if (opcode == ISD::STORE) {
2416 switch(Value.getValueType()) {
2417 default: assert(0 && "unknown Type in store");
2418 case MVT::i32: Opc = PPC::STW; break;
2419 case MVT::f64: Opc = PPC::STFD; break;
2420 case MVT::f32: Opc = PPC::STFS; break;
2421 }
2422 } else { //ISD::TRUNCSTORE
2423 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2424 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002425 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002426 case MVT::i8: Opc = PPC::STB; break;
2427 case MVT::i16: Opc = PPC::STH; break;
2428 }
2429 }
2430
Nate Begemana7e11a42005-04-01 05:57:17 +00002431 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002432 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002433 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2434 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002435 }
2436 else
2437 {
2438 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002439 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002440 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002441 Opc = IndexedOpForOp(Opc);
2442 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2443 } else {
2444 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2445 }
Nate Begemana9795f82005-03-24 04:41:43 +00002446 }
2447 return;
2448 }
2449 case ISD::EXTLOAD:
2450 case ISD::SEXTLOAD:
2451 case ISD::ZEXTLOAD:
2452 case ISD::LOAD:
2453 case ISD::CopyFromReg:
2454 case ISD::CALL:
2455 case ISD::DYNAMIC_STACKALLOC:
2456 ExprMap.erase(N);
2457 SelectExpr(N);
2458 return;
2459 }
2460 assert(0 && "Should not be reached!");
2461}
2462
2463
2464/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2465/// into a machine code representation using pattern matching and a machine
2466/// description file.
2467///
2468FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002469 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002470}
2471