blob: 8f3122ef8144cb8ae6c339fb626839fd4a6dd852 [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) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000046 // Fold away setcc operations if possible.
47 setSetCCIsExpensive();
48
Nate Begemana9795f82005-03-24 04:41:43 +000049 // Set up the register classes.
50 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000051 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000052 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000053
Nate Begeman74d73452005-03-31 00:15:26 +000054 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000055 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
56 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
57 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
58
Nate Begeman74d73452005-03-31 00:15:26 +000059 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
60 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
61 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000062
Nate Begeman815d6da2005-04-06 00:25:27 +000063 // PowerPC has no SREM/UREM instructions
64 setOperationAction(ISD::SREM, MVT::i32, Expand);
65 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000066
Chris Lattner32f3cf62005-05-13 16:20:22 +000067 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000068 setOperationAction(ISD::FSIN , MVT::f64, Expand);
69 setOperationAction(ISD::FCOS , MVT::f64, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000071 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000072 setOperationAction(ISD::FSIN , MVT::f32, Expand);
73 setOperationAction(ISD::FCOS , MVT::f32, Expand);
74 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000075 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000076
Nate Begemand7c4a4a2005-05-11 23:43:56 +000077 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000078 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
79 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000080
Chris Lattnercbd06fc2005-04-07 19:41:49 +000081 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000082 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000084
Nate Begemana9795f82005-03-24 04:41:43 +000085 computeRegisterProperties();
86 }
87
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092
Nate Begemana9795f82005-03-24 04:41:43 +000093 /// LowerCallTo - This hook lowers an abstract call to a function into an
94 /// actual call.
95 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +000096 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +000097 bool isTailCall, SDOperand Callee, ArgListTy &Args,
98 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Nate Begemana9795f82005-03-24 04:41:43 +0000100 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc1671e22005-07-01 23:11:56 +0000101 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000102
Nate Begemana9795f82005-03-24 04:41:43 +0000103 virtual std::pair<SDOperand,SDOperand>
Chris Lattnerc1671e22005-07-01 23:11:56 +0000104 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Nate Begemana9795f82005-03-24 04:41:43 +0000105 const Type *ArgTy, SelectionDAG &DAG);
106
107 virtual std::pair<SDOperand, SDOperand>
108 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
109 SelectionDAG &DAG);
110 };
111}
112
113
114std::vector<SDOperand>
115PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
116 //
117 // add beautiful description of PPC stack frame format, or at least some docs
118 //
119 MachineFunction &MF = DAG.getMachineFunction();
120 MachineFrameInfo *MFI = MF.getFrameInfo();
121 MachineBasicBlock& BB = MF.front();
122 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000123
124 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000125 // fixed size array of physical args, for the sake of simplicity let the STL
126 // handle tracking them for us.
127 std::vector<unsigned> argVR, argPR, argOp;
128 unsigned ArgOffset = 24;
129 unsigned GPR_remaining = 8;
130 unsigned FPR_remaining = 13;
131 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
135 };
136 static const unsigned FPR[] = {
137 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
138 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
139 };
140
141 // Add DAG nodes to load the arguments... On entry to a function on PPC,
142 // the arguments start at offset 24, although they are likely to be passed
143 // in registers.
144 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
145 SDOperand newroot, argt;
146 unsigned ObjSize;
147 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000148 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000149 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000150
Nate Begemana9795f82005-03-24 04:41:43 +0000151 switch (ObjectVT) {
152 default: assert(0 && "Unhandled argument type!");
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000156 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000157 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000158 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000159 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000160 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
162 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000163 if (ObjectVT != MVT::i32)
164 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000165 } else {
166 needsLoad = true;
167 }
168 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000169 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000170 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000171 if (GPR_remaining > 0) {
172 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000173 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000174 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
175 // If we have two or more remaining argument registers, then both halves
176 // of the i64 can be sourced from there. Otherwise, the lower half will
177 // have to come off the stack. This can happen when an i64 is preceded
178 // by 28 bytes of arguments.
179 if (GPR_remaining > 1) {
180 MF.addLiveIn(GPR[GPR_idx+1]);
181 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
182 } else {
183 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
184 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000185 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
186 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000187 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000188 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000189 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
190 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000191 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000192 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000193 }
194 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000195 case MVT::f32:
196 case MVT::f64:
197 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
198 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000199 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000200 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000202 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000203 --FPR_remaining;
204 ++FPR_idx;
205 } else {
206 needsLoad = true;
207 }
208 break;
209 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000210
Nate Begemana9795f82005-03-24 04:41:43 +0000211 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000212 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000213 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000214 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000215 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000216 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000217 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
218 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000220 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000221 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
222 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000223 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224
Nate Begemana9795f82005-03-24 04:41:43 +0000225 // Every 4 bytes of argument space consumes one of the GPRs available for
226 // argument passing.
227 if (GPR_remaining > 0) {
228 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
229 GPR_remaining -= delta;
230 GPR_idx += delta;
231 }
232 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000233 if (newroot.Val)
234 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000235
Nate Begemana9795f82005-03-24 04:41:43 +0000236 ArgValues.push_back(argt);
237 }
238
Nate Begemana9795f82005-03-24 04:41:43 +0000239 // If the function takes variable number of arguments, make a frame index for
240 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000241 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000242 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000243 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000244 // If this function is vararg, store any remaining integer argument regs
245 // to their spots on the stack so that they may be loaded by deferencing the
246 // result of va_next.
247 std::vector<SDOperand> MemOps;
248 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000249 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000250 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000251 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000252 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000253 MemOps.push_back(Store);
254 // Increment the address by four for the next argument to store
255 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
256 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
257 }
258 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000259 }
Nate Begemana9795f82005-03-24 04:41:43 +0000260
Nate Begemancd08e4c2005-04-09 20:09:12 +0000261 // Finally, inform the code generator which regs we return values in.
262 switch (getValueType(F.getReturnType())) {
263 default: assert(0 && "Unknown type!");
264 case MVT::isVoid: break;
265 case MVT::i1:
266 case MVT::i8:
267 case MVT::i16:
268 case MVT::i32:
269 MF.addLiveOut(PPC::R3);
270 break;
271 case MVT::i64:
272 MF.addLiveOut(PPC::R3);
273 MF.addLiveOut(PPC::R4);
274 break;
275 case MVT::f32:
276 case MVT::f64:
277 MF.addLiveOut(PPC::F1);
278 break;
279 }
280
Nate Begemana9795f82005-03-24 04:41:43 +0000281 return ArgValues;
282}
283
284std::pair<SDOperand, SDOperand>
285PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000286 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000287 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000288 SDOperand Callee, ArgListTy &Args,
289 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000290 // args_to_use will accumulate outgoing args for the ISD::CALL case in
291 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000292 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000293
294 // Count how many bytes are to be pushed on the stack, including the linkage
295 // area, and parameter passing area.
296 unsigned NumBytes = 24;
297
298 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000299 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000300 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000301 } else {
302 for (unsigned i = 0, e = Args.size(); i != e; ++i)
303 switch (getValueType(Args[i].second)) {
304 default: assert(0 && "Unknown value type!");
305 case MVT::i1:
306 case MVT::i8:
307 case MVT::i16:
308 case MVT::i32:
309 case MVT::f32:
310 NumBytes += 4;
311 break;
312 case MVT::i64:
313 case MVT::f64:
314 NumBytes += 8;
315 break;
316 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000317
318 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000319 // plus 32 bytes of argument space in case any called code gets funky on us.
320 if (NumBytes < 56) NumBytes = 56;
321
322 // Adjust the stack pointer for the new arguments...
323 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000324 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000325 DAG.getConstant(NumBytes, getPointerTy()));
326
327 // Set up a copy of the stack pointer for use loading and storing any
328 // arguments that may not fit in the registers available for argument
329 // passing.
330 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
331 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000332
Nate Begeman307e7442005-03-26 01:28:53 +0000333 // Figure out which arguments are going to go in registers, and which in
334 // memory. Also, if this is a vararg function, floating point operations
335 // must be stored to our stack, and loaded into integer regs as well, if
336 // any integer regs are available for argument passing.
337 unsigned ArgOffset = 24;
338 unsigned GPR_remaining = 8;
339 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000340
Nate Begeman74d73452005-03-31 00:15:26 +0000341 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000342 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
343 // PtrOff will be used to store the current argument to the stack if a
344 // register cannot be found for it.
345 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
346 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000347 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000348
Nate Begemanf7e43382005-03-26 07:46:36 +0000349 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000350 default: assert(0 && "Unexpected ValueType for argument!");
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 // Promote the integer to 32 bits. If the input type is signed use a
355 // sign extend, otherwise use a zero extend.
356 if (Args[i].second->isSigned())
357 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
358 else
359 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
360 // FALL THROUGH
361 case MVT::i32:
362 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000363 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000364 --GPR_remaining;
365 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000367 Args[i].first, PtrOff,
368 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000369 }
370 ArgOffset += 4;
371 break;
372 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000373 // If we have one free GPR left, we can place the upper half of the i64
374 // in it, and store the other half to the stack. If we have two or more
375 // free GPRs, then we can pass both halves of the i64 in registers.
376 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000377 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000378 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000379 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000380 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000381 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000382 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000383 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000384 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000386 } else {
387 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
388 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000389 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000390 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000391 }
Nate Begeman307e7442005-03-26 01:28:53 +0000392 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000394 Args[i].first, PtrOff,
395 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000396 }
397 ArgOffset += 8;
398 break;
399 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000400 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000401 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000402 args_to_use.push_back(Args[i].first);
403 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000404 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000405 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000406 Args[i].first, PtrOff,
407 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000408 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000409 // Float varargs are always shadowed in available integer registers
410 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000411 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
412 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000414 args_to_use.push_back(Load);
415 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000416 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000417 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000418 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
419 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000420 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
421 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000422 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000423 args_to_use.push_back(Load);
424 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000425 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000426 } else {
427 // If we have any FPRs remaining, we may also have GPRs remaining.
428 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
429 // GPRs.
430 if (GPR_remaining > 0) {
431 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
432 --GPR_remaining;
433 }
434 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
435 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
436 --GPR_remaining;
437 }
Nate Begeman74d73452005-03-31 00:15:26 +0000438 }
Nate Begeman307e7442005-03-26 01:28:53 +0000439 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000440 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000441 Args[i].first, PtrOff,
442 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000443 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000444 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000445 break;
446 }
Nate Begemana9795f82005-03-24 04:41:43 +0000447 }
Nate Begeman74d73452005-03-31 00:15:26 +0000448 if (!MemOps.empty())
449 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000450 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000451
Nate Begemana9795f82005-03-24 04:41:43 +0000452 std::vector<MVT::ValueType> RetVals;
453 MVT::ValueType RetTyVT = getValueType(RetTy);
454 if (RetTyVT != MVT::isVoid)
455 RetVals.push_back(RetTyVT);
456 RetVals.push_back(MVT::Other);
457
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000458 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000459 Chain, Callee, args_to_use), 0);
460 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000461 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000462 DAG.getConstant(NumBytes, getPointerTy()));
463 return std::make_pair(TheCall, Chain);
464}
465
466std::pair<SDOperand, SDOperand>
Chris Lattnerc1671e22005-07-01 23:11:56 +0000467PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
Nate Begemana9795f82005-03-24 04:41:43 +0000468 //vastart just returns the address of the VarArgsFrameIndex slot.
469 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
470}
471
472std::pair<SDOperand,SDOperand> PPC32TargetLowering::
Chris Lattnerc1671e22005-07-01 23:11:56 +0000473LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Nate Begemana9795f82005-03-24 04:41:43 +0000474 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattnerc1671e22005-07-01 23:11:56 +0000475 // FIXME: THIS IS BROKEN!!!
476
477 bool isVANext = true;
478
479
Nate Begemanc7b09f12005-03-25 08:34:25 +0000480 MVT::ValueType ArgVT = getValueType(ArgTy);
481 SDOperand Result;
482 if (!isVANext) {
Chris Lattner022ed322005-05-15 19:54:37 +0000483 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
484 DAG.getSrcValue(NULL));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000485 } else {
486 unsigned Amt;
487 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
488 Amt = 4;
489 else {
490 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
491 "Other types should have been promoted for varargs!");
492 Amt = 8;
493 }
494 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
495 DAG.getConstant(Amt, VAList.getValueType()));
496 }
497 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000498}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000499
Nate Begemana9795f82005-03-24 04:41:43 +0000500
501std::pair<SDOperand, SDOperand> PPC32TargetLowering::
502LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
503 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000504 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000505 abort();
506}
507
508namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000509Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000510Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000511Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000512//===--------------------------------------------------------------------===//
513/// ISel - PPC32 specific code to select PPC32 machine instructions for
514/// SelectionDAG operations.
515//===--------------------------------------------------------------------===//
516class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000517 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000518 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
519 // for sdiv and udiv until it is put into the future
520 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000521
Nate Begemana9795f82005-03-24 04:41:43 +0000522 /// ExprMap - As shared expressions are codegen'd, we keep track of which
523 /// vreg the value is produced in, so we only emit one copy of each compiled
524 /// tree.
525 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000526
527 unsigned GlobalBaseReg;
528 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000529 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000530public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000531 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
532 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000533
Nate Begemanc7b09f12005-03-25 08:34:25 +0000534 /// runOnFunction - Override this function in order to reset our per-function
535 /// variables.
536 virtual bool runOnFunction(Function &Fn) {
537 // Make sure we re-emit a set of the global base reg if necessary
538 GlobalBaseInitialized = false;
539 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000540 }
541
Nate Begemana9795f82005-03-24 04:41:43 +0000542 /// InstructionSelectBasicBlock - This callback is invoked by
543 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
544 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
545 DEBUG(BB->dump());
546 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000547 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000548 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000549
Nate Begemana9795f82005-03-24 04:41:43 +0000550 // Clear state used for selection.
551 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000552 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000553 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000554
555 // dag -> dag expanders for integer divide by constant
556 SDOperand BuildSDIVSequence(SDOperand N);
557 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000558
Nate Begemandffcfcc2005-04-01 00:32:34 +0000559 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000560 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000561 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000562 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000563 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000564 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
565 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000566 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000567 unsigned SelectExprFP(SDOperand N, unsigned Result);
568 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000569
Nate Begeman04730362005-04-01 04:45:11 +0000570 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000571 void SelectBranchCC(SDOperand N);
572};
573
Nate Begeman80196b12005-04-05 00:15:08 +0000574/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
575/// returns zero when the input is not exactly a power of two.
576static unsigned ExactLog2(unsigned Val) {
577 if (Val == 0 || (Val & (Val-1))) return 0;
578 unsigned Count = 0;
579 while (Val != 1) {
580 Val >>= 1;
581 ++Count;
582 }
583 return Count;
584}
585
Nate Begeman7ddecb42005-04-06 23:51:40 +0000586// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
587// any number of 0's on either side. the 1's are allowed to wrap from LSB to
588// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
589// not, since all 1's are not contiguous.
590static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
591 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000592 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000593 ME = 0;
594
595 // look for first set bit
596 int i = 0;
597 for (; i < 32; i++) {
598 if ((Val & (1 << (31 - i))) != 0) {
599 MB = i;
600 ME = i;
601 break;
602 }
603 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000604
Nate Begeman7ddecb42005-04-06 23:51:40 +0000605 // look for last set bit
606 for (; i < 32; i++) {
607 if ((Val & (1 << (31 - i))) == 0)
608 break;
609 ME = i;
610 }
611
612 // look for next set bit
613 for (; i < 32; i++) {
614 if ((Val & (1 << (31 - i))) != 0)
615 break;
616 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000617
Nate Begeman7ddecb42005-04-06 23:51:40 +0000618 // if we exhausted all the bits, we found a match at this point for 0*1*0*
619 if (i == 32)
620 return true;
621
622 // since we just encountered more 1's, if it doesn't wrap around to the
623 // most significant bit of the word, then we did not find a match to 1*0*1* so
624 // exit.
625 if (MB != 0)
626 return false;
627
628 // look for last set bit
629 for (MB = i; i < 32; i++) {
630 if ((Val & (1 << (31 - i))) == 0)
631 break;
632 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000633
Nate Begeman7ddecb42005-04-06 23:51:40 +0000634 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
635 // the value is not a run of ones.
636 if (i == 32)
637 return true;
638 return false;
639}
640
Nate Begeman439b4442005-04-05 04:22:58 +0000641/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000642/// the ConstantSDNode N can be used as an immediate to Opcode. The return
643/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000644/// ConstantSDNode, or is not suitable for use by that opcode.
645/// Return value codes for turning into an enum someday:
646/// 1: constant may be used in normal immediate form.
647/// 2: constant may be used in shifted immediate form.
648/// 3: log base 2 of the constant may be used.
649/// 4: constant is suitable for integer division conversion
650/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000651///
Nate Begeman439b4442005-04-05 04:22:58 +0000652static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
653 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000654 if (N.getOpcode() != ISD::Constant) return 0;
655
656 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000657
Nate Begemana9795f82005-03-24 04:41:43 +0000658 switch(Opcode) {
659 default: return 0;
660 case ISD::ADD:
661 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
662 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
663 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000664 case ISD::AND: {
665 unsigned MB, ME;
666 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
667 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
668 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
669 break;
670 }
Nate Begemana9795f82005-03-24 04:41:43 +0000671 case ISD::XOR:
672 case ISD::OR:
673 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
674 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
675 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000676 case ISD::MUL:
677 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
678 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000679 case ISD::SUB:
680 // handle subtract-from separately from subtract, since subi is really addi
681 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
682 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
683 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000684 case ISD::SETCC:
685 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
686 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
687 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000688 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000689 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000690 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000691 if (v <= -2 || v >= 2) { return 4; }
692 break;
693 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000694 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000695 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000696 }
697 return 0;
698}
Nate Begeman3e897162005-03-31 23:55:40 +0000699
Nate Begemanc7bd4822005-04-11 06:34:10 +0000700/// NodeHasRecordingVariant - If SelectExpr can always produce code for
701/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
702/// return false.
703static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
704 switch(NodeOpcode) {
705 default: return false;
706 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000707 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000708 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000709 }
710}
711
Nate Begeman3e897162005-03-31 23:55:40 +0000712/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
713/// to Condition. If the Condition is unordered or unsigned, the bool argument
714/// U is set to true, otherwise it is set to false.
715static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
716 U = false;
717 switch (Condition) {
718 default: assert(0 && "Unknown condition!"); abort();
719 case ISD::SETEQ: return PPC::BEQ;
720 case ISD::SETNE: return PPC::BNE;
721 case ISD::SETULT: U = true;
722 case ISD::SETLT: return PPC::BLT;
723 case ISD::SETULE: U = true;
724 case ISD::SETLE: return PPC::BLE;
725 case ISD::SETUGT: U = true;
726 case ISD::SETGT: return PPC::BGT;
727 case ISD::SETUGE: U = true;
728 case ISD::SETGE: return PPC::BGE;
729 }
Nate Begeman04730362005-04-01 04:45:11 +0000730 return 0;
731}
732
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000733/// getCROpForOp - Return the condition register opcode (or inverted opcode)
734/// associated with the SelectionDAG opcode.
735static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
736 switch (Opcode) {
737 default: assert(0 && "Unknown opcode!"); abort();
738 case ISD::AND:
739 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
740 if (!Inv1 && !Inv2) return PPC::CRAND;
741 if (Inv1 ^ Inv2) return PPC::CRANDC;
742 case ISD::OR:
743 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
744 if (!Inv1 && !Inv2) return PPC::CROR;
745 if (Inv1 ^ Inv2) return PPC::CRORC;
746 }
747 return 0;
748}
749
750/// getCRIdxForSetCC - Return the index of the condition register field
751/// associated with the SetCC condition, and whether or not the field is
752/// treated as inverted. That is, lt = 0; ge = 0 inverted.
753static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
754 switch (Condition) {
755 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000756 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000757 case ISD::SETLT: Inv = false; return 0;
758 case ISD::SETUGE:
759 case ISD::SETGE: Inv = true; return 0;
760 case ISD::SETUGT:
761 case ISD::SETGT: Inv = false; return 1;
762 case ISD::SETULE:
763 case ISD::SETLE: Inv = true; return 1;
764 case ISD::SETEQ: Inv = false; return 2;
765 case ISD::SETNE: Inv = true; return 2;
766 }
767 return 0;
768}
769
Nate Begeman04730362005-04-01 04:45:11 +0000770/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
771/// and store immediate instructions.
772static unsigned IndexedOpForOp(unsigned Opcode) {
773 switch(Opcode) {
774 default: assert(0 && "Unknown opcode!"); abort();
775 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
776 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
777 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
778 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
779 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
780 case PPC::LFD: return PPC::LFDX;
781 }
782 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000783}
Nate Begeman815d6da2005-04-06 00:25:27 +0000784
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000785// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000786// a multiply.
787struct ms {
788 int m; // magic number
789 int s; // shift amount
790};
791
792struct mu {
793 unsigned int m; // magic number
794 int a; // add indicator
795 int s; // shift amount
796};
797
798/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000799/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000800/// or -1.
801static struct ms magic(int d) {
802 int p;
803 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
804 const unsigned int two31 = 2147483648U; // 2^31
805 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000806
Nate Begeman815d6da2005-04-06 00:25:27 +0000807 ad = abs(d);
808 t = two31 + ((unsigned int)d >> 31);
809 anc = t - 1 - t%ad; // absolute value of nc
810 p = 31; // initialize p
811 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
812 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
813 q2 = two31/ad; // initialize q2 = 2p/abs(d)
814 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
815 do {
816 p = p + 1;
817 q1 = 2*q1; // update q1 = 2p/abs(nc)
818 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
819 if (r1 >= anc) { // must be unsigned comparison
820 q1 = q1 + 1;
821 r1 = r1 - anc;
822 }
823 q2 = 2*q2; // update q2 = 2p/abs(d)
824 r2 = 2*r2; // update r2 = rem(2p/abs(d))
825 if (r2 >= ad) { // must be unsigned comparison
826 q2 = q2 + 1;
827 r2 = r2 - ad;
828 }
829 delta = ad - r2;
830 } while (q1 < delta || (q1 == delta && r1 == 0));
831
832 mag.m = q2 + 1;
833 if (d < 0) mag.m = -mag.m; // resulting magic number
834 mag.s = p - 32; // resulting shift
835 return mag;
836}
837
838/// magicu - calculate the magic numbers required to codegen an integer udiv as
839/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
840static struct mu magicu(unsigned d)
841{
842 int p;
843 unsigned int nc, delta, q1, r1, q2, r2;
844 struct mu magu;
845 magu.a = 0; // initialize "add" indicator
846 nc = - 1 - (-d)%d;
847 p = 31; // initialize p
848 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
849 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
850 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
851 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
852 do {
853 p = p + 1;
854 if (r1 >= nc - r1 ) {
855 q1 = 2*q1 + 1; // update q1
856 r1 = 2*r1 - nc; // update r1
857 }
858 else {
859 q1 = 2*q1; // update q1
860 r1 = 2*r1; // update r1
861 }
862 if (r2 + 1 >= d - r2) {
863 if (q2 >= 0x7FFFFFFF) magu.a = 1;
864 q2 = 2*q2 + 1; // update q2
865 r2 = 2*r2 + 1 - d; // update r2
866 }
867 else {
868 if (q2 >= 0x80000000) magu.a = 1;
869 q2 = 2*q2; // update q2
870 r2 = 2*r2 + 1; // update r2
871 }
872 delta = d - 1 - r2;
873 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
874 magu.m = q2 + 1; // resulting magic number
875 magu.s = p - 32; // resulting shift
876 return magu;
877}
878}
879
880/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
881/// return a DAG expression to select that will generate the same value by
882/// multiplying by a magic number. See:
883/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
884SDOperand ISel::BuildSDIVSequence(SDOperand N) {
885 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
886 ms magics = magic(d);
887 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000888 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000889 ISelDAG->getConstant(magics.m, MVT::i32));
890 // If d > 0 and m < 0, add the numerator
891 if (d > 0 && magics.m < 0)
892 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
893 // If d < 0 and m > 0, subtract the numerator.
894 if (d < 0 && magics.m > 0)
895 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
896 // Shift right algebraic if shift value is nonzero
897 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000898 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000899 ISelDAG->getConstant(magics.s, MVT::i32));
900 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000901 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000902 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000903 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000904}
905
906/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
907/// return a DAG expression to select that will generate the same value by
908/// multiplying by a magic number. See:
909/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
910SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000911 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000912 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
913 mu magics = magicu(d);
914 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000915 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000916 ISelDAG->getConstant(magics.m, MVT::i32));
917 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000918 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000919 ISelDAG->getConstant(magics.s, MVT::i32));
920 } else {
921 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000922 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000923 ISelDAG->getConstant(1, MVT::i32));
924 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000925 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000926 ISelDAG->getConstant(magics.s-1, MVT::i32));
927 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000928 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000929}
930
Nate Begemanc7b09f12005-03-25 08:34:25 +0000931/// getGlobalBaseReg - Output the instructions required to put the
932/// base address to use for accessing globals into a register.
933///
934unsigned ISel::getGlobalBaseReg() {
935 if (!GlobalBaseInitialized) {
936 // Insert the set of GlobalBaseReg into the first MBB of the function
937 MachineBasicBlock &FirstMBB = BB->getParent()->front();
938 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
939 GlobalBaseReg = MakeReg(MVT::i32);
940 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
941 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
942 GlobalBaseInitialized = true;
943 }
944 return GlobalBaseReg;
945}
946
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000947/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000948/// Constant Pool. Optionally takes a register in which to load the value.
949unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
950 unsigned Tmp1 = MakeReg(MVT::i32);
951 if (0 == Result) Result = MakeReg(MVT::f64);
952 MachineConstantPool *CP = BB->getParent()->getConstantPool();
953 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
954 unsigned CPI = CP->getConstantPoolIndex(CFP);
955 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
956 .addConstantPoolIndex(CPI);
957 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
958 return Result;
959}
960
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000961/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000962/// Inv is true, then invert the result.
963void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
964 unsigned IntCR = MakeReg(MVT::i32);
965 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
966 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
967 if (Inv) {
968 unsigned Tmp1 = MakeReg(MVT::i32);
969 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
970 .addImm(31).addImm(31);
971 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
972 } else {
973 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
974 .addImm(31).addImm(31);
975 }
976}
977
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000978/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000979/// the rotate left word immediate then mask insert (rlwimi) instruction.
980/// Returns true on success, false if the caller still needs to select OR.
981///
982/// Patterns matched:
983/// 1. or shl, and 5. or and, and
984/// 2. or and, shl 6. or shl, shr
985/// 3. or shr, and 7. or shr, shl
986/// 4. or and, shr
987bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000988 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000989 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000990
991 SDOperand Op0 = OR.getOperand(0);
992 SDOperand Op1 = OR.getOperand(1);
993
994 unsigned Op0Opc = Op0.getOpcode();
995 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000996
Nate Begeman7ddecb42005-04-06 23:51:40 +0000997 // Verify that we have the correct opcodes
998 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
999 return false;
1000 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
1001 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001002
Nate Begeman7ddecb42005-04-06 23:51:40 +00001003 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001004 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001005 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001006 switch(Op0Opc) {
1007 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1008 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1009 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1010 }
1011 } else {
1012 return false;
1013 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001014
Nate Begeman7ddecb42005-04-06 23:51:40 +00001015 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001016 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001017 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001018 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001019 case ISD::SHL:
1020 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001021 InsMask <<= Amount;
1022 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001023 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001024 case ISD::SRL:
1025 Amount = CN->getValue();
1026 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001027 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001028 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001029 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001030 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001031 InsMask &= (unsigned)CN->getValue();
1032 break;
1033 }
1034 } else {
1035 return false;
1036 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001037
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001038 unsigned Tmp3 = 0;
1039
1040 // If both of the inputs are ANDs and one of them has a logical shift by
1041 // constant as its input, make that the inserted value so that we can combine
1042 // the shift into the rotate part of the rlwimi instruction
1043 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
1044 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
1045 Op1.getOperand(0).getOpcode() == ISD::SRL) {
1046 if (ConstantSDNode *CN =
1047 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
1048 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1049 CN->getValue() : 32 - CN->getValue();
1050 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1051 }
1052 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1053 Op0.getOperand(0).getOpcode() == ISD::SRL) {
1054 if (ConstantSDNode *CN =
1055 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1056 std::swap(Op0, Op1);
1057 std::swap(TgtMask, InsMask);
1058 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1059 CN->getValue() : 32 - CN->getValue();
1060 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1061 }
1062 }
1063 }
1064
Nate Begeman7ddecb42005-04-06 23:51:40 +00001065 // Verify that the Target mask and Insert mask together form a full word mask
1066 // and that the Insert mask is a run of set bits (which implies both are runs
1067 // of set bits). Given that, Select the arguments and generate the rlwimi
1068 // instruction.
1069 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001070 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001071 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001072 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001073 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1074 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001075 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001076 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001077 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1078 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1079 .addImm(0).addImm(31);
1080 return true;
1081 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001082 if (Op0Opc == ISD::AND && fullMask)
1083 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001084 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001085 Tmp1 = SelectExpr(Op0);
1086 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001087 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1088 .addImm(Amount).addImm(MB).addImm(ME);
1089 return true;
1090 }
1091 return false;
1092}
1093
Nate Begeman3664cef2005-04-13 22:14:14 +00001094/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1095/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1096/// wider than the implicit mask, then we can get rid of the AND and let the
1097/// shift do the mask.
1098unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1099 unsigned C;
1100 if (N.getOpcode() == ISD::AND &&
1101 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1102 31 == (C & 0xFFFF) && // ME
1103 26 >= (C >> 16)) // MB
1104 return SelectExpr(N.getOperand(0));
1105 else
1106 return SelectExpr(N);
1107}
1108
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001109unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001110 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001111 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001112 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001113 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001114
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001115 // Allocate a condition register for this expression
1116 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001117
Nate Begemandffcfcc2005-04-01 00:32:34 +00001118 // If the first operand to the select is a SETCC node, then we can fold it
1119 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001120 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001121 bool U;
1122 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001123 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001124
Nate Begeman439b4442005-04-05 04:22:58 +00001125 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001126 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001127 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001128 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001129 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001130 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1131 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001132 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001133 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1134 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001135 RecordSuccess = false;
1136 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1137 if (RecordSuccess) {
1138 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001139 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1140 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001141 }
1142 AlreadySelected = true;
1143 }
1144 // If we could not implicitly set CR0, then emit a compare immediate
1145 // instead.
1146 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001147 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001148 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001149 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001150 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001151 } else {
1152 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1153 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001154 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001155 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001156 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001157 }
1158 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001159 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001160 return SelectCCExpr(CC, Opc, Inv, Idx);
1161 // If this isn't a SetCC, then select the value and compare it against zero,
1162 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001163 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001164 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001165 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001166 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001167 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001168 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001169}
1170
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001171unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001172 unsigned &Idx) {
1173 bool Inv0, Inv1;
1174 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1175
1176 // Allocate a condition register for this expression
1177 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1178
1179 // Check for the operations we support:
1180 switch(N.getOpcode()) {
1181 default:
1182 Opc = PPC::BNE;
1183 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1184 Tmp1 = SelectExpr(N);
1185 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1186 break;
1187 case ISD::OR:
1188 case ISD::AND:
1189 ++MultiBranch;
1190 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1191 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1192 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1193 if (Inv0 && !Inv1) {
1194 std::swap(Tmp1, Tmp2);
1195 std::swap(Idx0, Idx1);
1196 Opc = Opc1;
1197 }
1198 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1199 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1200 .addReg(Tmp2).addImm(Idx1);
1201 Inv = false;
1202 Idx = Idx0;
1203 break;
1204 case ISD::SETCC:
1205 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1206 Result = Tmp1;
1207 break;
1208 }
1209 return Result;
1210}
1211
Nate Begemandffcfcc2005-04-01 00:32:34 +00001212/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001213bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001214{
Nate Begeman96fc6812005-03-31 02:05:53 +00001215 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001216 if (N.getOpcode() == ISD::ADD) {
1217 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001218 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001219 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001220 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001221 }
Nate Begeman04730362005-04-01 04:45:11 +00001222 offset = SelectExpr(N.getOperand(1));
1223 return true;
1224 }
Nate Begemana9795f82005-03-24 04:41:43 +00001225 Reg = SelectExpr(N);
1226 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001227 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001228}
1229
1230void ISel::SelectBranchCC(SDOperand N)
1231{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001232 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001233 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001234
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001235 bool Inv;
1236 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001237 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001238 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001239
Nate Begeman439009c2005-06-15 18:22:43 +00001240 // Iterate to the next basic block
1241 ilist<MachineBasicBlock>::iterator It = BB;
1242 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001243
1244 // If this is a two way branch, then grab the fallthrough basic block argument
1245 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1246 // if necessary by the branch selection pass. Otherwise, emit a standard
1247 // conditional branch.
1248 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001249 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001250 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1251 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001252 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001253 .addMBB(Dest).addMBB(Fallthrough);
1254 if (Fallthrough != It)
1255 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1256 } else {
1257 if (Fallthrough != It) {
1258 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001259 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001260 .addMBB(Fallthrough).addMBB(Dest);
1261 }
1262 }
1263 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001264 // If the fallthrough path is off the end of the function, which would be
1265 // undefined behavior, set it to be the same as the current block because
1266 // we have nothing better to set it to, and leaving it alone will cause the
1267 // PowerPC Branch Selection pass to crash.
1268 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001269 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001270 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001271 }
Nate Begemana9795f82005-03-24 04:41:43 +00001272 return;
1273}
1274
1275unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1276{
1277 unsigned Tmp1, Tmp2, Tmp3;
1278 unsigned Opc = 0;
1279 SDNode *Node = N.Val;
1280 MVT::ValueType DestType = N.getValueType();
1281 unsigned opcode = N.getOpcode();
1282
1283 switch (opcode) {
1284 default:
1285 Node->dump();
1286 assert(0 && "Node not handled!\n");
1287
Nate Begeman23afcfb2005-03-29 22:48:55 +00001288 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001289 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1290 // and an FP comparison in the SetCC node.
1291 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1292 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1293 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1294 SetCC->getCondition() != ISD::SETEQ &&
1295 SetCC->getCondition() != ISD::SETNE) {
1296 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001297 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1298 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001299
Nate Begeman3e897162005-03-31 23:55:40 +00001300 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1301 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1302 switch(SetCC->getCondition()) {
1303 default: assert(0 && "Invalid FSEL condition"); abort();
1304 case ISD::SETULT:
1305 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001306 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001307 case ISD::SETUGE:
1308 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001309 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001310 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1311 return Result;
1312 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001313 case ISD::SETGT:
1314 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001315 case ISD::SETULE:
1316 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001317 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1318 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1319 } else {
1320 Tmp2 = MakeReg(VT);
1321 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1322 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1323 }
Nate Begeman3e897162005-03-31 23:55:40 +00001324 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1325 return Result;
1326 }
1327 }
1328 } else {
1329 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001330 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001331 Tmp2 = SelectExpr(SetCC->getOperand(1));
1332 Tmp3 = MakeReg(VT);
1333 switch(SetCC->getCondition()) {
1334 default: assert(0 && "Invalid FSEL condition"); abort();
1335 case ISD::SETULT:
1336 case ISD::SETLT:
1337 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1338 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1339 return Result;
1340 case ISD::SETUGE:
1341 case ISD::SETGE:
1342 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1343 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1344 return Result;
1345 case ISD::SETUGT:
1346 case ISD::SETGT:
1347 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1348 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1349 return Result;
1350 case ISD::SETULE:
1351 case ISD::SETLE:
1352 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1353 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1354 return Result;
1355 }
1356 }
1357 assert(0 && "Should never get here");
1358 return 0;
1359 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001360
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001361 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001362 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1363 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001364 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001365
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001366 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001367 // value and the MBB to hold the PHI instruction for this SetCC.
1368 MachineBasicBlock *thisMBB = BB;
1369 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1370 ilist<MachineBasicBlock>::iterator It = BB;
1371 ++It;
1372
1373 // thisMBB:
1374 // ...
1375 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001376 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001377 // bCC copy1MBB
1378 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001379 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1380 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001381 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001382 MachineFunction *F = BB->getParent();
1383 F->getBasicBlockList().insert(It, copy0MBB);
1384 F->getBasicBlockList().insert(It, sinkMBB);
1385 // Update machine-CFG edges
1386 BB->addSuccessor(copy0MBB);
1387 BB->addSuccessor(sinkMBB);
1388
1389 // copy0MBB:
1390 // %FalseValue = ...
1391 // # fallthrough to sinkMBB
1392 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001393 // Update machine-CFG edges
1394 BB->addSuccessor(sinkMBB);
1395
1396 // sinkMBB:
1397 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1398 // ...
1399 BB = sinkMBB;
1400 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1401 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1402 return Result;
1403 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001404
1405 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001406 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001407 ISD::ADD == N.getOperand(0).getOpcode() &&
1408 N.getOperand(0).Val->hasOneUse() &&
1409 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1410 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001411 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001412 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1413 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1414 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1415 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1416 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001417 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001418 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001419 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001420 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1421 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001422 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001423 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1424 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1425 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1426 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001427 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1428 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001429 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1430 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1431 } else {
1432 Tmp1 = SelectExpr(N.getOperand(0));
1433 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1434 }
1435 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001436
Nate Begeman27eeb002005-04-02 05:59:34 +00001437 case ISD::FABS:
1438 Tmp1 = SelectExpr(N.getOperand(0));
1439 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1440 return Result;
1441
Nate Begemana9795f82005-03-24 04:41:43 +00001442 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001443 assert (DestType == MVT::f32 &&
1444 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001445 "only f64 to f32 conversion supported here");
1446 Tmp1 = SelectExpr(N.getOperand(0));
1447 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1448 return Result;
1449
1450 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001451 assert (DestType == MVT::f64 &&
1452 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001453 "only f32 to f64 conversion supported here");
1454 Tmp1 = SelectExpr(N.getOperand(0));
1455 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1456 return Result;
1457
1458 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001459 if (Result == 1)
1460 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1461 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1462 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1463 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001464
Nate Begeman6d369cc2005-04-01 01:08:07 +00001465 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001466 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001467 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001468 return Result;
1469 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001470
Nate Begemana9795f82005-03-24 04:41:43 +00001471 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001472 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1473 N.getOperand(0).Val->hasOneUse()) {
1474 ++FusedFP; // Statistic
1475 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1476 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1477 Tmp3 = SelectExpr(N.getOperand(1));
1478 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1479 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1480 return Result;
1481 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001482 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1483 N.getOperand(1).Val->hasOneUse()) {
1484 ++FusedFP; // Statistic
1485 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1486 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1487 Tmp3 = SelectExpr(N.getOperand(0));
1488 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1489 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1490 return Result;
1491 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001492 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1493 Tmp1 = SelectExpr(N.getOperand(0));
1494 Tmp2 = SelectExpr(N.getOperand(1));
1495 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1496 return Result;
1497
Nate Begemana9795f82005-03-24 04:41:43 +00001498 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001499 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1500 N.getOperand(0).Val->hasOneUse()) {
1501 ++FusedFP; // Statistic
1502 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1503 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1504 Tmp3 = SelectExpr(N.getOperand(1));
1505 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1506 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1507 return Result;
1508 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001509 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1510 N.getOperand(1).Val->hasOneUse()) {
1511 ++FusedFP; // Statistic
1512 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1513 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1514 Tmp3 = SelectExpr(N.getOperand(0));
1515 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1516 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1517 return Result;
1518 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001519 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1520 Tmp1 = SelectExpr(N.getOperand(0));
1521 Tmp2 = SelectExpr(N.getOperand(1));
1522 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1523 return Result;
1524
1525 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001526 case ISD::SDIV:
1527 switch( opcode ) {
1528 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001529 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1530 };
Nate Begemana9795f82005-03-24 04:41:43 +00001531 Tmp1 = SelectExpr(N.getOperand(0));
1532 Tmp2 = SelectExpr(N.getOperand(1));
1533 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1534 return Result;
1535
Nate Begemana9795f82005-03-24 04:41:43 +00001536 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001537 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001538 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001539 && "int to float must operate on i32");
1540 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1541 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1542 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1543 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001544
Nate Begemanfdcf3412005-03-30 19:38:35 +00001545 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1546 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001547
Nate Begemanfdcf3412005-03-30 19:38:35 +00001548 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001549 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001550 // Store the hi & low halves of the fp value, currently in int regs
1551 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1552 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1553 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1554 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1555 // Generate the return value with a subtract
1556 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1557 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001558 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001559 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001560 // Store the hi & low halves of the fp value, currently in int regs
1561 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1562 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1563 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1564 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1565 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1566 // Generate the return value with a subtract
1567 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1568 }
1569 return Result;
1570 }
Nate Begemana9795f82005-03-24 04:41:43 +00001571 }
Nate Begeman6b559972005-04-01 02:59:27 +00001572 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001573 return 0;
1574}
1575
Nate Begemanc7bd4822005-04-11 06:34:10 +00001576unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001577 unsigned Result;
1578 unsigned Tmp1, Tmp2, Tmp3;
1579 unsigned Opc = 0;
1580 unsigned opcode = N.getOpcode();
1581
1582 SDNode *Node = N.Val;
1583 MVT::ValueType DestType = N.getValueType();
1584
Nate Begemana43b1762005-06-14 03:55:23 +00001585 if (Node->getOpcode() == ISD::CopyFromReg &&
1586 MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()))
1587 // Just use the specified register as our input.
1588 return cast<RegSDNode>(Node)->getReg();
1589
Nate Begemana9795f82005-03-24 04:41:43 +00001590 unsigned &Reg = ExprMap[N];
1591 if (Reg) return Reg;
1592
Nate Begeman27eeb002005-04-02 05:59:34 +00001593 switch (N.getOpcode()) {
1594 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001595 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001596 MakeReg(N.getValueType()) : 1;
1597 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001598 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001599 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001600 // If this is a call instruction, make sure to prepare ALL of the result
1601 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001602 if (Node->getNumValues() == 1)
1603 Reg = Result = 1; // Void call, just a chain.
1604 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001605 Result = MakeReg(Node->getValueType(0));
1606 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001607 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001608 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001609 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001610 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001611 break;
1612 case ISD::ADD_PARTS:
1613 case ISD::SUB_PARTS:
1614 case ISD::SHL_PARTS:
1615 case ISD::SRL_PARTS:
1616 case ISD::SRA_PARTS:
1617 Result = MakeReg(Node->getValueType(0));
1618 ExprMap[N.getValue(0)] = Result;
1619 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1620 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1621 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001622 }
1623
Nate Begemane5846682005-04-04 06:52:38 +00001624 if (ISD::CopyFromReg == opcode)
1625 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001626
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001627 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001628 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001629 ISD::UNDEF != opcode && ISD::CALL != opcode && ISD::TAILCALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001630 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001631
1632 switch (opcode) {
1633 default:
1634 Node->dump();
1635 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001636 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001637 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1638 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001639 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001640 // Generate both result values. FIXME: Need a better commment here?
1641 if (Result != 1)
1642 ExprMap[N.getValue(1)] = 1;
1643 else
1644 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1645
1646 // FIXME: We are currently ignoring the requested alignment for handling
1647 // greater than the stack alignment. This will need to be revisited at some
1648 // point. Align = N.getOperand(2);
1649 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1650 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1651 std::cerr << "Cannot allocate stack object with greater alignment than"
1652 << " the stack alignment yet!";
1653 abort();
1654 }
1655 Select(N.getOperand(0));
1656 Tmp1 = SelectExpr(N.getOperand(1));
1657 // Subtract size from stack pointer, thereby allocating some space.
1658 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1659 // Put a pointer to the space into the result register by copying the SP
1660 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1661 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001662
1663 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001664 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1665 Tmp2 = MakeReg(MVT::i32);
1666 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1667 .addConstantPoolIndex(Tmp1);
1668 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1669 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001670
1671 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001672 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001673 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001674 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001675
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001676 case ISD::GlobalAddress: {
1677 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001678 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001679 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1680 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001681 if (GV->hasWeakLinkage() || GV->isExternal()) {
1682 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1683 } else {
1684 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1685 }
1686 return Result;
1687 }
1688
Nate Begeman5e966612005-03-24 06:28:42 +00001689 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001690 case ISD::EXTLOAD:
1691 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001692 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001693 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1694 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001695 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001696
Nate Begeman5e966612005-03-24 06:28:42 +00001697 // Make sure we generate both values.
1698 if (Result != 1)
1699 ExprMap[N.getValue(1)] = 1; // Generate the token
1700 else
1701 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1702
1703 SDOperand Chain = N.getOperand(0);
1704 SDOperand Address = N.getOperand(1);
1705 Select(Chain);
1706
Nate Begeman9db505c2005-03-28 19:36:43 +00001707 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001708 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001709 case MVT::i1: Opc = PPC::LBZ; break;
1710 case MVT::i8: Opc = PPC::LBZ; break;
1711 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1712 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001713 case MVT::f32: Opc = PPC::LFS; break;
1714 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001715 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001716
Nate Begeman74d73452005-03-31 00:15:26 +00001717 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1718 Tmp1 = MakeReg(MVT::i32);
1719 int CPI = CP->getIndex();
1720 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1721 .addConstantPoolIndex(CPI);
1722 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001723 }
Nate Begeman74d73452005-03-31 00:15:26 +00001724 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001725 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1726 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001727 } else {
1728 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001729 bool idx = SelectAddr(Address, Tmp1, offset);
1730 if (idx) {
1731 Opc = IndexedOpForOp(Opc);
1732 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1733 } else {
1734 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1735 }
Nate Begeman5e966612005-03-24 06:28:42 +00001736 }
1737 return Result;
1738 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001739
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001740 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001741 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001742 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001743 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001744 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1745 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1746 };
1747 static const unsigned FPR[] = {
1748 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1749 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1750 };
1751
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001752 // Lower the chain for this call.
1753 Select(N.getOperand(0));
1754 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001755
Nate Begemand860aa62005-04-04 22:17:48 +00001756 MachineInstr *CallMI;
1757 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001758 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001759 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001760 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001761 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001762 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001763 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001764 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001765 true);
1766 } else {
1767 Tmp1 = SelectExpr(N.getOperand(1));
1768 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1769 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1770 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1771 .addReg(PPC::R12);
1772 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001773
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001774 // Load the register args to virtual regs
1775 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001776 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001777 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1778
1779 // Copy the virtual registers into the appropriate argument register
1780 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1781 switch(N.getOperand(i+2).getValueType()) {
1782 default: Node->dump(); assert(0 && "Unknown value type for call");
1783 case MVT::i1:
1784 case MVT::i8:
1785 case MVT::i16:
1786 case MVT::i32:
1787 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001788 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001789 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001790 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1791 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001792 ++GPR_idx;
1793 break;
1794 case MVT::f64:
1795 case MVT::f32:
1796 assert(FPR_idx < 13 && "Too many fp args");
1797 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001798 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001799 ++FPR_idx;
1800 break;
1801 }
1802 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001803
Nate Begemand860aa62005-04-04 22:17:48 +00001804 // Put the call instruction in the correct place in the MachineBasicBlock
1805 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001806
1807 switch (Node->getValueType(0)) {
1808 default: assert(0 && "Unknown value type for call result!");
1809 case MVT::Other: return 1;
1810 case MVT::i1:
1811 case MVT::i8:
1812 case MVT::i16:
1813 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001814 if (Node->getValueType(1) == MVT::i32) {
1815 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1816 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1817 } else {
1818 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1819 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001820 break;
1821 case MVT::f32:
1822 case MVT::f64:
1823 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1824 break;
1825 }
1826 return Result+N.ResNo;
1827 }
Nate Begemana9795f82005-03-24 04:41:43 +00001828
1829 case ISD::SIGN_EXTEND:
1830 case ISD::SIGN_EXTEND_INREG:
1831 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001832 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1833 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001834 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001835 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001836 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001837 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001838 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001839 break;
Nate Begeman74747862005-03-29 22:24:51 +00001840 case MVT::i1:
1841 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1842 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001843 }
Nate Begemana9795f82005-03-24 04:41:43 +00001844 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001845
Nate Begemana9795f82005-03-24 04:41:43 +00001846 case ISD::CopyFromReg:
1847 if (Result == 1)
1848 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1849 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1850 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1851 return Result;
1852
1853 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001854 Tmp1 = SelectExpr(N.getOperand(0));
1855 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1856 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001857 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001858 .addImm(31-Tmp2);
1859 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001860 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001861 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1862 }
1863 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001864
Nate Begeman5e966612005-03-24 06:28:42 +00001865 case ISD::SRL:
1866 Tmp1 = SelectExpr(N.getOperand(0));
1867 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1868 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001869 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001870 .addImm(Tmp2).addImm(31);
1871 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001872 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001873 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1874 }
1875 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001876
Nate Begeman5e966612005-03-24 06:28:42 +00001877 case ISD::SRA:
1878 Tmp1 = SelectExpr(N.getOperand(0));
1879 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1880 Tmp2 = CN->getValue() & 0x1F;
1881 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1882 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001883 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001884 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1885 }
1886 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001887
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001888 case ISD::CTLZ:
1889 Tmp1 = SelectExpr(N.getOperand(0));
1890 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1891 return Result;
1892
Nate Begemana9795f82005-03-24 04:41:43 +00001893 case ISD::ADD:
1894 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1895 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001896 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001897 default: assert(0 && "unhandled result code");
1898 case 0: // No immediate
1899 Tmp2 = SelectExpr(N.getOperand(1));
1900 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1901 break;
1902 case 1: // Low immediate
1903 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1904 break;
1905 case 2: // Shifted immediate
1906 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1907 break;
1908 }
1909 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001910
Nate Begemana9795f82005-03-24 04:41:43 +00001911 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001912 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001913 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001914 N.getOperand(1).getOpcode() == ISD::SETCC) {
1915 bool Inv;
1916 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1917 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1918 return Result;
1919 }
1920 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001921 // FIXME: should add check in getImmediateForOpcode to return a value
1922 // indicating the immediate is a run of set bits so we can emit a bitfield
1923 // clear with RLWINM instead.
1924 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1925 default: assert(0 && "unhandled result code");
1926 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001927 // Check for andc: and, (xor a, -1), b
1928 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1929 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1930 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1931 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1932 Tmp2 = SelectExpr(N.getOperand(1));
1933 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1934 return Result;
1935 }
1936 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001937 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001938 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001939 Opc = Recording ? PPC::ANDo : PPC::AND;
1940 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001941 break;
1942 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001943 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001944 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1945 break;
1946 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001947 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001948 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1949 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001950 case 5: // Bitfield mask
1951 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1952 Tmp3 = Tmp2 >> 16; // MB
1953 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001954
1955 if (N.getOperand(0).getOpcode() == ISD::SRL)
1956 if (ConstantSDNode *SA =
1957 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1958
1959 // We can fold the RLWINM and the SRL together if the mask is
1960 // clearing the top bits which are rotated around.
1961 unsigned RotAmt = 32-(SA->getValue() & 31);
1962 if (Tmp2 <= RotAmt) {
1963 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1964 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1965 .addImm(Tmp3).addImm(Tmp2);
1966 break;
1967 }
1968 }
1969
1970 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001971 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1972 .addImm(Tmp3).addImm(Tmp2);
1973 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001974 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001975 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001976 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001977
Nate Begemana9795f82005-03-24 04:41:43 +00001978 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001979 if (SelectBitfieldInsert(N, Result))
1980 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001981 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001982 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001983 N.getOperand(1).getOpcode() == ISD::SETCC) {
1984 bool Inv;
1985 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1986 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1987 return Result;
1988 }
1989 }
Nate Begemana9795f82005-03-24 04:41:43 +00001990 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001991 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001992 default: assert(0 && "unhandled result code");
1993 case 0: // No immediate
1994 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001995 Opc = Recording ? PPC::ORo : PPC::OR;
1996 RecordSuccess = true;
1997 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001998 break;
1999 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00002000 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002001 break;
2002 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00002003 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002004 break;
2005 }
2006 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002007
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002008 case ISD::XOR: {
2009 // Check for EQV: xor, (xor a, -1), b
2010 if (N.getOperand(0).getOpcode() == ISD::XOR &&
2011 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
2012 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002013 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2014 Tmp2 = SelectExpr(N.getOperand(1));
2015 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
2016 return Result;
2017 }
Chris Lattner837a5212005-04-21 21:09:11 +00002018 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002019 if (N.getOperand(1).getOpcode() == ISD::Constant &&
2020 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002021 switch(N.getOperand(0).getOpcode()) {
2022 case ISD::OR:
2023 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2024 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2025 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2026 break;
2027 case ISD::AND:
2028 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2029 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2030 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
2031 break;
Chris Lattner837a5212005-04-21 21:09:11 +00002032 case ISD::XOR:
2033 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2034 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2035 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
2036 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002037 default:
2038 Tmp1 = SelectExpr(N.getOperand(0));
2039 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
2040 break;
2041 }
2042 return Result;
2043 }
2044 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002045 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002046 default: assert(0 && "unhandled result code");
2047 case 0: // No immediate
2048 Tmp2 = SelectExpr(N.getOperand(1));
2049 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2050 break;
2051 case 1: // Low immediate
2052 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
2053 break;
2054 case 2: // Shifted immediate
2055 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
2056 break;
2057 }
2058 return Result;
2059 }
2060
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002061 case ISD::SUB:
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002062 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
2063 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002064 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002065 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00002066 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002067 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2068 } else {
2069 Tmp1 = SelectExpr(N.getOperand(0));
2070 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002071 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
2072 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002073 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002074
Nate Begeman5e966612005-03-24 06:28:42 +00002075 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002076 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002077 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00002078 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2079 else {
2080 Tmp2 = SelectExpr(N.getOperand(1));
2081 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
2082 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002083 return Result;
2084
Nate Begeman815d6da2005-04-06 00:25:27 +00002085 case ISD::MULHS:
2086 case ISD::MULHU:
2087 Tmp1 = SelectExpr(N.getOperand(0));
2088 Tmp2 = SelectExpr(N.getOperand(1));
2089 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
2090 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2091 return Result;
2092
Nate Begemanf3d08f32005-03-29 00:03:27 +00002093 case ISD::SDIV:
2094 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00002095 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
2096 default: break;
2097 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
2098 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00002099 Tmp1 = MakeReg(MVT::i32);
2100 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00002101 if ((int)Tmp3 < 0) {
2102 unsigned Tmp4 = MakeReg(MVT::i32);
2103 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
2104 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
2105 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
2106 } else {
2107 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
2108 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
2109 }
Nate Begeman80196b12005-04-05 00:15:08 +00002110 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00002111 // If this is a divide by constant, we can emit code using some magic
2112 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00002113 case 4:
2114 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002115 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00002116 return SelectExpr(BuildSDIVSequence(N));
2117 else
2118 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002119 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002120 Tmp1 = SelectExpr(N.getOperand(0));
2121 Tmp2 = SelectExpr(N.getOperand(1));
2122 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2123 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2124 return Result;
2125
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002126 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002127 case ISD::SUB_PARTS: {
2128 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2129 "Not an i64 add/sub!");
2130 // Emit all of the operands.
2131 std::vector<unsigned> InVals;
2132 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2133 InVals.push_back(SelectExpr(N.getOperand(i)));
2134 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002135 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2136 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002137 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002138 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2139 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2140 }
2141 return Result+N.ResNo;
2142 }
2143
2144 case ISD::SHL_PARTS:
2145 case ISD::SRA_PARTS:
2146 case ISD::SRL_PARTS: {
2147 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2148 "Not an i64 shift!");
2149 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2150 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002151 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2152 Tmp1 = MakeReg(MVT::i32);
2153 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002154 Tmp3 = MakeReg(MVT::i32);
2155 unsigned Tmp4 = MakeReg(MVT::i32);
2156 unsigned Tmp5 = MakeReg(MVT::i32);
2157 unsigned Tmp6 = MakeReg(MVT::i32);
2158 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2159 if (ISD::SHL_PARTS == opcode) {
2160 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2161 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2162 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2163 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002164 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002165 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2166 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2167 } else if (ISD::SRL_PARTS == opcode) {
2168 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2169 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2170 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2171 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2172 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2173 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2174 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2175 } else {
2176 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2177 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2178 MachineBasicBlock *OldMBB = BB;
2179 MachineFunction *F = BB->getParent();
2180 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2181 F->getBasicBlockList().insert(It, TmpMBB);
2182 F->getBasicBlockList().insert(It, PhiMBB);
2183 BB->addSuccessor(TmpMBB);
2184 BB->addSuccessor(PhiMBB);
2185 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2186 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2187 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2188 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2189 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2190 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2191 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2192 // Select correct least significant half if the shift amount > 32
2193 BB = TmpMBB;
2194 unsigned Tmp7 = MakeReg(MVT::i32);
2195 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2196 TmpMBB->addSuccessor(PhiMBB);
2197 BB = PhiMBB;
2198 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2199 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002200 }
2201 return Result+N.ResNo;
2202 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002203
Nate Begemana9795f82005-03-24 04:41:43 +00002204 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002205 case ISD::FP_TO_SINT: {
2206 bool U = (ISD::FP_TO_UINT == opcode);
2207 Tmp1 = SelectExpr(N.getOperand(0));
2208 if (!U) {
2209 Tmp2 = MakeReg(MVT::f64);
2210 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2211 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2212 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2213 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2214 return Result;
2215 } else {
2216 unsigned Zero = getConstDouble(0.0);
2217 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2218 unsigned Border = getConstDouble(1LL << 31);
2219 unsigned UseZero = MakeReg(MVT::f64);
2220 unsigned UseMaxInt = MakeReg(MVT::f64);
2221 unsigned UseChoice = MakeReg(MVT::f64);
2222 unsigned TmpReg = MakeReg(MVT::f64);
2223 unsigned TmpReg2 = MakeReg(MVT::f64);
2224 unsigned ConvReg = MakeReg(MVT::f64);
2225 unsigned IntTmp = MakeReg(MVT::i32);
2226 unsigned XorReg = MakeReg(MVT::i32);
2227 MachineFunction *F = BB->getParent();
2228 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2229 // Update machine-CFG edges
2230 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2231 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2232 MachineBasicBlock *OldMBB = BB;
2233 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2234 F->getBasicBlockList().insert(It, XorMBB);
2235 F->getBasicBlockList().insert(It, PhiMBB);
2236 BB->addSuccessor(XorMBB);
2237 BB->addSuccessor(PhiMBB);
2238 // Convert from floating point to unsigned 32-bit value
2239 // Use 0 if incoming value is < 0.0
2240 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2241 // Use 2**32 - 1 if incoming value is >= 2**32
2242 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2243 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2244 .addReg(MaxInt);
2245 // Subtract 2**31
2246 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2247 // Use difference if >= 2**31
2248 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2249 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2250 .addReg(UseChoice);
2251 // Convert to integer
2252 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2253 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2254 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2255 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2256 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2257
2258 // XorMBB:
2259 // add 2**31 if input was >= 2**31
2260 BB = XorMBB;
2261 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2262 XorMBB->addSuccessor(PhiMBB);
2263
2264 // PhiMBB:
2265 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2266 BB = PhiMBB;
2267 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2268 .addReg(XorReg).addMBB(XorMBB);
2269 return Result;
2270 }
2271 assert(0 && "Should never get here");
2272 return 0;
2273 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002274
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002275 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002276 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002277 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002278 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002279 // We can codegen setcc op, imm very efficiently compared to a brcond.
2280 // Check for those cases here.
2281 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002282 if (CN->getValue() == 0) {
2283 Tmp1 = SelectExpr(SetCC->getOperand(0));
2284 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002285 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002286 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002287 Tmp2 = MakeReg(MVT::i32);
2288 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2289 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2290 .addImm(5).addImm(31);
2291 break;
2292 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002293 Tmp2 = MakeReg(MVT::i32);
2294 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2295 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2296 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002297 case ISD::SETLT:
2298 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2299 .addImm(31).addImm(31);
2300 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002301 case ISD::SETGT:
2302 Tmp2 = MakeReg(MVT::i32);
2303 Tmp3 = MakeReg(MVT::i32);
2304 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2305 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2306 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2307 .addImm(31).addImm(31);
2308 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002309 }
2310 return Result;
2311 }
2312 // setcc op, -1
2313 if (CN->isAllOnesValue()) {
2314 Tmp1 = SelectExpr(SetCC->getOperand(0));
2315 switch (SetCC->getCondition()) {
2316 default: assert(0 && "Unhandled SetCC condition"); abort();
2317 case ISD::SETEQ:
2318 Tmp2 = MakeReg(MVT::i32);
2319 Tmp3 = MakeReg(MVT::i32);
2320 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2321 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2322 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002323 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002324 case ISD::SETNE:
2325 Tmp2 = MakeReg(MVT::i32);
2326 Tmp3 = MakeReg(MVT::i32);
2327 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2328 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2329 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2330 break;
2331 case ISD::SETLT:
2332 Tmp2 = MakeReg(MVT::i32);
2333 Tmp3 = MakeReg(MVT::i32);
2334 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2335 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2336 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2337 .addImm(31).addImm(31);
2338 break;
2339 case ISD::SETGT:
2340 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002341 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2342 .addImm(31).addImm(31);
2343 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2344 break;
2345 }
2346 return Result;
2347 }
2348 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002349
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002350 bool Inv;
2351 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2352 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002353 return Result;
2354 }
2355 assert(0 && "Is this legal?");
2356 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002357
Nate Begeman74747862005-03-29 22:24:51 +00002358 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002359 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002360 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2361 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002362 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002363
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002364 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002365 // value and the MBB to hold the PHI instruction for this SetCC.
2366 MachineBasicBlock *thisMBB = BB;
2367 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2368 ilist<MachineBasicBlock>::iterator It = BB;
2369 ++It;
2370
2371 // thisMBB:
2372 // ...
2373 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002374 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002375 // bCC copy1MBB
2376 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002377 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2378 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002379 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002380 MachineFunction *F = BB->getParent();
2381 F->getBasicBlockList().insert(It, copy0MBB);
2382 F->getBasicBlockList().insert(It, sinkMBB);
2383 // Update machine-CFG edges
2384 BB->addSuccessor(copy0MBB);
2385 BB->addSuccessor(sinkMBB);
2386
2387 // copy0MBB:
2388 // %FalseValue = ...
2389 // # fallthrough to sinkMBB
2390 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002391 // Update machine-CFG edges
2392 BB->addSuccessor(sinkMBB);
2393
2394 // sinkMBB:
2395 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2396 // ...
2397 BB = sinkMBB;
2398 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2399 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002400 return Result;
2401 }
Nate Begemana9795f82005-03-24 04:41:43 +00002402
2403 case ISD::Constant:
2404 switch (N.getValueType()) {
2405 default: assert(0 && "Cannot use constants of this type!");
2406 case MVT::i1:
2407 BuildMI(BB, PPC::LI, 1, Result)
2408 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2409 break;
2410 case MVT::i32:
2411 {
2412 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2413 if (v < 32768 && v >= -32768) {
2414 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2415 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002416 Tmp1 = MakeReg(MVT::i32);
2417 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2418 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002419 }
2420 }
2421 }
2422 return Result;
2423 }
2424
2425 return 0;
2426}
2427
2428void ISel::Select(SDOperand N) {
2429 unsigned Tmp1, Tmp2, Opc;
2430 unsigned opcode = N.getOpcode();
2431
2432 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2433 return; // Already selected.
2434
2435 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002436
Nate Begemana9795f82005-03-24 04:41:43 +00002437 switch (Node->getOpcode()) {
2438 default:
2439 Node->dump(); std::cerr << "\n";
2440 assert(0 && "Node not handled yet!");
2441 case ISD::EntryToken: return; // Noop
2442 case ISD::TokenFactor:
2443 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2444 Select(Node->getOperand(i));
2445 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002446 case ISD::CALLSEQ_START:
2447 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002448 Select(N.getOperand(0));
2449 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002450 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002451 PPC::ADJCALLSTACKUP;
2452 BuildMI(BB, Opc, 1).addImm(Tmp1);
2453 return;
2454 case ISD::BR: {
2455 MachineBasicBlock *Dest =
2456 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002457 Select(N.getOperand(0));
2458 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2459 return;
2460 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002461 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002462 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002463 SelectBranchCC(N);
2464 return;
2465 case ISD::CopyToReg:
2466 Select(N.getOperand(0));
2467 Tmp1 = SelectExpr(N.getOperand(1));
2468 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002469
Nate Begemana9795f82005-03-24 04:41:43 +00002470 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002471 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002472 N.getOperand(1).getValueType() == MVT::f32)
2473 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2474 else
2475 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2476 }
2477 return;
2478 case ISD::ImplicitDef:
2479 Select(N.getOperand(0));
2480 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2481 return;
2482 case ISD::RET:
2483 switch (N.getNumOperands()) {
2484 default:
2485 assert(0 && "Unknown return instruction!");
2486 case 3:
2487 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2488 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002489 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002490 Select(N.getOperand(0));
2491 Tmp1 = SelectExpr(N.getOperand(1));
2492 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002493 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2494 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002495 break;
2496 case 2:
2497 Select(N.getOperand(0));
2498 Tmp1 = SelectExpr(N.getOperand(1));
2499 switch (N.getOperand(1).getValueType()) {
2500 default:
2501 assert(0 && "Unknown return type!");
2502 case MVT::f64:
2503 case MVT::f32:
2504 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2505 break;
2506 case MVT::i32:
2507 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2508 break;
2509 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002510 case 1:
2511 Select(N.getOperand(0));
2512 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002513 }
2514 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2515 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002516 case ISD::TRUNCSTORE:
2517 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002518 {
2519 SDOperand Chain = N.getOperand(0);
2520 SDOperand Value = N.getOperand(1);
2521 SDOperand Address = N.getOperand(2);
2522 Select(Chain);
2523
2524 Tmp1 = SelectExpr(Value); //value
2525
2526 if (opcode == ISD::STORE) {
2527 switch(Value.getValueType()) {
2528 default: assert(0 && "unknown Type in store");
2529 case MVT::i32: Opc = PPC::STW; break;
2530 case MVT::f64: Opc = PPC::STFD; break;
2531 case MVT::f32: Opc = PPC::STFS; break;
2532 }
2533 } else { //ISD::TRUNCSTORE
2534 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2535 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002536 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002537 case MVT::i8: Opc = PPC::STB; break;
2538 case MVT::i16: Opc = PPC::STH; break;
2539 }
2540 }
2541
Nate Begemana7e11a42005-04-01 05:57:17 +00002542 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002543 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002544 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2545 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002546 }
2547 else
2548 {
2549 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002550 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002551 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002552 Opc = IndexedOpForOp(Opc);
2553 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2554 } else {
2555 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2556 }
Nate Begemana9795f82005-03-24 04:41:43 +00002557 }
2558 return;
2559 }
2560 case ISD::EXTLOAD:
2561 case ISD::SEXTLOAD:
2562 case ISD::ZEXTLOAD:
2563 case ISD::LOAD:
2564 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002565 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002566 case ISD::CALL:
2567 case ISD::DYNAMIC_STACKALLOC:
2568 ExprMap.erase(N);
2569 SelectExpr(N);
2570 return;
2571 }
2572 assert(0 && "Should not be reached!");
2573}
2574
2575
2576/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2577/// into a machine code representation using pattern matching and a machine
2578/// description file.
2579///
2580FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002581 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002582}
2583