blob: 6d687ef9f3ea4866a018781d22fcdc3ba3e07b6f [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
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000072 //PowerPC has these, but they are not implemented
73 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
74 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000075 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000076
Chris Lattnercbd06fc2005-04-07 19:41:49 +000077 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000078 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000079 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000080
Nate Begemana9795f82005-03-24 04:41:43 +000081 computeRegisterProperties();
82 }
83
84 /// LowerArguments - This hook must be implemented to indicate how we should
85 /// lower the arguments for the specified function, into the specified DAG.
86 virtual std::vector<SDOperand>
87 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000088
Nate Begemana9795f82005-03-24 04:41:43 +000089 /// LowerCallTo - This hook lowers an abstract call to a function into an
90 /// actual call.
91 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000092 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
93 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000094
Nate Begemana9795f82005-03-24 04:41:43 +000095 virtual std::pair<SDOperand, SDOperand>
96 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000097
Nate Begemana9795f82005-03-24 04:41:43 +000098 virtual std::pair<SDOperand,SDOperand>
99 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
100 const Type *ArgTy, SelectionDAG &DAG);
101
102 virtual std::pair<SDOperand, SDOperand>
103 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
104 SelectionDAG &DAG);
105 };
106}
107
108
109std::vector<SDOperand>
110PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
111 //
112 // add beautiful description of PPC stack frame format, or at least some docs
113 //
114 MachineFunction &MF = DAG.getMachineFunction();
115 MachineFrameInfo *MFI = MF.getFrameInfo();
116 MachineBasicBlock& BB = MF.front();
117 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000118
119 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000120 // fixed size array of physical args, for the sake of simplicity let the STL
121 // handle tracking them for us.
122 std::vector<unsigned> argVR, argPR, argOp;
123 unsigned ArgOffset = 24;
124 unsigned GPR_remaining = 8;
125 unsigned FPR_remaining = 13;
126 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000127 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000128 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
129 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
130 };
131 static const unsigned FPR[] = {
132 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
133 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
134 };
135
136 // Add DAG nodes to load the arguments... On entry to a function on PPC,
137 // the arguments start at offset 24, although they are likely to be passed
138 // in registers.
139 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
140 SDOperand newroot, argt;
141 unsigned ObjSize;
142 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000143 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000144 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000145
Nate Begemana9795f82005-03-24 04:41:43 +0000146 switch (ObjectVT) {
147 default: assert(0 && "Unhandled argument type!");
148 case MVT::i1:
149 case MVT::i8:
150 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000151 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000152 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000153 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000154 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000155 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000156 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
157 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000158 if (ObjectVT != MVT::i32)
159 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000160 } else {
161 needsLoad = true;
162 }
163 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000164 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000165 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000166 if (GPR_remaining > 0) {
167 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000168 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000169 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
170 // If we have two or more remaining argument registers, then both halves
171 // of the i64 can be sourced from there. Otherwise, the lower half will
172 // have to come off the stack. This can happen when an i64 is preceded
173 // by 28 bytes of arguments.
174 if (GPR_remaining > 1) {
175 MF.addLiveIn(GPR[GPR_idx+1]);
176 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
177 } else {
178 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
179 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000180 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000181 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000182 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000183 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
184 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000185 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000186 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000187 }
188 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000189 case MVT::f32:
190 case MVT::f64:
191 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
192 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000193 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000194 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000195 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000196 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000197 --FPR_remaining;
198 ++FPR_idx;
199 } else {
200 needsLoad = true;
201 }
202 break;
203 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000204
Nate Begemana9795f82005-03-24 04:41:43 +0000205 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000206 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000207 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000208 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000209 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000210 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000211 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
212 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000213 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000214 DAG.getConstant(SubregOffset, MVT::i32));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000215 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000216 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000217
Nate Begemana9795f82005-03-24 04:41:43 +0000218 // Every 4 bytes of argument space consumes one of the GPRs available for
219 // argument passing.
220 if (GPR_remaining > 0) {
221 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
222 GPR_remaining -= delta;
223 GPR_idx += delta;
224 }
225 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000226 if (newroot.Val)
227 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000228
Nate Begemana9795f82005-03-24 04:41:43 +0000229 ArgValues.push_back(argt);
230 }
231
Nate Begemana9795f82005-03-24 04:41:43 +0000232 // If the function takes variable number of arguments, make a frame index for
233 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000234 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000235 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000236 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000237 // If this function is vararg, store any remaining integer argument regs
238 // to their spots on the stack so that they may be loaded by deferencing the
239 // result of va_next.
240 std::vector<SDOperand> MemOps;
241 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000242 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000243 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000244 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000245 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000246 MemOps.push_back(Store);
247 // Increment the address by four for the next argument to store
248 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
249 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
250 }
251 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000252 }
Nate Begemana9795f82005-03-24 04:41:43 +0000253
Nate Begemancd08e4c2005-04-09 20:09:12 +0000254 // Finally, inform the code generator which regs we return values in.
255 switch (getValueType(F.getReturnType())) {
256 default: assert(0 && "Unknown type!");
257 case MVT::isVoid: break;
258 case MVT::i1:
259 case MVT::i8:
260 case MVT::i16:
261 case MVT::i32:
262 MF.addLiveOut(PPC::R3);
263 break;
264 case MVT::i64:
265 MF.addLiveOut(PPC::R3);
266 MF.addLiveOut(PPC::R4);
267 break;
268 case MVT::f32:
269 case MVT::f64:
270 MF.addLiveOut(PPC::F1);
271 break;
272 }
273
Nate Begemana9795f82005-03-24 04:41:43 +0000274 return ArgValues;
275}
276
277std::pair<SDOperand, SDOperand>
278PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000279 const Type *RetTy, bool isVarArg,
280 SDOperand Callee, ArgListTy &Args,
281 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000282 // args_to_use will accumulate outgoing args for the ISD::CALL case in
283 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000284 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000285
286 // Count how many bytes are to be pushed on the stack, including the linkage
287 // area, and parameter passing area.
288 unsigned NumBytes = 24;
289
290 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000291 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
292 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000293 } else {
294 for (unsigned i = 0, e = Args.size(); i != e; ++i)
295 switch (getValueType(Args[i].second)) {
296 default: assert(0 && "Unknown value type!");
297 case MVT::i1:
298 case MVT::i8:
299 case MVT::i16:
300 case MVT::i32:
301 case MVT::f32:
302 NumBytes += 4;
303 break;
304 case MVT::i64:
305 case MVT::f64:
306 NumBytes += 8;
307 break;
308 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000309
310 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000311 // plus 32 bytes of argument space in case any called code gets funky on us.
312 if (NumBytes < 56) NumBytes = 56;
313
314 // Adjust the stack pointer for the new arguments...
315 // These operations are automatically eliminated by the prolog/epilog pass
316 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
317 DAG.getConstant(NumBytes, getPointerTy()));
318
319 // Set up a copy of the stack pointer for use loading and storing any
320 // arguments that may not fit in the registers available for argument
321 // passing.
322 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
323 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000324
Nate Begeman307e7442005-03-26 01:28:53 +0000325 // Figure out which arguments are going to go in registers, and which in
326 // memory. Also, if this is a vararg function, floating point operations
327 // must be stored to our stack, and loaded into integer regs as well, if
328 // any integer regs are available for argument passing.
329 unsigned ArgOffset = 24;
330 unsigned GPR_remaining = 8;
331 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000332
Nate Begeman74d73452005-03-31 00:15:26 +0000333 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000334 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
335 // PtrOff will be used to store the current argument to the stack if a
336 // register cannot be found for it.
337 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
338 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000339 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000340
Nate Begemanf7e43382005-03-26 07:46:36 +0000341 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000342 default: assert(0 && "Unexpected ValueType for argument!");
343 case MVT::i1:
344 case MVT::i8:
345 case MVT::i16:
346 // Promote the integer to 32 bits. If the input type is signed use a
347 // sign extend, otherwise use a zero extend.
348 if (Args[i].second->isSigned())
349 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
350 else
351 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
352 // FALL THROUGH
353 case MVT::i32:
354 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000355 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000356 --GPR_remaining;
357 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000358 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000359 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000360 }
361 ArgOffset += 4;
362 break;
363 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000364 // If we have one free GPR left, we can place the upper half of the i64
365 // in it, and store the other half to the stack. If we have two or more
366 // free GPRs, then we can pass both halves of the i64 in registers.
367 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000368 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000369 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000370 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000371 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000372 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000373 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000374 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000375 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000376 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 } else {
378 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
379 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000380 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000381 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000382 }
Nate Begeman307e7442005-03-26 01:28:53 +0000383 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000384 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000385 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000386 }
387 ArgOffset += 8;
388 break;
389 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000390 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000391 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000392 args_to_use.push_back(Args[i].first);
393 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000394 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000395 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000396 Args[i].first, PtrOff, DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000397 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000398 // Float varargs are always shadowed in available integer registers
399 if (GPR_remaining > 0) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000400 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000401 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000402 args_to_use.push_back(Load);
403 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000404 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000405 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000406 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
407 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000408 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000409 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000410 args_to_use.push_back(Load);
411 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000412 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000413 } else {
414 // If we have any FPRs remaining, we may also have GPRs remaining.
415 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
416 // GPRs.
417 if (GPR_remaining > 0) {
418 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
419 --GPR_remaining;
420 }
421 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
422 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
423 --GPR_remaining;
424 }
Nate Begeman74d73452005-03-31 00:15:26 +0000425 }
Nate Begeman307e7442005-03-26 01:28:53 +0000426 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000427 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000428 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000429 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000430 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000431 break;
432 }
Nate Begemana9795f82005-03-24 04:41:43 +0000433 }
Nate Begeman74d73452005-03-31 00:15:26 +0000434 if (!MemOps.empty())
435 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000436 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000437
Nate Begemana9795f82005-03-24 04:41:43 +0000438 std::vector<MVT::ValueType> RetVals;
439 MVT::ValueType RetTyVT = getValueType(RetTy);
440 if (RetTyVT != MVT::isVoid)
441 RetVals.push_back(RetTyVT);
442 RetVals.push_back(MVT::Other);
443
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000444 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000445 Chain, Callee, args_to_use), 0);
446 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
447 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
448 DAG.getConstant(NumBytes, getPointerTy()));
449 return std::make_pair(TheCall, Chain);
450}
451
452std::pair<SDOperand, SDOperand>
453PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
454 //vastart just returns the address of the VarArgsFrameIndex slot.
455 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
456}
457
458std::pair<SDOperand,SDOperand> PPC32TargetLowering::
459LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
460 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000461 MVT::ValueType ArgVT = getValueType(ArgTy);
462 SDOperand Result;
463 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000464 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000465 } else {
466 unsigned Amt;
467 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
468 Amt = 4;
469 else {
470 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
471 "Other types should have been promoted for varargs!");
472 Amt = 8;
473 }
474 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
475 DAG.getConstant(Amt, VAList.getValueType()));
476 }
477 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000478}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000479
Nate Begemana9795f82005-03-24 04:41:43 +0000480
481std::pair<SDOperand, SDOperand> PPC32TargetLowering::
482LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
483 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000484 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000485 abort();
486}
487
488namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000489Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000490Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000491Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000492//===--------------------------------------------------------------------===//
493/// ISel - PPC32 specific code to select PPC32 machine instructions for
494/// SelectionDAG operations.
495//===--------------------------------------------------------------------===//
496class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000497 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000498 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
499 // for sdiv and udiv until it is put into the future
500 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000501
Nate Begemana9795f82005-03-24 04:41:43 +0000502 /// ExprMap - As shared expressions are codegen'd, we keep track of which
503 /// vreg the value is produced in, so we only emit one copy of each compiled
504 /// tree.
505 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000506
507 unsigned GlobalBaseReg;
508 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000509 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000510public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000511 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
512 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000513
Nate Begemanc7b09f12005-03-25 08:34:25 +0000514 /// runOnFunction - Override this function in order to reset our per-function
515 /// variables.
516 virtual bool runOnFunction(Function &Fn) {
517 // Make sure we re-emit a set of the global base reg if necessary
518 GlobalBaseInitialized = false;
519 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000520 }
521
Nate Begemana9795f82005-03-24 04:41:43 +0000522 /// InstructionSelectBasicBlock - This callback is invoked by
523 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
524 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
525 DEBUG(BB->dump());
526 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000527 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000528 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000529
Nate Begemana9795f82005-03-24 04:41:43 +0000530 // Clear state used for selection.
531 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000532 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000533 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000534
535 // dag -> dag expanders for integer divide by constant
536 SDOperand BuildSDIVSequence(SDOperand N);
537 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000538
Nate Begemandffcfcc2005-04-01 00:32:34 +0000539 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000540 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000541 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000542 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000543 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000544 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
545 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000546 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000547 unsigned SelectExprFP(SDOperand N, unsigned Result);
548 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000549
Nate Begeman04730362005-04-01 04:45:11 +0000550 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000551 void SelectBranchCC(SDOperand N);
552};
553
Nate Begeman80196b12005-04-05 00:15:08 +0000554/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
555/// returns zero when the input is not exactly a power of two.
556static unsigned ExactLog2(unsigned Val) {
557 if (Val == 0 || (Val & (Val-1))) return 0;
558 unsigned Count = 0;
559 while (Val != 1) {
560 Val >>= 1;
561 ++Count;
562 }
563 return Count;
564}
565
Nate Begeman7ddecb42005-04-06 23:51:40 +0000566// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
567// any number of 0's on either side. the 1's are allowed to wrap from LSB to
568// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
569// not, since all 1's are not contiguous.
570static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
571 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000572 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000573 ME = 0;
574
575 // look for first set bit
576 int i = 0;
577 for (; i < 32; i++) {
578 if ((Val & (1 << (31 - i))) != 0) {
579 MB = i;
580 ME = i;
581 break;
582 }
583 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000584
Nate Begeman7ddecb42005-04-06 23:51:40 +0000585 // look for last set bit
586 for (; i < 32; i++) {
587 if ((Val & (1 << (31 - i))) == 0)
588 break;
589 ME = i;
590 }
591
592 // look for next set bit
593 for (; i < 32; i++) {
594 if ((Val & (1 << (31 - i))) != 0)
595 break;
596 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000597
Nate Begeman7ddecb42005-04-06 23:51:40 +0000598 // if we exhausted all the bits, we found a match at this point for 0*1*0*
599 if (i == 32)
600 return true;
601
602 // since we just encountered more 1's, if it doesn't wrap around to the
603 // most significant bit of the word, then we did not find a match to 1*0*1* so
604 // exit.
605 if (MB != 0)
606 return false;
607
608 // look for last set bit
609 for (MB = i; i < 32; i++) {
610 if ((Val & (1 << (31 - i))) == 0)
611 break;
612 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000613
Nate Begeman7ddecb42005-04-06 23:51:40 +0000614 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
615 // the value is not a run of ones.
616 if (i == 32)
617 return true;
618 return false;
619}
620
Nate Begeman439b4442005-04-05 04:22:58 +0000621/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000622/// the ConstantSDNode N can be used as an immediate to Opcode. The return
623/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000624/// ConstantSDNode, or is not suitable for use by that opcode.
625/// Return value codes for turning into an enum someday:
626/// 1: constant may be used in normal immediate form.
627/// 2: constant may be used in shifted immediate form.
628/// 3: log base 2 of the constant may be used.
629/// 4: constant is suitable for integer division conversion
630/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000631///
Nate Begeman439b4442005-04-05 04:22:58 +0000632static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
633 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000634 if (N.getOpcode() != ISD::Constant) return 0;
635
636 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000637
Nate Begemana9795f82005-03-24 04:41:43 +0000638 switch(Opcode) {
639 default: return 0;
640 case ISD::ADD:
641 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
642 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
643 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000644 case ISD::AND: {
645 unsigned MB, ME;
646 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
647 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
648 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
649 break;
650 }
Nate Begemana9795f82005-03-24 04:41:43 +0000651 case ISD::XOR:
652 case ISD::OR:
653 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
654 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
655 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000656 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000657 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000658 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
659 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000660 case ISD::SETCC:
661 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
662 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
663 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000664 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000665 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000666 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000667 if (v <= -2 || v >= 2) { return 4; }
668 break;
669 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000670 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000671 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000672 }
673 return 0;
674}
Nate Begeman3e897162005-03-31 23:55:40 +0000675
Nate Begemanc7bd4822005-04-11 06:34:10 +0000676/// NodeHasRecordingVariant - If SelectExpr can always produce code for
677/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
678/// return false.
679static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
680 switch(NodeOpcode) {
681 default: return false;
682 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000683 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000684 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000685 }
686}
687
Nate Begeman3e897162005-03-31 23:55:40 +0000688/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
689/// to Condition. If the Condition is unordered or unsigned, the bool argument
690/// U is set to true, otherwise it is set to false.
691static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
692 U = false;
693 switch (Condition) {
694 default: assert(0 && "Unknown condition!"); abort();
695 case ISD::SETEQ: return PPC::BEQ;
696 case ISD::SETNE: return PPC::BNE;
697 case ISD::SETULT: U = true;
698 case ISD::SETLT: return PPC::BLT;
699 case ISD::SETULE: U = true;
700 case ISD::SETLE: return PPC::BLE;
701 case ISD::SETUGT: U = true;
702 case ISD::SETGT: return PPC::BGT;
703 case ISD::SETUGE: U = true;
704 case ISD::SETGE: return PPC::BGE;
705 }
Nate Begeman04730362005-04-01 04:45:11 +0000706 return 0;
707}
708
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000709/// getCROpForOp - Return the condition register opcode (or inverted opcode)
710/// associated with the SelectionDAG opcode.
711static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
712 switch (Opcode) {
713 default: assert(0 && "Unknown opcode!"); abort();
714 case ISD::AND:
715 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
716 if (!Inv1 && !Inv2) return PPC::CRAND;
717 if (Inv1 ^ Inv2) return PPC::CRANDC;
718 case ISD::OR:
719 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
720 if (!Inv1 && !Inv2) return PPC::CROR;
721 if (Inv1 ^ Inv2) return PPC::CRORC;
722 }
723 return 0;
724}
725
726/// getCRIdxForSetCC - Return the index of the condition register field
727/// associated with the SetCC condition, and whether or not the field is
728/// treated as inverted. That is, lt = 0; ge = 0 inverted.
729static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
730 switch (Condition) {
731 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000732 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000733 case ISD::SETLT: Inv = false; return 0;
734 case ISD::SETUGE:
735 case ISD::SETGE: Inv = true; return 0;
736 case ISD::SETUGT:
737 case ISD::SETGT: Inv = false; return 1;
738 case ISD::SETULE:
739 case ISD::SETLE: Inv = true; return 1;
740 case ISD::SETEQ: Inv = false; return 2;
741 case ISD::SETNE: Inv = true; return 2;
742 }
743 return 0;
744}
745
Nate Begeman04730362005-04-01 04:45:11 +0000746/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
747/// and store immediate instructions.
748static unsigned IndexedOpForOp(unsigned Opcode) {
749 switch(Opcode) {
750 default: assert(0 && "Unknown opcode!"); abort();
751 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
752 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
753 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
754 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
755 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
756 case PPC::LFD: return PPC::LFDX;
757 }
758 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000759}
Nate Begeman815d6da2005-04-06 00:25:27 +0000760
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000761// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000762// a multiply.
763struct ms {
764 int m; // magic number
765 int s; // shift amount
766};
767
768struct mu {
769 unsigned int m; // magic number
770 int a; // add indicator
771 int s; // shift amount
772};
773
774/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000775/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000776/// or -1.
777static struct ms magic(int d) {
778 int p;
779 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
780 const unsigned int two31 = 2147483648U; // 2^31
781 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000782
Nate Begeman815d6da2005-04-06 00:25:27 +0000783 ad = abs(d);
784 t = two31 + ((unsigned int)d >> 31);
785 anc = t - 1 - t%ad; // absolute value of nc
786 p = 31; // initialize p
787 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
788 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
789 q2 = two31/ad; // initialize q2 = 2p/abs(d)
790 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
791 do {
792 p = p + 1;
793 q1 = 2*q1; // update q1 = 2p/abs(nc)
794 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
795 if (r1 >= anc) { // must be unsigned comparison
796 q1 = q1 + 1;
797 r1 = r1 - anc;
798 }
799 q2 = 2*q2; // update q2 = 2p/abs(d)
800 r2 = 2*r2; // update r2 = rem(2p/abs(d))
801 if (r2 >= ad) { // must be unsigned comparison
802 q2 = q2 + 1;
803 r2 = r2 - ad;
804 }
805 delta = ad - r2;
806 } while (q1 < delta || (q1 == delta && r1 == 0));
807
808 mag.m = q2 + 1;
809 if (d < 0) mag.m = -mag.m; // resulting magic number
810 mag.s = p - 32; // resulting shift
811 return mag;
812}
813
814/// magicu - calculate the magic numbers required to codegen an integer udiv as
815/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
816static struct mu magicu(unsigned d)
817{
818 int p;
819 unsigned int nc, delta, q1, r1, q2, r2;
820 struct mu magu;
821 magu.a = 0; // initialize "add" indicator
822 nc = - 1 - (-d)%d;
823 p = 31; // initialize p
824 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
825 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
826 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
827 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
828 do {
829 p = p + 1;
830 if (r1 >= nc - r1 ) {
831 q1 = 2*q1 + 1; // update q1
832 r1 = 2*r1 - nc; // update r1
833 }
834 else {
835 q1 = 2*q1; // update q1
836 r1 = 2*r1; // update r1
837 }
838 if (r2 + 1 >= d - r2) {
839 if (q2 >= 0x7FFFFFFF) magu.a = 1;
840 q2 = 2*q2 + 1; // update q2
841 r2 = 2*r2 + 1 - d; // update r2
842 }
843 else {
844 if (q2 >= 0x80000000) magu.a = 1;
845 q2 = 2*q2; // update q2
846 r2 = 2*r2 + 1; // update r2
847 }
848 delta = d - 1 - r2;
849 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
850 magu.m = q2 + 1; // resulting magic number
851 magu.s = p - 32; // resulting shift
852 return magu;
853}
854}
855
856/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
857/// return a DAG expression to select that will generate the same value by
858/// multiplying by a magic number. See:
859/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
860SDOperand ISel::BuildSDIVSequence(SDOperand N) {
861 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
862 ms magics = magic(d);
863 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000864 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000865 ISelDAG->getConstant(magics.m, MVT::i32));
866 // If d > 0 and m < 0, add the numerator
867 if (d > 0 && magics.m < 0)
868 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
869 // If d < 0 and m > 0, subtract the numerator.
870 if (d < 0 && magics.m > 0)
871 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
872 // Shift right algebraic if shift value is nonzero
873 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000874 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000875 ISelDAG->getConstant(magics.s, MVT::i32));
876 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000877 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000878 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000879 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000880}
881
882/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
883/// return a DAG expression to select that will generate the same value by
884/// multiplying by a magic number. See:
885/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
886SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000887 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000888 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
889 mu magics = magicu(d);
890 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000891 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000892 ISelDAG->getConstant(magics.m, MVT::i32));
893 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000894 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000895 ISelDAG->getConstant(magics.s, MVT::i32));
896 } else {
897 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000898 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000899 ISelDAG->getConstant(1, MVT::i32));
900 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000901 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000902 ISelDAG->getConstant(magics.s-1, MVT::i32));
903 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000904 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000905}
906
Nate Begemanc7b09f12005-03-25 08:34:25 +0000907/// getGlobalBaseReg - Output the instructions required to put the
908/// base address to use for accessing globals into a register.
909///
910unsigned ISel::getGlobalBaseReg() {
911 if (!GlobalBaseInitialized) {
912 // Insert the set of GlobalBaseReg into the first MBB of the function
913 MachineBasicBlock &FirstMBB = BB->getParent()->front();
914 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
915 GlobalBaseReg = MakeReg(MVT::i32);
916 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
917 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
918 GlobalBaseInitialized = true;
919 }
920 return GlobalBaseReg;
921}
922
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000923/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000924/// Constant Pool. Optionally takes a register in which to load the value.
925unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
926 unsigned Tmp1 = MakeReg(MVT::i32);
927 if (0 == Result) Result = MakeReg(MVT::f64);
928 MachineConstantPool *CP = BB->getParent()->getConstantPool();
929 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
930 unsigned CPI = CP->getConstantPoolIndex(CFP);
931 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
932 .addConstantPoolIndex(CPI);
933 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
934 return Result;
935}
936
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000937/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000938/// Inv is true, then invert the result.
939void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
940 unsigned IntCR = MakeReg(MVT::i32);
941 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
942 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
943 if (Inv) {
944 unsigned Tmp1 = MakeReg(MVT::i32);
945 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
946 .addImm(31).addImm(31);
947 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
948 } else {
949 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
950 .addImm(31).addImm(31);
951 }
952}
953
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000954/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000955/// the rotate left word immediate then mask insert (rlwimi) instruction.
956/// Returns true on success, false if the caller still needs to select OR.
957///
958/// Patterns matched:
959/// 1. or shl, and 5. or and, and
960/// 2. or and, shl 6. or shl, shr
961/// 3. or shr, and 7. or shr, shl
962/// 4. or and, shr
963bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000964 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000965 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
966 unsigned Op0Opc = OR.getOperand(0).getOpcode();
967 unsigned Op1Opc = OR.getOperand(1).getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000968
Nate Begeman7ddecb42005-04-06 23:51:40 +0000969 // Verify that we have the correct opcodes
970 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
971 return false;
972 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
973 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000974
Nate Begeman7ddecb42005-04-06 23:51:40 +0000975 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000976 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000977 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
978 switch(Op0Opc) {
979 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
980 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
981 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
982 }
983 } else {
984 return false;
985 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000986
Nate Begeman7ddecb42005-04-06 23:51:40 +0000987 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000988 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000989 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
990 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000991 case ISD::SHL:
992 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000993 InsMask <<= Amount;
994 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000995 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000996 case ISD::SRL:
997 Amount = CN->getValue();
998 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000999 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001000 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001001 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001002 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001003 InsMask &= (unsigned)CN->getValue();
1004 break;
1005 }
1006 } else {
1007 return false;
1008 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001009
Nate Begeman7ddecb42005-04-06 23:51:40 +00001010 // Verify that the Target mask and Insert mask together form a full word mask
1011 // and that the Insert mask is a run of set bits (which implies both are runs
1012 // of set bits). Given that, Select the arguments and generate the rlwimi
1013 // instruction.
1014 unsigned MB, ME;
1015 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
1016 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001017 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1018 // where both bitfield halves are sourced from the same value.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001019 if (IsRotate &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001020 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001021 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1022 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1023 .addImm(0).addImm(31);
1024 return true;
1025 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001026 if (Op0Opc == ISD::AND)
1027 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1028 else
1029 Tmp1 = SelectExpr(OR.getOperand(0));
1030 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
1031 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1032 .addImm(Amount).addImm(MB).addImm(ME);
1033 return true;
1034 }
1035 return false;
1036}
1037
Nate Begeman3664cef2005-04-13 22:14:14 +00001038/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1039/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1040/// wider than the implicit mask, then we can get rid of the AND and let the
1041/// shift do the mask.
1042unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1043 unsigned C;
1044 if (N.getOpcode() == ISD::AND &&
1045 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1046 31 == (C & 0xFFFF) && // ME
1047 26 >= (C >> 16)) // MB
1048 return SelectExpr(N.getOperand(0));
1049 else
1050 return SelectExpr(N);
1051}
1052
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001053unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001054 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001055 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001056 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001057 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001058
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001059 // Allocate a condition register for this expression
1060 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001061
Nate Begemandffcfcc2005-04-01 00:32:34 +00001062 // If the first operand to the select is a SETCC node, then we can fold it
1063 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001064 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001065 bool U;
1066 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001067 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001068
Nate Begeman439b4442005-04-05 04:22:58 +00001069 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001070 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001071 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001072 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001073 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001074 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1075 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001076 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001077 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1078 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001079 RecordSuccess = false;
1080 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1081 if (RecordSuccess) {
1082 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001083 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1084 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001085 }
1086 AlreadySelected = true;
1087 }
1088 // If we could not implicitly set CR0, then emit a compare immediate
1089 // instead.
1090 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001091 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001092 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001093 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001094 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001095 } else {
1096 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1097 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001098 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001099 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001100 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001101 }
1102 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001103 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001104 return SelectCCExpr(CC, Opc, Inv, Idx);
1105 // If this isn't a SetCC, then select the value and compare it against zero,
1106 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001107 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001108 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001109 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001110 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001111 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001112 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001113}
1114
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001115unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001116 unsigned &Idx) {
1117 bool Inv0, Inv1;
1118 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1119
1120 // Allocate a condition register for this expression
1121 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1122
1123 // Check for the operations we support:
1124 switch(N.getOpcode()) {
1125 default:
1126 Opc = PPC::BNE;
1127 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1128 Tmp1 = SelectExpr(N);
1129 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1130 break;
1131 case ISD::OR:
1132 case ISD::AND:
1133 ++MultiBranch;
1134 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1135 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1136 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1137 if (Inv0 && !Inv1) {
1138 std::swap(Tmp1, Tmp2);
1139 std::swap(Idx0, Idx1);
1140 Opc = Opc1;
1141 }
1142 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1143 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1144 .addReg(Tmp2).addImm(Idx1);
1145 Inv = false;
1146 Idx = Idx0;
1147 break;
1148 case ISD::SETCC:
1149 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1150 Result = Tmp1;
1151 break;
1152 }
1153 return Result;
1154}
1155
Nate Begemandffcfcc2005-04-01 00:32:34 +00001156/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001157bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001158{
Nate Begeman96fc6812005-03-31 02:05:53 +00001159 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001160 if (N.getOpcode() == ISD::ADD) {
1161 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001162 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001163 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001164 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001165 }
Nate Begeman04730362005-04-01 04:45:11 +00001166 offset = SelectExpr(N.getOperand(1));
1167 return true;
1168 }
Nate Begemana9795f82005-03-24 04:41:43 +00001169 Reg = SelectExpr(N);
1170 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001171 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001172}
1173
1174void ISel::SelectBranchCC(SDOperand N)
1175{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001176 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001177 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001178
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001179 bool Inv;
1180 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001181 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001182 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001183
Nate Begemancd08e4c2005-04-09 20:09:12 +00001184 // Iterate to the next basic block, unless we're already at the end of the
1185 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001186 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001187
1188 // If this is a two way branch, then grab the fallthrough basic block argument
1189 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1190 // if necessary by the branch selection pass. Otherwise, emit a standard
1191 // conditional branch.
1192 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001193 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001194 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1195 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001196 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001197 .addMBB(Dest).addMBB(Fallthrough);
1198 if (Fallthrough != It)
1199 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1200 } else {
1201 if (Fallthrough != It) {
1202 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001203 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001204 .addMBB(Fallthrough).addMBB(Dest);
1205 }
1206 }
1207 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001208 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001209 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001210 }
Nate Begemana9795f82005-03-24 04:41:43 +00001211 return;
1212}
1213
1214unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1215{
1216 unsigned Tmp1, Tmp2, Tmp3;
1217 unsigned Opc = 0;
1218 SDNode *Node = N.Val;
1219 MVT::ValueType DestType = N.getValueType();
1220 unsigned opcode = N.getOpcode();
1221
1222 switch (opcode) {
1223 default:
1224 Node->dump();
1225 assert(0 && "Node not handled!\n");
1226
Nate Begeman23afcfb2005-03-29 22:48:55 +00001227 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001228 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1229 // and an FP comparison in the SetCC node.
1230 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1231 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1232 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1233 SetCC->getCondition() != ISD::SETEQ &&
1234 SetCC->getCondition() != ISD::SETNE) {
1235 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001236 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1237 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001238
Nate Begeman3e897162005-03-31 23:55:40 +00001239 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1240 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1241 switch(SetCC->getCondition()) {
1242 default: assert(0 && "Invalid FSEL condition"); abort();
1243 case ISD::SETULT:
1244 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001245 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001246 case ISD::SETUGE:
1247 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001248 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001249 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1250 return Result;
1251 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001252 case ISD::SETGT:
1253 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001254 case ISD::SETULE:
1255 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001256 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1257 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1258 } else {
1259 Tmp2 = MakeReg(VT);
1260 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1261 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1262 }
Nate Begeman3e897162005-03-31 23:55:40 +00001263 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1264 return Result;
1265 }
1266 }
1267 } else {
1268 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001269 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001270 Tmp2 = SelectExpr(SetCC->getOperand(1));
1271 Tmp3 = MakeReg(VT);
1272 switch(SetCC->getCondition()) {
1273 default: assert(0 && "Invalid FSEL condition"); abort();
1274 case ISD::SETULT:
1275 case ISD::SETLT:
1276 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1277 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1278 return Result;
1279 case ISD::SETUGE:
1280 case ISD::SETGE:
1281 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1282 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1283 return Result;
1284 case ISD::SETUGT:
1285 case ISD::SETGT:
1286 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1287 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1288 return Result;
1289 case ISD::SETULE:
1290 case ISD::SETLE:
1291 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1292 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1293 return Result;
1294 }
1295 }
1296 assert(0 && "Should never get here");
1297 return 0;
1298 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001299
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001300 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001301 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1302 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001303 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001304
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001305 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001306 // value and the MBB to hold the PHI instruction for this SetCC.
1307 MachineBasicBlock *thisMBB = BB;
1308 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1309 ilist<MachineBasicBlock>::iterator It = BB;
1310 ++It;
1311
1312 // thisMBB:
1313 // ...
1314 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001315 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001316 // bCC copy1MBB
1317 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001318 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1319 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001320 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001321 MachineFunction *F = BB->getParent();
1322 F->getBasicBlockList().insert(It, copy0MBB);
1323 F->getBasicBlockList().insert(It, sinkMBB);
1324 // Update machine-CFG edges
1325 BB->addSuccessor(copy0MBB);
1326 BB->addSuccessor(sinkMBB);
1327
1328 // copy0MBB:
1329 // %FalseValue = ...
1330 // # fallthrough to sinkMBB
1331 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001332 // Update machine-CFG edges
1333 BB->addSuccessor(sinkMBB);
1334
1335 // sinkMBB:
1336 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1337 // ...
1338 BB = sinkMBB;
1339 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1340 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1341 return Result;
1342 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001343
1344 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001345 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001346 ISD::ADD == N.getOperand(0).getOpcode() &&
1347 N.getOperand(0).Val->hasOneUse() &&
1348 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1349 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001350 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001351 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1352 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1353 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1354 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1355 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001356 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001357 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001358 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001359 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1360 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001361 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001362 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1363 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1364 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1365 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001366 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1367 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001368 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1369 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1370 } else {
1371 Tmp1 = SelectExpr(N.getOperand(0));
1372 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1373 }
1374 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001375
Nate Begeman27eeb002005-04-02 05:59:34 +00001376 case ISD::FABS:
1377 Tmp1 = SelectExpr(N.getOperand(0));
1378 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1379 return Result;
1380
Nate Begemana9795f82005-03-24 04:41:43 +00001381 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001382 assert (DestType == MVT::f32 &&
1383 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001384 "only f64 to f32 conversion supported here");
1385 Tmp1 = SelectExpr(N.getOperand(0));
1386 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1387 return Result;
1388
1389 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001390 assert (DestType == MVT::f64 &&
1391 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001392 "only f32 to f64 conversion supported here");
1393 Tmp1 = SelectExpr(N.getOperand(0));
1394 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1395 return Result;
1396
1397 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001398 if (Result == 1)
1399 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1400 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1401 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1402 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001403
Nate Begeman6d369cc2005-04-01 01:08:07 +00001404 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001405 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001406 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001407 return Result;
1408 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001409
Nate Begemana9795f82005-03-24 04:41:43 +00001410 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001411 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1412 N.getOperand(0).Val->hasOneUse()) {
1413 ++FusedFP; // Statistic
1414 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1415 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1416 Tmp3 = SelectExpr(N.getOperand(1));
1417 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1418 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1419 return Result;
1420 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001421 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1422 N.getOperand(1).Val->hasOneUse()) {
1423 ++FusedFP; // Statistic
1424 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1425 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1426 Tmp3 = SelectExpr(N.getOperand(0));
1427 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1428 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1429 return Result;
1430 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001431 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1432 Tmp1 = SelectExpr(N.getOperand(0));
1433 Tmp2 = SelectExpr(N.getOperand(1));
1434 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1435 return Result;
1436
Nate Begemana9795f82005-03-24 04:41:43 +00001437 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001438 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1439 N.getOperand(0).Val->hasOneUse()) {
1440 ++FusedFP; // Statistic
1441 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1442 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1443 Tmp3 = SelectExpr(N.getOperand(1));
1444 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1445 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1446 return Result;
1447 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001448 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1449 N.getOperand(1).Val->hasOneUse()) {
1450 ++FusedFP; // Statistic
1451 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1452 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1453 Tmp3 = SelectExpr(N.getOperand(0));
1454 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1455 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1456 return Result;
1457 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001458 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1459 Tmp1 = SelectExpr(N.getOperand(0));
1460 Tmp2 = SelectExpr(N.getOperand(1));
1461 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1462 return Result;
1463
1464 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001465 case ISD::SDIV:
1466 switch( opcode ) {
1467 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001468 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1469 };
Nate Begemana9795f82005-03-24 04:41:43 +00001470 Tmp1 = SelectExpr(N.getOperand(0));
1471 Tmp2 = SelectExpr(N.getOperand(1));
1472 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1473 return Result;
1474
Nate Begemana9795f82005-03-24 04:41:43 +00001475 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001476 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001477 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001478 && "int to float must operate on i32");
1479 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1480 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1481 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1482 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001483
Nate Begemanfdcf3412005-03-30 19:38:35 +00001484 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1485 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001486
Nate Begemanfdcf3412005-03-30 19:38:35 +00001487 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001488 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001489 // Store the hi & low halves of the fp value, currently in int regs
1490 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1491 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1492 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1493 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1494 // Generate the return value with a subtract
1495 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1496 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001497 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001498 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001499 // Store the hi & low halves of the fp value, currently in int regs
1500 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1501 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1502 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1503 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1504 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1505 // Generate the return value with a subtract
1506 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1507 }
1508 return Result;
1509 }
Nate Begemana9795f82005-03-24 04:41:43 +00001510 }
Nate Begeman6b559972005-04-01 02:59:27 +00001511 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001512 return 0;
1513}
1514
Nate Begemanc7bd4822005-04-11 06:34:10 +00001515unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001516 unsigned Result;
1517 unsigned Tmp1, Tmp2, Tmp3;
1518 unsigned Opc = 0;
1519 unsigned opcode = N.getOpcode();
1520
1521 SDNode *Node = N.Val;
1522 MVT::ValueType DestType = N.getValueType();
1523
1524 unsigned &Reg = ExprMap[N];
1525 if (Reg) return Reg;
1526
Nate Begeman27eeb002005-04-02 05:59:34 +00001527 switch (N.getOpcode()) {
1528 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001529 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001530 MakeReg(N.getValueType()) : 1;
1531 break;
1532 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001533 // If this is a call instruction, make sure to prepare ALL of the result
1534 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001535 if (Node->getNumValues() == 1)
1536 Reg = Result = 1; // Void call, just a chain.
1537 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001538 Result = MakeReg(Node->getValueType(0));
1539 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001540 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001541 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001542 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001543 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001544 break;
1545 case ISD::ADD_PARTS:
1546 case ISD::SUB_PARTS:
1547 case ISD::SHL_PARTS:
1548 case ISD::SRL_PARTS:
1549 case ISD::SRA_PARTS:
1550 Result = MakeReg(Node->getValueType(0));
1551 ExprMap[N.getValue(0)] = Result;
1552 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1553 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1554 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001555 }
1556
Nate Begemane5846682005-04-04 06:52:38 +00001557 if (ISD::CopyFromReg == opcode)
1558 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001559
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001560 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001561 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Nate Begemana0e3e942005-04-10 01:14:13 +00001562 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001563 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001564
1565 switch (opcode) {
1566 default:
1567 Node->dump();
1568 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001569 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001570 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1571 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001572 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001573 // Generate both result values. FIXME: Need a better commment here?
1574 if (Result != 1)
1575 ExprMap[N.getValue(1)] = 1;
1576 else
1577 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1578
1579 // FIXME: We are currently ignoring the requested alignment for handling
1580 // greater than the stack alignment. This will need to be revisited at some
1581 // point. Align = N.getOperand(2);
1582 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1583 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1584 std::cerr << "Cannot allocate stack object with greater alignment than"
1585 << " the stack alignment yet!";
1586 abort();
1587 }
1588 Select(N.getOperand(0));
1589 Tmp1 = SelectExpr(N.getOperand(1));
1590 // Subtract size from stack pointer, thereby allocating some space.
1591 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1592 // Put a pointer to the space into the result register by copying the SP
1593 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1594 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001595
1596 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001597 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1598 Tmp2 = MakeReg(MVT::i32);
1599 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1600 .addConstantPoolIndex(Tmp1);
1601 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1602 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001603
1604 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001605 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001606 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001607 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001608
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001609 case ISD::GlobalAddress: {
1610 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001611 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001612 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1613 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001614 if (GV->hasWeakLinkage() || GV->isExternal()) {
1615 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1616 } else {
1617 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1618 }
1619 return Result;
1620 }
1621
Nate Begeman5e966612005-03-24 06:28:42 +00001622 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001623 case ISD::EXTLOAD:
1624 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001625 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001626 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1627 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001628 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001629
Nate Begeman5e966612005-03-24 06:28:42 +00001630 // Make sure we generate both values.
1631 if (Result != 1)
1632 ExprMap[N.getValue(1)] = 1; // Generate the token
1633 else
1634 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1635
1636 SDOperand Chain = N.getOperand(0);
1637 SDOperand Address = N.getOperand(1);
1638 Select(Chain);
1639
Nate Begeman9db505c2005-03-28 19:36:43 +00001640 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001641 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001642 case MVT::i1: Opc = PPC::LBZ; break;
1643 case MVT::i8: Opc = PPC::LBZ; break;
1644 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1645 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001646 case MVT::f32: Opc = PPC::LFS; break;
1647 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001648 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001649
Nate Begeman74d73452005-03-31 00:15:26 +00001650 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1651 Tmp1 = MakeReg(MVT::i32);
1652 int CPI = CP->getIndex();
1653 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1654 .addConstantPoolIndex(CPI);
1655 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001656 }
Nate Begeman74d73452005-03-31 00:15:26 +00001657 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001658 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1659 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001660 } else {
1661 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001662 bool idx = SelectAddr(Address, Tmp1, offset);
1663 if (idx) {
1664 Opc = IndexedOpForOp(Opc);
1665 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1666 } else {
1667 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1668 }
Nate Begeman5e966612005-03-24 06:28:42 +00001669 }
1670 return Result;
1671 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001672
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001673 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001674 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001675 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001676 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1677 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1678 };
1679 static const unsigned FPR[] = {
1680 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1681 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1682 };
1683
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001684 // Lower the chain for this call.
1685 Select(N.getOperand(0));
1686 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001687
Nate Begemand860aa62005-04-04 22:17:48 +00001688 MachineInstr *CallMI;
1689 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001690 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001691 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001692 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001693 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001694 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001695 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001696 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001697 true);
1698 } else {
1699 Tmp1 = SelectExpr(N.getOperand(1));
1700 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1701 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1702 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1703 .addReg(PPC::R12);
1704 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001705
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001706 // Load the register args to virtual regs
1707 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001708 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001709 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1710
1711 // Copy the virtual registers into the appropriate argument register
1712 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1713 switch(N.getOperand(i+2).getValueType()) {
1714 default: Node->dump(); assert(0 && "Unknown value type for call");
1715 case MVT::i1:
1716 case MVT::i8:
1717 case MVT::i16:
1718 case MVT::i32:
1719 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001720 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001721 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001722 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1723 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001724 ++GPR_idx;
1725 break;
1726 case MVT::f64:
1727 case MVT::f32:
1728 assert(FPR_idx < 13 && "Too many fp args");
1729 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001730 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001731 ++FPR_idx;
1732 break;
1733 }
1734 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001735
Nate Begemand860aa62005-04-04 22:17:48 +00001736 // Put the call instruction in the correct place in the MachineBasicBlock
1737 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001738
1739 switch (Node->getValueType(0)) {
1740 default: assert(0 && "Unknown value type for call result!");
1741 case MVT::Other: return 1;
1742 case MVT::i1:
1743 case MVT::i8:
1744 case MVT::i16:
1745 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001746 if (Node->getValueType(1) == MVT::i32) {
1747 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1748 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1749 } else {
1750 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1751 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001752 break;
1753 case MVT::f32:
1754 case MVT::f64:
1755 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1756 break;
1757 }
1758 return Result+N.ResNo;
1759 }
Nate Begemana9795f82005-03-24 04:41:43 +00001760
1761 case ISD::SIGN_EXTEND:
1762 case ISD::SIGN_EXTEND_INREG:
1763 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001764 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1765 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001766 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001767 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001768 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001769 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001770 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001771 break;
Nate Begeman74747862005-03-29 22:24:51 +00001772 case MVT::i1:
1773 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1774 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001775 }
Nate Begemana9795f82005-03-24 04:41:43 +00001776 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001777
Nate Begemana9795f82005-03-24 04:41:43 +00001778 case ISD::CopyFromReg:
1779 if (Result == 1)
1780 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1781 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1782 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1783 return Result;
1784
1785 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001786 Tmp1 = SelectExpr(N.getOperand(0));
1787 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1788 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001789 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001790 .addImm(31-Tmp2);
1791 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001792 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001793 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1794 }
1795 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001796
Nate Begeman5e966612005-03-24 06:28:42 +00001797 case ISD::SRL:
1798 Tmp1 = SelectExpr(N.getOperand(0));
1799 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1800 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001801 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001802 .addImm(Tmp2).addImm(31);
1803 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001804 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001805 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1806 }
1807 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001808
Nate Begeman5e966612005-03-24 06:28:42 +00001809 case ISD::SRA:
1810 Tmp1 = SelectExpr(N.getOperand(0));
1811 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1812 Tmp2 = CN->getValue() & 0x1F;
1813 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1814 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001815 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001816 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1817 }
1818 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001819
Nate Begemana9795f82005-03-24 04:41:43 +00001820 case ISD::ADD:
1821 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1822 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001823 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001824 default: assert(0 && "unhandled result code");
1825 case 0: // No immediate
1826 Tmp2 = SelectExpr(N.getOperand(1));
1827 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 break;
1829 case 1: // Low immediate
1830 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1831 break;
1832 case 2: // Shifted immediate
1833 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1834 break;
1835 }
1836 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001837
Nate Begemana9795f82005-03-24 04:41:43 +00001838 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001839 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001840 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001841 N.getOperand(1).getOpcode() == ISD::SETCC) {
1842 bool Inv;
1843 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1844 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1845 return Result;
1846 }
1847 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001848 // FIXME: should add check in getImmediateForOpcode to return a value
1849 // indicating the immediate is a run of set bits so we can emit a bitfield
1850 // clear with RLWINM instead.
1851 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1852 default: assert(0 && "unhandled result code");
1853 case 0: // No immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001854 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001855 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001856 Opc = Recording ? PPC::ANDo : PPC::AND;
1857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001858 break;
1859 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001860 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001861 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1862 break;
1863 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001864 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001865 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1866 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001867 case 5: // Bitfield mask
1868 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1869 Tmp3 = Tmp2 >> 16; // MB
1870 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001871
1872 if (N.getOperand(0).getOpcode() == ISD::SRL)
1873 if (ConstantSDNode *SA =
1874 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1875
1876 // We can fold the RLWINM and the SRL together if the mask is
1877 // clearing the top bits which are rotated around.
1878 unsigned RotAmt = 32-(SA->getValue() & 31);
1879 if (Tmp2 <= RotAmt) {
1880 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1881 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1882 .addImm(Tmp3).addImm(Tmp2);
1883 break;
1884 }
1885 }
1886
1887 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001888 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1889 .addImm(Tmp3).addImm(Tmp2);
1890 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001891 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001892 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001893 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001894
Nate Begemana9795f82005-03-24 04:41:43 +00001895 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001896 if (SelectBitfieldInsert(N, Result))
1897 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001898 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001899 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001900 N.getOperand(1).getOpcode() == ISD::SETCC) {
1901 bool Inv;
1902 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1903 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1904 return Result;
1905 }
1906 }
Nate Begemana9795f82005-03-24 04:41:43 +00001907 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001908 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001909 default: assert(0 && "unhandled result code");
1910 case 0: // No immediate
1911 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001912 Opc = Recording ? PPC::ORo : PPC::OR;
1913 RecordSuccess = true;
1914 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001915 break;
1916 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001917 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001918 break;
1919 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001920 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001921 break;
1922 }
1923 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001924
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001925 case ISD::XOR: {
1926 // Check for EQV: xor, (xor a, -1), b
1927 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1928 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1929 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001930 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1931 Tmp2 = SelectExpr(N.getOperand(1));
1932 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1933 return Result;
1934 }
Chris Lattner837a5212005-04-21 21:09:11 +00001935 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001936 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1937 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001938 switch(N.getOperand(0).getOpcode()) {
1939 case ISD::OR:
1940 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1941 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1942 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1943 break;
1944 case ISD::AND:
1945 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1946 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1947 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1948 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001949 case ISD::XOR:
1950 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1951 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1952 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1953 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001954 default:
1955 Tmp1 = SelectExpr(N.getOperand(0));
1956 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1957 break;
1958 }
1959 return Result;
1960 }
1961 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001962 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001963 default: assert(0 && "unhandled result code");
1964 case 0: // No immediate
1965 Tmp2 = SelectExpr(N.getOperand(1));
1966 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1967 break;
1968 case 1: // Low immediate
1969 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1970 break;
1971 case 2: // Shifted immediate
1972 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1973 break;
1974 }
1975 return Result;
1976 }
1977
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001978 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001979 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001980 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001981 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1982 else {
1983 Tmp1 = SelectExpr(N.getOperand(0));
1984 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1985 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001986 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001987
Nate Begeman5e966612005-03-24 06:28:42 +00001988 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001989 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001990 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001991 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1992 else {
1993 Tmp2 = SelectExpr(N.getOperand(1));
1994 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1995 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001996 return Result;
1997
Nate Begeman815d6da2005-04-06 00:25:27 +00001998 case ISD::MULHS:
1999 case ISD::MULHU:
2000 Tmp1 = SelectExpr(N.getOperand(0));
2001 Tmp2 = SelectExpr(N.getOperand(1));
2002 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
2003 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2004 return Result;
2005
Nate Begemanf3d08f32005-03-29 00:03:27 +00002006 case ISD::SDIV:
2007 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00002008 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
2009 default: break;
2010 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
2011 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00002012 Tmp1 = MakeReg(MVT::i32);
2013 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00002014 if ((int)Tmp3 < 0) {
2015 unsigned Tmp4 = MakeReg(MVT::i32);
2016 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
2017 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
2018 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
2019 } else {
2020 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
2021 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
2022 }
Nate Begeman80196b12005-04-05 00:15:08 +00002023 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00002024 // If this is a divide by constant, we can emit code using some magic
2025 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00002026 case 4:
2027 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002028 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00002029 return SelectExpr(BuildSDIVSequence(N));
2030 else
2031 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002032 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002033 Tmp1 = SelectExpr(N.getOperand(0));
2034 Tmp2 = SelectExpr(N.getOperand(1));
2035 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2036 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2037 return Result;
2038
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002039 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002040 case ISD::SUB_PARTS: {
2041 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2042 "Not an i64 add/sub!");
2043 // Emit all of the operands.
2044 std::vector<unsigned> InVals;
2045 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2046 InVals.push_back(SelectExpr(N.getOperand(i)));
2047 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002048 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2049 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002050 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002051 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2052 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2053 }
2054 return Result+N.ResNo;
2055 }
2056
2057 case ISD::SHL_PARTS:
2058 case ISD::SRA_PARTS:
2059 case ISD::SRL_PARTS: {
2060 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2061 "Not an i64 shift!");
2062 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2063 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002064 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2065 Tmp1 = MakeReg(MVT::i32);
2066 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002067 Tmp3 = MakeReg(MVT::i32);
2068 unsigned Tmp4 = MakeReg(MVT::i32);
2069 unsigned Tmp5 = MakeReg(MVT::i32);
2070 unsigned Tmp6 = MakeReg(MVT::i32);
2071 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2072 if (ISD::SHL_PARTS == opcode) {
2073 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2074 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2075 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2076 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002077 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002078 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2079 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2080 } else if (ISD::SRL_PARTS == opcode) {
2081 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2082 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2083 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2084 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2085 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2086 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2087 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2088 } else {
2089 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2090 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2091 MachineBasicBlock *OldMBB = BB;
2092 MachineFunction *F = BB->getParent();
2093 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2094 F->getBasicBlockList().insert(It, TmpMBB);
2095 F->getBasicBlockList().insert(It, PhiMBB);
2096 BB->addSuccessor(TmpMBB);
2097 BB->addSuccessor(PhiMBB);
2098 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2099 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2100 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2101 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2102 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2103 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2104 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2105 // Select correct least significant half if the shift amount > 32
2106 BB = TmpMBB;
2107 unsigned Tmp7 = MakeReg(MVT::i32);
2108 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2109 TmpMBB->addSuccessor(PhiMBB);
2110 BB = PhiMBB;
2111 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2112 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002113 }
2114 return Result+N.ResNo;
2115 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002116
Nate Begemana9795f82005-03-24 04:41:43 +00002117 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002118 case ISD::FP_TO_SINT: {
2119 bool U = (ISD::FP_TO_UINT == opcode);
2120 Tmp1 = SelectExpr(N.getOperand(0));
2121 if (!U) {
2122 Tmp2 = MakeReg(MVT::f64);
2123 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2124 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2125 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2126 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2127 return Result;
2128 } else {
2129 unsigned Zero = getConstDouble(0.0);
2130 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2131 unsigned Border = getConstDouble(1LL << 31);
2132 unsigned UseZero = MakeReg(MVT::f64);
2133 unsigned UseMaxInt = MakeReg(MVT::f64);
2134 unsigned UseChoice = MakeReg(MVT::f64);
2135 unsigned TmpReg = MakeReg(MVT::f64);
2136 unsigned TmpReg2 = MakeReg(MVT::f64);
2137 unsigned ConvReg = MakeReg(MVT::f64);
2138 unsigned IntTmp = MakeReg(MVT::i32);
2139 unsigned XorReg = MakeReg(MVT::i32);
2140 MachineFunction *F = BB->getParent();
2141 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2142 // Update machine-CFG edges
2143 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2144 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2145 MachineBasicBlock *OldMBB = BB;
2146 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2147 F->getBasicBlockList().insert(It, XorMBB);
2148 F->getBasicBlockList().insert(It, PhiMBB);
2149 BB->addSuccessor(XorMBB);
2150 BB->addSuccessor(PhiMBB);
2151 // Convert from floating point to unsigned 32-bit value
2152 // Use 0 if incoming value is < 0.0
2153 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2154 // Use 2**32 - 1 if incoming value is >= 2**32
2155 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2156 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2157 .addReg(MaxInt);
2158 // Subtract 2**31
2159 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2160 // Use difference if >= 2**31
2161 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2162 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2163 .addReg(UseChoice);
2164 // Convert to integer
2165 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2166 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2167 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2168 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2169 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2170
2171 // XorMBB:
2172 // add 2**31 if input was >= 2**31
2173 BB = XorMBB;
2174 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2175 XorMBB->addSuccessor(PhiMBB);
2176
2177 // PhiMBB:
2178 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2179 BB = PhiMBB;
2180 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2181 .addReg(XorReg).addMBB(XorMBB);
2182 return Result;
2183 }
2184 assert(0 && "Should never get here");
2185 return 0;
2186 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002187
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002188 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002189 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002190 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002191 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002192 // We can codegen setcc op, imm very efficiently compared to a brcond.
2193 // Check for those cases here.
2194 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002195 if (CN->getValue() == 0) {
2196 Tmp1 = SelectExpr(SetCC->getOperand(0));
2197 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002198 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002199 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002200 Tmp2 = MakeReg(MVT::i32);
2201 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2202 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2203 .addImm(5).addImm(31);
2204 break;
2205 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002206 Tmp2 = MakeReg(MVT::i32);
2207 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2208 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2209 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002210 case ISD::SETLT:
2211 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2212 .addImm(31).addImm(31);
2213 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002214 case ISD::SETGT:
2215 Tmp2 = MakeReg(MVT::i32);
2216 Tmp3 = MakeReg(MVT::i32);
2217 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2218 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2219 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2220 .addImm(31).addImm(31);
2221 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002222 }
2223 return Result;
2224 }
2225 // setcc op, -1
2226 if (CN->isAllOnesValue()) {
2227 Tmp1 = SelectExpr(SetCC->getOperand(0));
2228 switch (SetCC->getCondition()) {
2229 default: assert(0 && "Unhandled SetCC condition"); abort();
2230 case ISD::SETEQ:
2231 Tmp2 = MakeReg(MVT::i32);
2232 Tmp3 = MakeReg(MVT::i32);
2233 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2234 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2235 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002236 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002237 case ISD::SETNE:
2238 Tmp2 = MakeReg(MVT::i32);
2239 Tmp3 = MakeReg(MVT::i32);
2240 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2241 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2242 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2243 break;
2244 case ISD::SETLT:
2245 Tmp2 = MakeReg(MVT::i32);
2246 Tmp3 = MakeReg(MVT::i32);
2247 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2248 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2249 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2250 .addImm(31).addImm(31);
2251 break;
2252 case ISD::SETGT:
2253 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002254 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2255 .addImm(31).addImm(31);
2256 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2257 break;
2258 }
2259 return Result;
2260 }
2261 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002262
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002263 bool Inv;
2264 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2265 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002266 return Result;
2267 }
2268 assert(0 && "Is this legal?");
2269 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002270
Nate Begeman74747862005-03-29 22:24:51 +00002271 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002272 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002273 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2274 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002275 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002276
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002277 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002278 // value and the MBB to hold the PHI instruction for this SetCC.
2279 MachineBasicBlock *thisMBB = BB;
2280 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2281 ilist<MachineBasicBlock>::iterator It = BB;
2282 ++It;
2283
2284 // thisMBB:
2285 // ...
2286 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002287 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002288 // bCC copy1MBB
2289 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002290 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2291 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002292 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002293 MachineFunction *F = BB->getParent();
2294 F->getBasicBlockList().insert(It, copy0MBB);
2295 F->getBasicBlockList().insert(It, sinkMBB);
2296 // Update machine-CFG edges
2297 BB->addSuccessor(copy0MBB);
2298 BB->addSuccessor(sinkMBB);
2299
2300 // copy0MBB:
2301 // %FalseValue = ...
2302 // # fallthrough to sinkMBB
2303 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002304 // Update machine-CFG edges
2305 BB->addSuccessor(sinkMBB);
2306
2307 // sinkMBB:
2308 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2309 // ...
2310 BB = sinkMBB;
2311 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2312 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002313 return Result;
2314 }
Nate Begemana9795f82005-03-24 04:41:43 +00002315
2316 case ISD::Constant:
2317 switch (N.getValueType()) {
2318 default: assert(0 && "Cannot use constants of this type!");
2319 case MVT::i1:
2320 BuildMI(BB, PPC::LI, 1, Result)
2321 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2322 break;
2323 case MVT::i32:
2324 {
2325 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2326 if (v < 32768 && v >= -32768) {
2327 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2328 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002329 Tmp1 = MakeReg(MVT::i32);
2330 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2331 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002332 }
2333 }
2334 }
2335 return Result;
2336 }
2337
2338 return 0;
2339}
2340
2341void ISel::Select(SDOperand N) {
2342 unsigned Tmp1, Tmp2, Opc;
2343 unsigned opcode = N.getOpcode();
2344
2345 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2346 return; // Already selected.
2347
2348 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002349
Nate Begemana9795f82005-03-24 04:41:43 +00002350 switch (Node->getOpcode()) {
2351 default:
2352 Node->dump(); std::cerr << "\n";
2353 assert(0 && "Node not handled yet!");
2354 case ISD::EntryToken: return; // Noop
2355 case ISD::TokenFactor:
2356 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2357 Select(Node->getOperand(i));
2358 return;
2359 case ISD::ADJCALLSTACKDOWN:
2360 case ISD::ADJCALLSTACKUP:
2361 Select(N.getOperand(0));
2362 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2363 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2364 PPC::ADJCALLSTACKUP;
2365 BuildMI(BB, Opc, 1).addImm(Tmp1);
2366 return;
2367 case ISD::BR: {
2368 MachineBasicBlock *Dest =
2369 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002370 Select(N.getOperand(0));
2371 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2372 return;
2373 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002374 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002375 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002376 SelectBranchCC(N);
2377 return;
2378 case ISD::CopyToReg:
2379 Select(N.getOperand(0));
2380 Tmp1 = SelectExpr(N.getOperand(1));
2381 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002382
Nate Begemana9795f82005-03-24 04:41:43 +00002383 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002384 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002385 N.getOperand(1).getValueType() == MVT::f32)
2386 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2387 else
2388 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2389 }
2390 return;
2391 case ISD::ImplicitDef:
2392 Select(N.getOperand(0));
2393 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2394 return;
2395 case ISD::RET:
2396 switch (N.getNumOperands()) {
2397 default:
2398 assert(0 && "Unknown return instruction!");
2399 case 3:
2400 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2401 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002402 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002403 Select(N.getOperand(0));
2404 Tmp1 = SelectExpr(N.getOperand(1));
2405 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002406 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2407 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002408 break;
2409 case 2:
2410 Select(N.getOperand(0));
2411 Tmp1 = SelectExpr(N.getOperand(1));
2412 switch (N.getOperand(1).getValueType()) {
2413 default:
2414 assert(0 && "Unknown return type!");
2415 case MVT::f64:
2416 case MVT::f32:
2417 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2418 break;
2419 case MVT::i32:
2420 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2421 break;
2422 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002423 case 1:
2424 Select(N.getOperand(0));
2425 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002426 }
2427 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2428 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002429 case ISD::TRUNCSTORE:
2430 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002431 {
2432 SDOperand Chain = N.getOperand(0);
2433 SDOperand Value = N.getOperand(1);
2434 SDOperand Address = N.getOperand(2);
2435 Select(Chain);
2436
2437 Tmp1 = SelectExpr(Value); //value
2438
2439 if (opcode == ISD::STORE) {
2440 switch(Value.getValueType()) {
2441 default: assert(0 && "unknown Type in store");
2442 case MVT::i32: Opc = PPC::STW; break;
2443 case MVT::f64: Opc = PPC::STFD; break;
2444 case MVT::f32: Opc = PPC::STFS; break;
2445 }
2446 } else { //ISD::TRUNCSTORE
2447 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2448 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002449 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002450 case MVT::i8: Opc = PPC::STB; break;
2451 case MVT::i16: Opc = PPC::STH; break;
2452 }
2453 }
2454
Nate Begemana7e11a42005-04-01 05:57:17 +00002455 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002456 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002457 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2458 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002459 }
2460 else
2461 {
2462 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002463 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002464 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002465 Opc = IndexedOpForOp(Opc);
2466 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2467 } else {
2468 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2469 }
Nate Begemana9795f82005-03-24 04:41:43 +00002470 }
2471 return;
2472 }
2473 case ISD::EXTLOAD:
2474 case ISD::SEXTLOAD:
2475 case ISD::ZEXTLOAD:
2476 case ISD::LOAD:
2477 case ISD::CopyFromReg:
2478 case ISD::CALL:
2479 case ISD::DYNAMIC_STACKALLOC:
2480 ExprMap.erase(N);
2481 SelectExpr(N);
2482 return;
2483 }
2484 assert(0 && "Should not be reached!");
2485}
2486
2487
2488/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2489/// into a machine code representation using pattern matching and a machine
2490/// description file.
2491///
2492FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002493 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002494}
2495