blob: 288ca195f7a7d5d0843b1497a2e407eb6b35388f [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 Begemana3fd4002005-07-19 16:51:05 +000020#include "llvm/Constants.h"
Nate Begemana9795f82005-03-24 04:41:43 +000021#include "llvm/Function.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begemana9795f82005-03-24 04:41:43 +000023#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
Chris Lattner0561b3f2005-08-02 19:26:06 +000038
Nate Begemana9795f82005-03-24 04:41:43 +000039//===----------------------------------------------------------------------===//
40// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
41namespace {
42 class PPC32TargetLowering : public TargetLowering {
43 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
44 int ReturnAddrIndex; // FrameIndex for return slot.
45 public:
46 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000047 // Fold away setcc operations if possible.
48 setSetCCIsExpensive();
49
Nate Begemana9795f82005-03-24 04:41:43 +000050 // Set up the register classes.
51 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000052 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000053 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000054
Nate Begeman74d73452005-03-31 00:15:26 +000055 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000056 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
57 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
58 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
59
Nate Begeman74d73452005-03-31 00:15:26 +000060 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
61 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
62 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000063
Nate Begeman815d6da2005-04-06 00:25:27 +000064 // PowerPC has no SREM/UREM instructions
65 setOperationAction(ISD::SREM, MVT::i32, Expand);
66 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000067
Chris Lattner32f3cf62005-05-13 16:20:22 +000068 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000069 setOperationAction(ISD::FSIN , MVT::f64, Expand);
70 setOperationAction(ISD::FCOS , 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);
Chris Lattner32f3cf62005-05-13 16:20:22 +000074 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000075
Nate Begemanadeb43d2005-07-20 22:42:00 +000076 // If we're enabling GP optimizations, use hardware square root
Chris Lattner3c304a32005-08-05 22:05:03 +000077 if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
Nate Begemanadeb43d2005-07-20 22:42:00 +000078 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
79 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
80 }
Jeff Cohen00b168892005-07-27 06:12:32 +000081
Nate Begemanc24d4842005-08-10 20:52:09 +000082 // PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000083 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
84 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Nate Begemanc24d4842005-08-10 20:52:09 +000085
86 // PowerPC does not have Select
87 setOperationAction(ISD::SELECT, MVT::i32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f32, Expand);
89 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000090
Chris Lattnercbd06fc2005-04-07 19:41:49 +000091 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000092 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000093 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000094
Nate Begemana9795f82005-03-24 04:41:43 +000095 computeRegisterProperties();
96 }
97
98 /// LowerArguments - This hook must be implemented to indicate how we should
99 /// lower the arguments for the specified function, into the specified DAG.
100 virtual std::vector<SDOperand>
101 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000102
Nate Begemana9795f82005-03-24 04:41:43 +0000103 /// LowerCallTo - This hook lowers an abstract call to a function into an
104 /// actual call.
105 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000106 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000107 bool isTailCall, SDOperand Callee, ArgListTy &Args,
108 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000109
Chris Lattnere0fe2252005-07-05 19:58:54 +0000110 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
111 Value *VAListV, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000112
Nate Begemana9795f82005-03-24 04:41:43 +0000113 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000114 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
115 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000116
Nate Begemana9795f82005-03-24 04:41:43 +0000117 virtual std::pair<SDOperand, SDOperand>
118 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
119 SelectionDAG &DAG);
120 };
121}
122
123
124std::vector<SDOperand>
125PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
126 //
127 // add beautiful description of PPC stack frame format, or at least some docs
128 //
129 MachineFunction &MF = DAG.getMachineFunction();
130 MachineFrameInfo *MFI = MF.getFrameInfo();
131 MachineBasicBlock& BB = MF.front();
132 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000133
134 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000135 // fixed size array of physical args, for the sake of simplicity let the STL
136 // handle tracking them for us.
137 std::vector<unsigned> argVR, argPR, argOp;
138 unsigned ArgOffset = 24;
139 unsigned GPR_remaining = 8;
140 unsigned FPR_remaining = 13;
141 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000142 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000143 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
144 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
145 };
146 static const unsigned FPR[] = {
147 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
148 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
149 };
150
151 // Add DAG nodes to load the arguments... On entry to a function on PPC,
152 // the arguments start at offset 24, although they are likely to be passed
153 // in registers.
154 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
155 SDOperand newroot, argt;
156 unsigned ObjSize;
157 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000158 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000159 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000160
Nate Begemana9795f82005-03-24 04:41:43 +0000161 switch (ObjectVT) {
162 default: assert(0 && "Unhandled argument type!");
163 case MVT::i1:
164 case MVT::i8:
165 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000166 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000167 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000168 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000169 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000170 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000171 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
172 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000173 if (ObjectVT != MVT::i32)
174 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000175 } else {
176 needsLoad = true;
177 }
178 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000179 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000180 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000181 if (GPR_remaining > 0) {
182 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000183 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000184 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
185 // If we have two or more remaining argument registers, then both halves
186 // of the i64 can be sourced from there. Otherwise, the lower half will
187 // have to come off the stack. This can happen when an i64 is preceded
188 // by 28 bytes of arguments.
189 if (GPR_remaining > 1) {
190 MF.addLiveIn(GPR[GPR_idx+1]);
191 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
192 } else {
193 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
194 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000195 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
196 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000197 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000198 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000199 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
200 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000201 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000202 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000203 }
204 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000205 case MVT::f32:
206 case MVT::f64:
207 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
208 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000209 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000210 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000211 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000212 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000213 --FPR_remaining;
214 ++FPR_idx;
215 } else {
216 needsLoad = true;
217 }
218 break;
219 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000220
Nate Begemana9795f82005-03-24 04:41:43 +0000221 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000222 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000223 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000224 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000225 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000226 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000227 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
228 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000229 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000230 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000231 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
232 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000233 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000234
Nate Begemana9795f82005-03-24 04:41:43 +0000235 // Every 4 bytes of argument space consumes one of the GPRs available for
236 // argument passing.
237 if (GPR_remaining > 0) {
238 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
239 GPR_remaining -= delta;
240 GPR_idx += delta;
241 }
242 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000243 if (newroot.Val)
244 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000245
Nate Begemana9795f82005-03-24 04:41:43 +0000246 ArgValues.push_back(argt);
247 }
248
Nate Begemana9795f82005-03-24 04:41:43 +0000249 // If the function takes variable number of arguments, make a frame index for
250 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000251 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000252 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000253 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000254 // If this function is vararg, store any remaining integer argument regs
255 // to their spots on the stack so that they may be loaded by deferencing the
256 // result of va_next.
257 std::vector<SDOperand> MemOps;
258 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000259 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000260 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000261 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000262 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000263 MemOps.push_back(Store);
264 // Increment the address by four for the next argument to store
265 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
266 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
267 }
268 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000269 }
Nate Begemana9795f82005-03-24 04:41:43 +0000270
Nate Begemancd08e4c2005-04-09 20:09:12 +0000271 // Finally, inform the code generator which regs we return values in.
272 switch (getValueType(F.getReturnType())) {
273 default: assert(0 && "Unknown type!");
274 case MVT::isVoid: break;
275 case MVT::i1:
276 case MVT::i8:
277 case MVT::i16:
278 case MVT::i32:
279 MF.addLiveOut(PPC::R3);
280 break;
281 case MVT::i64:
282 MF.addLiveOut(PPC::R3);
283 MF.addLiveOut(PPC::R4);
284 break;
285 case MVT::f32:
286 case MVT::f64:
287 MF.addLiveOut(PPC::F1);
288 break;
289 }
290
Nate Begemana9795f82005-03-24 04:41:43 +0000291 return ArgValues;
292}
293
294std::pair<SDOperand, SDOperand>
295PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000296 const Type *RetTy, bool isVarArg,
Jeff Cohen00b168892005-07-27 06:12:32 +0000297 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000298 SDOperand Callee, ArgListTy &Args,
299 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000300 // args_to_use will accumulate outgoing args for the ISD::CALL case in
301 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000302 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000303
304 // Count how many bytes are to be pushed on the stack, including the linkage
305 // area, and parameter passing area.
306 unsigned NumBytes = 24;
307
308 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000309 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000310 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000311 } else {
312 for (unsigned i = 0, e = Args.size(); i != e; ++i)
313 switch (getValueType(Args[i].second)) {
314 default: assert(0 && "Unknown value type!");
315 case MVT::i1:
316 case MVT::i8:
317 case MVT::i16:
318 case MVT::i32:
319 case MVT::f32:
320 NumBytes += 4;
321 break;
322 case MVT::i64:
323 case MVT::f64:
324 NumBytes += 8;
325 break;
326 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000327
328 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000329 // plus 32 bytes of argument space in case any called code gets funky on us.
Chris Lattner0561b3f2005-08-02 19:26:06 +0000330 // (Required by ABI to support var arg)
Nate Begeman307e7442005-03-26 01:28:53 +0000331 if (NumBytes < 56) NumBytes = 56;
332
333 // Adjust the stack pointer for the new arguments...
334 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000335 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000336 DAG.getConstant(NumBytes, getPointerTy()));
337
338 // Set up a copy of the stack pointer for use loading and storing any
339 // arguments that may not fit in the registers available for argument
340 // passing.
341 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
342 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000343
Nate Begeman307e7442005-03-26 01:28:53 +0000344 // Figure out which arguments are going to go in registers, and which in
345 // memory. Also, if this is a vararg function, floating point operations
346 // must be stored to our stack, and loaded into integer regs as well, if
347 // any integer regs are available for argument passing.
348 unsigned ArgOffset = 24;
349 unsigned GPR_remaining = 8;
350 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000351
Nate Begeman74d73452005-03-31 00:15:26 +0000352 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000353 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
354 // PtrOff will be used to store the current argument to the stack if a
355 // register cannot be found for it.
356 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
357 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000358 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000359
Nate Begemanf7e43382005-03-26 07:46:36 +0000360 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000361 default: assert(0 && "Unexpected ValueType for argument!");
362 case MVT::i1:
363 case MVT::i8:
364 case MVT::i16:
365 // Promote the integer to 32 bits. If the input type is signed use a
366 // sign extend, otherwise use a zero extend.
367 if (Args[i].second->isSigned())
368 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
369 else
370 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
371 // FALL THROUGH
372 case MVT::i32:
373 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000374 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000375 --GPR_remaining;
376 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000377 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000378 Args[i].first, PtrOff,
379 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000380 }
381 ArgOffset += 4;
382 break;
383 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000384 // If we have one free GPR left, we can place the upper half of the i64
385 // in it, and store the other half to the stack. If we have two or more
386 // free GPRs, then we can pass both halves of the i64 in registers.
387 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000388 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000389 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000390 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000391 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000392 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000393 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000394 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000395 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000396 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000397 } else {
398 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
399 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000400 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000401 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000402 }
Nate Begeman307e7442005-03-26 01:28:53 +0000403 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000404 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000405 Args[i].first, PtrOff,
406 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000407 }
408 ArgOffset += 8;
409 break;
410 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000411 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000412 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000413 args_to_use.push_back(Args[i].first);
414 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000415 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000416 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000417 Args[i].first, PtrOff,
418 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000419 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000420 // Float varargs are always shadowed in available integer registers
421 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000422 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
423 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000424 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000425 args_to_use.push_back(Load);
426 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000427 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000428 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000429 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
430 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000431 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
432 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000433 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000434 args_to_use.push_back(Load);
435 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000436 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000437 } else {
438 // If we have any FPRs remaining, we may also have GPRs remaining.
439 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
440 // GPRs.
441 if (GPR_remaining > 0) {
442 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
443 --GPR_remaining;
444 }
445 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
446 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
447 --GPR_remaining;
448 }
Nate Begeman74d73452005-03-31 00:15:26 +0000449 }
Nate Begeman307e7442005-03-26 01:28:53 +0000450 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000451 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000452 Args[i].first, PtrOff,
453 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000454 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000455 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000456 break;
457 }
Nate Begemana9795f82005-03-24 04:41:43 +0000458 }
Nate Begeman74d73452005-03-31 00:15:26 +0000459 if (!MemOps.empty())
460 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000461 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000462
Nate Begemana9795f82005-03-24 04:41:43 +0000463 std::vector<MVT::ValueType> RetVals;
464 MVT::ValueType RetTyVT = getValueType(RetTy);
465 if (RetTyVT != MVT::isVoid)
466 RetVals.push_back(RetTyVT);
467 RetVals.push_back(MVT::Other);
468
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000469 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000470 Chain, Callee, args_to_use), 0);
471 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000472 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000473 DAG.getConstant(NumBytes, getPointerTy()));
474 return std::make_pair(TheCall, Chain);
475}
476
Chris Lattnere0fe2252005-07-05 19:58:54 +0000477SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
478 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000479 // vastart just stores the address of the VarArgsFrameIndex slot into the
480 // memory location argument.
481 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000482 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
483 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000484}
485
Chris Lattnere0fe2252005-07-05 19:58:54 +0000486std::pair<SDOperand,SDOperand>
487PPC32TargetLowering::LowerVAArg(SDOperand Chain,
488 SDOperand VAListP, Value *VAListV,
489 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000490 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000491
492 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000493 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
494 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000495 unsigned Amt;
496 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
497 Amt = 4;
498 else {
499 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
500 "Other types should have been promoted for varargs!");
501 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000502 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000503 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
504 DAG.getConstant(Amt, VAList.getValueType()));
505 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000506 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000507 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000508}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000509
Nate Begemana9795f82005-03-24 04:41:43 +0000510
511std::pair<SDOperand, SDOperand> PPC32TargetLowering::
512LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
513 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000514 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000515 abort();
516}
517
518namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000519Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000520Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000521Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Chris Lattner3c304a32005-08-05 22:05:03 +0000522
Nate Begemana9795f82005-03-24 04:41:43 +0000523//===--------------------------------------------------------------------===//
524/// ISel - PPC32 specific code to select PPC32 machine instructions for
525/// SelectionDAG operations.
526//===--------------------------------------------------------------------===//
527class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000528 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000529 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
530 // for sdiv and udiv until it is put into the future
531 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000532
Nate Begemana9795f82005-03-24 04:41:43 +0000533 /// ExprMap - As shared expressions are codegen'd, we keep track of which
534 /// vreg the value is produced in, so we only emit one copy of each compiled
535 /// tree.
536 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000537
538 unsigned GlobalBaseReg;
539 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000540 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000541public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000542 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
543 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000544
Nate Begemanc7b09f12005-03-25 08:34:25 +0000545 /// runOnFunction - Override this function in order to reset our per-function
546 /// variables.
547 virtual bool runOnFunction(Function &Fn) {
548 // Make sure we re-emit a set of the global base reg if necessary
549 GlobalBaseInitialized = false;
550 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000551 }
552
Nate Begemana9795f82005-03-24 04:41:43 +0000553 /// InstructionSelectBasicBlock - This callback is invoked by
554 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
555 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
556 DEBUG(BB->dump());
557 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000558 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000559 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000560
Nate Begemana9795f82005-03-24 04:41:43 +0000561 // Clear state used for selection.
562 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000563 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000564 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000565
Chris Lattner54abfc52005-08-11 17:15:31 +0000566 // convenience functions for virtual register creation
567 inline unsigned MakeIntReg() {
568 return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass);
569 }
570 inline unsigned MakeFPReg() {
571 return RegMap->createVirtualRegister(PPC32::FPRCRegisterClass);
572 }
573
Nate Begeman815d6da2005-04-06 00:25:27 +0000574 // dag -> dag expanders for integer divide by constant
575 SDOperand BuildSDIVSequence(SDOperand N);
576 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000577
Nate Begemandffcfcc2005-04-01 00:32:34 +0000578 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000579 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemanc24d4842005-08-10 20:52:09 +0000580 void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000581 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000582 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begemanc24d4842005-08-10 20:52:09 +0000583 unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
Chris Lattnerb4138c42005-08-10 18:11:33 +0000584 bool SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000585 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +0000586 bool IsArithmetic = false, bool Negate = false);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000587 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000588 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000589
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000590 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000591 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000592
593 virtual const char *getPassName() const {
594 return "PowerPC Pattern Instruction Selection";
595 }
Nate Begemana9795f82005-03-24 04:41:43 +0000596};
597
Chris Lattner02efa6c2005-08-08 21:08:09 +0000598// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
599// any number of 0s on either side. The 1s are allowed to wrap from LSB to
600// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
601// not, since all 1s are not contiguous.
602static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
603 if (isShiftedMask_32(Val)) {
604 // look for the first non-zero bit
605 MB = CountLeadingZeros_32(Val);
606 // look for the first zero bit after the run of ones
607 ME = CountLeadingZeros_32((Val - 1) ^ Val);
608 return true;
609 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
610 // effectively look for the first zero bit
611 ME = CountLeadingZeros_32(Val) - 1;
612 // effectively look for the first one bit after the run of zeros
613 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
614 return true;
615 }
616 // no run present
617 return false;
618}
619
Chris Lattnercf1cf182005-08-08 21:10:27 +0000620// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
621// and mask opcode and mask operation.
622static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
623 bool IsShiftMask,
624 unsigned &SH, unsigned &MB, unsigned &ME) {
625 if (Shift > 31) return false;
626 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
627
628 if (Opcode == ISD::SHL) { // shift left
629 // apply shift to mask if it comes first
630 if (IsShiftMask) Mask = Mask << Shift;
631 // determine which bits are made indeterminant by shift
632 Indeterminant = ~(0xFFFFFFFFu << Shift);
633 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
634 // apply shift to mask if it comes first
635 if (IsShiftMask) Mask = Mask >> Shift;
636 // determine which bits are made indeterminant by shift
637 Indeterminant = ~(0xFFFFFFFFu >> Shift);
638 // adjust for the left rotate
639 Shift = 32 - Shift;
640 }
641
642 // if the mask doesn't intersect any Indeterminant bits
643 if (!(Mask & Indeterminant)) {
644 SH = Shift;
645 // make sure the mask is still a mask (wrap arounds may not be)
646 return isRunOfOnes(Mask, MB, ME);
647 }
648
649 // can't do it
650 return false;
651}
652
Chris Lattner59b21c22005-08-09 18:29:55 +0000653// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000654// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000655static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000656 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000657 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000658 // retrieve value
Chris Lattner59b21c22005-08-09 18:29:55 +0000659 Imm = (unsigned)CN->getSignExtended();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000660 // passes muster
661 return true;
662 }
663 // not a constant
664 return false;
665}
666
667// isOprShiftImm - Returns true if the specified operand is a shift opcode with
668// a immediate shift count less than 32.
669static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
670 Opc = N.getOpcode();
671 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000672 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000673}
674
675// isOprNot - Returns true if the specified operand is an xor with immediate -1.
676static bool isOprNot(SDOperand N) {
677 unsigned Imm;
678 return N.getOpcode() == ISD::XOR &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000679 isIntImmediate(N.getOperand(1), Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000680}
681
682// Immediate constant composers.
683// Lo16 - grabs the lo 16 bits from a 32 bit constant.
684// Hi16 - grabs the hi 16 bits from a 32 bit constant.
685// HA16 - computes the hi bits required if the lo bits are add/subtracted in
686// arithmethically.
687static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
688static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
689static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
690
Nate Begemanc7bd4822005-04-11 06:34:10 +0000691/// NodeHasRecordingVariant - If SelectExpr can always produce code for
692/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
693/// return false.
694static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
695 switch(NodeOpcode) {
696 default: return false;
697 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000698 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000699 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000700 }
701}
702
Nate Begeman3e897162005-03-31 23:55:40 +0000703/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
Nate Begemanc24d4842005-08-10 20:52:09 +0000704/// to Condition.
705static unsigned getBCCForSetCC(ISD::CondCode CC) {
706 switch (CC) {
Nate Begeman3e897162005-03-31 23:55:40 +0000707 default: assert(0 && "Unknown condition!"); abort();
708 case ISD::SETEQ: return PPC::BEQ;
709 case ISD::SETNE: return PPC::BNE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000710 case ISD::SETULT:
Nate Begeman3e897162005-03-31 23:55:40 +0000711 case ISD::SETLT: return PPC::BLT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000712 case ISD::SETULE:
Nate Begeman3e897162005-03-31 23:55:40 +0000713 case ISD::SETLE: return PPC::BLE;
Nate Begemanc24d4842005-08-10 20:52:09 +0000714 case ISD::SETUGT:
Nate Begeman3e897162005-03-31 23:55:40 +0000715 case ISD::SETGT: return PPC::BGT;
Nate Begemanc24d4842005-08-10 20:52:09 +0000716 case ISD::SETUGE:
Nate Begeman3e897162005-03-31 23:55:40 +0000717 case ISD::SETGE: return PPC::BGE;
718 }
Nate Begeman04730362005-04-01 04:45:11 +0000719 return 0;
720}
721
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000722/// getCROpForOp - Return the condition register opcode (or inverted opcode)
723/// associated with the SelectionDAG opcode.
724static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
725 switch (Opcode) {
726 default: assert(0 && "Unknown opcode!"); abort();
727 case ISD::AND:
728 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
729 if (!Inv1 && !Inv2) return PPC::CRAND;
730 if (Inv1 ^ Inv2) return PPC::CRANDC;
731 case ISD::OR:
732 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
733 if (!Inv1 && !Inv2) return PPC::CROR;
734 if (Inv1 ^ Inv2) return PPC::CRORC;
735 }
736 return 0;
737}
738
739/// getCRIdxForSetCC - Return the index of the condition register field
740/// associated with the SetCC condition, and whether or not the field is
741/// treated as inverted. That is, lt = 0; ge = 0 inverted.
Nate Begemanc24d4842005-08-10 20:52:09 +0000742static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
743 switch (CC) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000744 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000745 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000746 case ISD::SETLT: Inv = false; return 0;
747 case ISD::SETUGE:
748 case ISD::SETGE: Inv = true; return 0;
749 case ISD::SETUGT:
750 case ISD::SETGT: Inv = false; return 1;
751 case ISD::SETULE:
752 case ISD::SETLE: Inv = true; return 1;
753 case ISD::SETEQ: Inv = false; return 2;
754 case ISD::SETNE: Inv = true; return 2;
755 }
756 return 0;
757}
758
Nate Begeman04730362005-04-01 04:45:11 +0000759/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
760/// and store immediate instructions.
761static unsigned IndexedOpForOp(unsigned Opcode) {
762 switch(Opcode) {
763 default: assert(0 && "Unknown opcode!"); abort();
764 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
765 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
766 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
767 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
768 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
769 case PPC::LFD: return PPC::LFDX;
770 }
771 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000772}
Nate Begeman815d6da2005-04-06 00:25:27 +0000773
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000774// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000775// a multiply.
776struct ms {
777 int m; // magic number
778 int s; // shift amount
779};
780
781struct mu {
782 unsigned int m; // magic number
783 int a; // add indicator
784 int s; // shift amount
785};
786
787/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000788/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000789/// or -1.
790static struct ms magic(int d) {
791 int p;
792 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000793 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000794 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000795
Nate Begeman815d6da2005-04-06 00:25:27 +0000796 ad = abs(d);
797 t = two31 + ((unsigned int)d >> 31);
798 anc = t - 1 - t%ad; // absolute value of nc
799 p = 31; // initialize p
800 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
801 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
802 q2 = two31/ad; // initialize q2 = 2p/abs(d)
803 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
804 do {
805 p = p + 1;
806 q1 = 2*q1; // update q1 = 2p/abs(nc)
807 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
808 if (r1 >= anc) { // must be unsigned comparison
809 q1 = q1 + 1;
810 r1 = r1 - anc;
811 }
812 q2 = 2*q2; // update q2 = 2p/abs(d)
813 r2 = 2*r2; // update r2 = rem(2p/abs(d))
814 if (r2 >= ad) { // must be unsigned comparison
815 q2 = q2 + 1;
816 r2 = r2 - ad;
817 }
818 delta = ad - r2;
819 } while (q1 < delta || (q1 == delta && r1 == 0));
820
821 mag.m = q2 + 1;
822 if (d < 0) mag.m = -mag.m; // resulting magic number
823 mag.s = p - 32; // resulting shift
824 return mag;
825}
826
827/// magicu - calculate the magic numbers required to codegen an integer udiv as
828/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
829static struct mu magicu(unsigned d)
830{
831 int p;
832 unsigned int nc, delta, q1, r1, q2, r2;
833 struct mu magu;
834 magu.a = 0; // initialize "add" indicator
835 nc = - 1 - (-d)%d;
836 p = 31; // initialize p
837 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
838 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
839 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
840 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
841 do {
842 p = p + 1;
843 if (r1 >= nc - r1 ) {
844 q1 = 2*q1 + 1; // update q1
845 r1 = 2*r1 - nc; // update r1
846 }
847 else {
848 q1 = 2*q1; // update q1
849 r1 = 2*r1; // update r1
850 }
851 if (r2 + 1 >= d - r2) {
852 if (q2 >= 0x7FFFFFFF) magu.a = 1;
853 q2 = 2*q2 + 1; // update q2
854 r2 = 2*r2 + 1 - d; // update r2
855 }
856 else {
857 if (q2 >= 0x80000000) magu.a = 1;
858 q2 = 2*q2; // update q2
859 r2 = 2*r2 + 1; // update r2
860 }
861 delta = d - 1 - r2;
862 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
863 magu.m = q2 + 1; // resulting magic number
864 magu.s = p - 32; // resulting shift
865 return magu;
866}
867}
868
869/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
870/// return a DAG expression to select that will generate the same value by
871/// multiplying by a magic number. See:
872/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
873SDOperand ISel::BuildSDIVSequence(SDOperand N) {
874 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
875 ms magics = magic(d);
876 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000877 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000878 ISelDAG->getConstant(magics.m, MVT::i32));
879 // If d > 0 and m < 0, add the numerator
880 if (d > 0 && magics.m < 0)
881 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
882 // If d < 0 and m > 0, subtract the numerator.
883 if (d < 0 && magics.m > 0)
884 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
885 // Shift right algebraic if shift value is nonzero
886 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000887 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000888 ISelDAG->getConstant(magics.s, MVT::i32));
889 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000890 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000891 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000892 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000893}
894
895/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
896/// return a DAG expression to select that will generate the same value by
897/// multiplying by a magic number. See:
898/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
899SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000900 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000901 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
902 mu magics = magicu(d);
903 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000904 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000905 ISelDAG->getConstant(magics.m, MVT::i32));
906 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000907 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000908 ISelDAG->getConstant(magics.s, MVT::i32));
909 } else {
910 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000911 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000912 ISelDAG->getConstant(1, MVT::i32));
913 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000914 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000915 ISelDAG->getConstant(magics.s-1, MVT::i32));
916 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000917 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000918}
919
Nate Begemanc7b09f12005-03-25 08:34:25 +0000920/// getGlobalBaseReg - Output the instructions required to put the
921/// base address to use for accessing globals into a register.
922///
923unsigned ISel::getGlobalBaseReg() {
924 if (!GlobalBaseInitialized) {
925 // Insert the set of GlobalBaseReg into the first MBB of the function
926 MachineBasicBlock &FirstMBB = BB->getParent()->front();
927 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Chris Lattner54abfc52005-08-11 17:15:31 +0000928 GlobalBaseReg = MakeIntReg();
Nate Begemanc7b09f12005-03-25 08:34:25 +0000929 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
930 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
931 GlobalBaseInitialized = true;
932 }
933 return GlobalBaseReg;
934}
935
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000936/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000937/// Constant Pool. Optionally takes a register in which to load the value.
938unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000939 unsigned Tmp1 = MakeIntReg();
940 if (0 == Result) Result = MakeFPReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000941 MachineConstantPool *CP = BB->getParent()->getConstantPool();
942 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
943 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000944 if (PICEnabled)
945 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
946 .addConstantPoolIndex(CPI);
947 else
948 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000949 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
950 return Result;
951}
952
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000953/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000954/// Inv is true, then invert the result.
Nate Begemanc24d4842005-08-10 20:52:09 +0000955void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){
956 bool Inv;
Chris Lattner54abfc52005-08-11 17:15:31 +0000957 unsigned IntCR = MakeIntReg();
Nate Begemanc24d4842005-08-10 20:52:09 +0000958 unsigned Idx = getCRIdxForSetCC(CC, Inv);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000959 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000960 bool GPOpt =
961 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
962 BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000963 if (Inv) {
Chris Lattner54abfc52005-08-11 17:15:31 +0000964 unsigned Tmp1 = MakeIntReg();
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000965 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
966 .addImm(31).addImm(31);
967 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
968 } else {
969 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
970 .addImm(31).addImm(31);
971 }
972}
973
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000974/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000975/// the rotate left word immediate then mask insert (rlwimi) instruction.
976/// Returns true on success, false if the caller still needs to select OR.
977///
978/// Patterns matched:
979/// 1. or shl, and 5. or and, and
980/// 2. or and, shl 6. or shl, shr
981/// 3. or shr, and 7. or shr, shl
982/// 4. or and, shr
983bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000984 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000985 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Chris Lattner2b48bc62005-08-11 17:56:50 +0000986 unsigned Value;
Jeff Cohen00b168892005-07-27 06:12:32 +0000987
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000988 SDOperand Op0 = OR.getOperand(0);
989 SDOperand Op1 = OR.getOperand(1);
990
991 unsigned Op0Opc = Op0.getOpcode();
992 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000993
Nate Begeman7ddecb42005-04-06 23:51:40 +0000994 // Verify that we have the correct opcodes
995 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
996 return false;
997 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
998 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000999
Nate Begeman7ddecb42005-04-06 23:51:40 +00001000 // Generate Mask value for Target
Chris Lattner2b48bc62005-08-11 17:56:50 +00001001 if (isIntImmediate(Op0.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001002 switch(Op0Opc) {
Chris Lattner2b48bc62005-08-11 17:56:50 +00001003 case ISD::SHL: TgtMask <<= Value; break;
1004 case ISD::SRL: TgtMask >>= Value; break;
1005 case ISD::AND: TgtMask &= Value; break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001006 }
1007 } else {
1008 return false;
1009 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001010
Nate Begeman7ddecb42005-04-06 23:51:40 +00001011 // Generate Mask value for Insert
Chris Lattner2b48bc62005-08-11 17:56:50 +00001012 if (isIntImmediate(Op1.getOperand(1), Value)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001013 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001014 case ISD::SHL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001015 Amount = Value;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001016 InsMask <<= Amount;
1017 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001018 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001019 case ISD::SRL:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001020 Amount = Value;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001021 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001022 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001023 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001024 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001025 case ISD::AND:
Chris Lattner2b48bc62005-08-11 17:56:50 +00001026 InsMask &= Value;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001027 break;
1028 }
1029 } else {
1030 return false;
1031 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001032
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001033 unsigned Tmp3 = 0;
1034
1035 // If both of the inputs are ANDs and one of them has a logical shift by
1036 // constant as its input, make that the inserted value so that we can combine
1037 // the shift into the rotate part of the rlwimi instruction
1038 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001039 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001040 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +00001041 if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001042 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +00001043 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001044 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1045 }
1046 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1047 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Chris Lattner2b48bc62005-08-11 17:56:50 +00001048 if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) {
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001049 std::swap(Op0, Op1);
1050 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +00001051 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Chris Lattner2b48bc62005-08-11 17:56:50 +00001052 Value : 32 - Value;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001053 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1054 }
1055 }
1056 }
1057
Nate Begeman7ddecb42005-04-06 23:51:40 +00001058 // Verify that the Target mask and Insert mask together form a full word mask
1059 // and that the Insert mask is a run of set bits (which implies both are runs
1060 // of set bits). Given that, Select the arguments and generate the rlwimi
1061 // instruction.
1062 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +00001063 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001064 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001065 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001066 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1067 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001068 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001069 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001070 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1071 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1072 .addImm(0).addImm(31);
1073 return true;
1074 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001075 if (Op0Opc == ISD::AND && fullMask)
1076 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001077 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001078 Tmp1 = SelectExpr(Op0);
1079 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001080 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1081 .addImm(Amount).addImm(MB).addImm(ME);
1082 return true;
1083 }
1084 return false;
1085}
1086
Nate Begeman3664cef2005-04-13 22:14:14 +00001087/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1088/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1089/// wider than the implicit mask, then we can get rid of the AND and let the
1090/// shift do the mask.
1091unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Chris Lattner8fd19802005-08-08 21:12:35 +00001092 unsigned C, MB, ME;
Nate Begeman3664cef2005-04-13 22:14:14 +00001093 if (N.getOpcode() == ISD::AND &&
Chris Lattner59b21c22005-08-09 18:29:55 +00001094 isIntImmediate(N.getOperand(1), C) && isRunOfOnes(C, MB, ME) &&
Chris Lattner8fd19802005-08-08 21:12:35 +00001095 MB <= 26 && ME == 31)
Nate Begeman3664cef2005-04-13 22:14:14 +00001096 return SelectExpr(N.getOperand(0));
1097 else
1098 return SelectExpr(N);
1099}
1100
Nate Begemanc24d4842005-08-10 20:52:09 +00001101unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001102 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001103 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001104 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001105 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001106
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001107 // Allocate a condition register for this expression
1108 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001109
Nate Begemanc24d4842005-08-10 20:52:09 +00001110 // Use U to determine whether the SETCC immediate range is signed or not.
1111 bool U = ISD::isUnsignedIntSetCC(CC);
1112 if (isIntImmediate(RHS, Tmp2) &&
1113 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
1114 Tmp2 = Lo16(Tmp2);
1115 // For comparisons against zero, we can implicity set CR0 if a recording
1116 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1117 // operand zero of the SetCC node is available.
1118 if (Tmp2 == 0 &&
1119 NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) {
1120 RecordSuccess = false;
1121 Tmp1 = SelectExpr(LHS, true);
1122 if (RecordSuccess) {
1123 ++Recorded;
1124 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1125 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001126 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001127 AlreadySelected = true;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001128 }
Nate Begemanc24d4842005-08-10 20:52:09 +00001129 // If we could not implicitly set CR0, then emit a compare immediate
1130 // instead.
1131 if (!AlreadySelected) Tmp1 = SelectExpr(LHS);
1132 if (U)
1133 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1134 else
1135 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001136 } else {
Nate Begemanc24d4842005-08-10 20:52:09 +00001137 bool IsInteger = MVT::isInteger(LHS.getValueType());
1138 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1139 Tmp1 = SelectExpr(LHS);
1140 Tmp2 = SelectExpr(RHS);
1141 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001142 }
1143 return Result;
1144}
1145
Nate Begemand3ded2d2005-08-08 22:22:56 +00001146/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001147unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001148{
Nate Begeman96fc6812005-03-31 02:05:53 +00001149 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001150 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001151 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +00001152 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +00001153 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001154 if (isFrame) {
1155 ++FrameOff;
1156 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
1157 return 1;
1158 } else {
1159 Reg = SelectExpr(N.getOperand(0));
1160 return 0;
1161 }
1162 } else {
1163 Reg = SelectExpr(N.getOperand(0));
1164 offset = SelectExpr(N.getOperand(1));
1165 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001166 }
Nate Begeman04730362005-04-01 04:45:11 +00001167 }
Nate Begemand3ded2d2005-08-08 22:22:56 +00001168 // Now check if we're dealing with a global, and whether or not we should emit
1169 // an optimized load or store for statics.
1170 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
1171 GlobalValue *GV = GN->getGlobal();
1172 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
Chris Lattner54abfc52005-08-11 17:15:31 +00001173 unsigned GlobalHi = MakeIntReg();
Nate Begemand3ded2d2005-08-08 22:22:56 +00001174 if (PICEnabled)
1175 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
1176 .addGlobalAddress(GV);
1177 else
1178 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
1179 Reg = GlobalHi;
1180 offset = 0;
1181 return 3;
1182 }
1183 }
Nate Begemana9795f82005-03-24 04:41:43 +00001184 Reg = SelectExpr(N);
1185 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001186 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +00001187}
1188
1189void ISel::SelectBranchCC(SDOperand N)
1190{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001191 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001192 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001193
Nate Begemana9795f82005-03-24 04:41:43 +00001194 Select(N.getOperand(0)); //chain
Nate Begemanc24d4842005-08-10 20:52:09 +00001195
1196 // FIXME: Until we have Branch_CC and Branch_Twoway_CC, we're going to have to
1197 // Fake it up by hand by checking to see if op 1 is a SetCC, or a boolean.
1198 unsigned CCReg;
1199 ISD::CondCode CC;
1200 SDOperand Cond = N.getOperand(1);
1201 if (Cond.getOpcode() == ISD::SETCC) {
1202 CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
1203 CCReg = SelectCC(Cond.getOperand(0), Cond.getOperand(1), CC);
1204 } else {
1205 CC = ISD::SETNE;
1206 CCReg = SelectCC(Cond, ISelDAG->getConstant(0, Cond.getValueType()), CC);
1207 }
1208 unsigned Opc = getBCCForSetCC(CC);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001209
Nate Begeman439009c2005-06-15 18:22:43 +00001210 // Iterate to the next basic block
1211 ilist<MachineBasicBlock>::iterator It = BB;
1212 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001213
1214 // If this is a two way branch, then grab the fallthrough basic block argument
1215 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1216 // if necessary by the branch selection pass. Otherwise, emit a standard
1217 // conditional branch.
1218 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001219 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001220 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1221 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001222 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001223 .addMBB(Dest).addMBB(Fallthrough);
1224 if (Fallthrough != It)
1225 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1226 } else {
1227 if (Fallthrough != It) {
1228 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001229 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001230 .addMBB(Fallthrough).addMBB(Dest);
1231 }
1232 }
1233 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001234 // If the fallthrough path is off the end of the function, which would be
1235 // undefined behavior, set it to be the same as the current block because
1236 // we have nothing better to set it to, and leaving it alone will cause the
1237 // PowerPC Branch Selection pass to crash.
1238 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001239 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001240 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001241 }
Nate Begemana9795f82005-03-24 04:41:43 +00001242 return;
1243}
1244
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001245// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
Chris Lattnerb4138c42005-08-10 18:11:33 +00001246bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result,
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001247 unsigned OCHi, unsigned OCLo,
Chris Lattnerb4138c42005-08-10 18:11:33 +00001248 bool IsArithmetic, bool Negate) {
1249 // check constant
1250 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1));
1251 // exit if not a constant
1252 if (!CN) return false;
1253 // extract immediate
1254 unsigned C = (unsigned)CN->getSignExtended();
1255 // negate if required (ISD::SUB)
1256 if (Negate) C = -C;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001257 // get the hi and lo portions of constant
1258 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
1259 unsigned Lo = Lo16(C);
1260 // assume no intermediate result from lo instruction (same as final result)
1261 unsigned Tmp = Result;
1262 // check if two instructions are needed
1263 if (Hi && Lo) {
1264 // exit if usage indicates it would be better to load immediate into a
1265 // register
Chris Lattnerb4138c42005-08-10 18:11:33 +00001266 if (CN->use_size() > 2) return false;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001267 // need intermediate result for two instructions
Chris Lattner54abfc52005-08-11 17:15:31 +00001268 Tmp = MakeIntReg();
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001269 }
1270 // get first operand
1271 unsigned Opr0 = SelectExpr(N.getOperand(0));
1272 // is a lo instruction needed
1273 if (Lo) {
1274 // generate instruction for hi portion
1275 const MachineInstrBuilder &MIBLo = BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0);
1276 if (IsArithmetic) MIBLo.addSImm(Lo); else MIBLo.addImm(Lo);
1277 // need to switch out first operand for hi instruction
1278 Opr0 = Tmp;
1279 }
1280 // is a ho instruction needed
1281 if (Hi) {
1282 // generate instruction for hi portion
1283 const MachineInstrBuilder &MIBHi = BuildMI(BB, OCHi, 2, Result).addReg(Opr0);
1284 if (IsArithmetic) MIBHi.addSImm(Hi); else MIBHi.addImm(Hi);
1285 }
1286 return true;
1287}
1288
Nate Begemanc7bd4822005-04-11 06:34:10 +00001289unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001290 unsigned Result;
1291 unsigned Tmp1, Tmp2, Tmp3;
1292 unsigned Opc = 0;
1293 unsigned opcode = N.getOpcode();
1294
1295 SDNode *Node = N.Val;
1296 MVT::ValueType DestType = N.getValueType();
1297
Nate Begemana43b1762005-06-14 03:55:23 +00001298 if (Node->getOpcode() == ISD::CopyFromReg &&
Chris Lattner988b1dd2005-07-28 05:23:43 +00001299 (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1300 cast<RegSDNode>(Node)->getReg() == PPC::R1))
Nate Begemana43b1762005-06-14 03:55:23 +00001301 // Just use the specified register as our input.
1302 return cast<RegSDNode>(Node)->getReg();
1303
Nate Begemana9795f82005-03-24 04:41:43 +00001304 unsigned &Reg = ExprMap[N];
1305 if (Reg) return Reg;
1306
Nate Begeman27eeb002005-04-02 05:59:34 +00001307 switch (N.getOpcode()) {
1308 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001309 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001310 MakeReg(N.getValueType()) : 1;
1311 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001312 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001313 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001314 // If this is a call instruction, make sure to prepare ALL of the result
1315 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001316 if (Node->getNumValues() == 1)
1317 Reg = Result = 1; // Void call, just a chain.
1318 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001319 Result = MakeReg(Node->getValueType(0));
1320 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001321 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001322 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001323 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001324 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001325 break;
1326 case ISD::ADD_PARTS:
1327 case ISD::SUB_PARTS:
1328 case ISD::SHL_PARTS:
1329 case ISD::SRL_PARTS:
1330 case ISD::SRA_PARTS:
1331 Result = MakeReg(Node->getValueType(0));
1332 ExprMap[N.getValue(0)] = Result;
1333 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1334 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1335 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001336 }
1337
Nate Begemana9795f82005-03-24 04:41:43 +00001338 switch (opcode) {
1339 default:
1340 Node->dump();
Nate Begemanc24d4842005-08-10 20:52:09 +00001341 assert(0 && "\nNode not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001342 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001343 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1344 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001345 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001346 // Generate both result values. FIXME: Need a better commment here?
1347 if (Result != 1)
1348 ExprMap[N.getValue(1)] = 1;
1349 else
1350 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1351
1352 // FIXME: We are currently ignoring the requested alignment for handling
1353 // greater than the stack alignment. This will need to be revisited at some
1354 // point. Align = N.getOperand(2);
1355 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1356 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1357 std::cerr << "Cannot allocate stack object with greater alignment than"
1358 << " the stack alignment yet!";
1359 abort();
1360 }
1361 Select(N.getOperand(0));
1362 Tmp1 = SelectExpr(N.getOperand(1));
1363 // Subtract size from stack pointer, thereby allocating some space.
1364 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1365 // Put a pointer to the space into the result register by copying the SP
1366 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1367 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001368
1369 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001370 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
Chris Lattner54abfc52005-08-11 17:15:31 +00001371 Tmp2 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +00001372 if (PICEnabled)
1373 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
1374 .addConstantPoolIndex(Tmp1);
1375 else
1376 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001377 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1378 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001379
1380 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001381 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001382 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001383 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001384
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001385 case ISD::GlobalAddress: {
1386 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Chris Lattner54abfc52005-08-11 17:15:31 +00001387 Tmp1 = MakeIntReg();
Nate Begeman2497e632005-07-21 20:44:43 +00001388 if (PICEnabled)
1389 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1390 .addGlobalAddress(GV);
1391 else
Chris Lattner4015ea82005-07-28 04:42:11 +00001392 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001393 if (GV->hasWeakLinkage() || GV->isExternal()) {
1394 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1395 } else {
1396 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1397 }
1398 return Result;
1399 }
1400
Nate Begeman5e966612005-03-24 06:28:42 +00001401 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001402 case ISD::EXTLOAD:
1403 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001404 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001405 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001406 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001407 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001408
Nate Begeman5e966612005-03-24 06:28:42 +00001409 // Make sure we generate both values.
1410 if (Result != 1)
1411 ExprMap[N.getValue(1)] = 1; // Generate the token
1412 else
1413 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1414
1415 SDOperand Chain = N.getOperand(0);
1416 SDOperand Address = N.getOperand(1);
1417 Select(Chain);
1418
Nate Begeman9db505c2005-03-28 19:36:43 +00001419 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001420 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001421 case MVT::i1: Opc = PPC::LBZ; break;
1422 case MVT::i8: Opc = PPC::LBZ; break;
1423 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1424 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001425 case MVT::f32: Opc = PPC::LFS; break;
1426 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001427 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001428
Nate Begeman74d73452005-03-31 00:15:26 +00001429 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner54abfc52005-08-11 17:15:31 +00001430 Tmp1 = MakeIntReg();
Nate Begeman74d73452005-03-31 00:15:26 +00001431 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +00001432 if (PICEnabled)
1433 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1434 .addConstantPoolIndex(CPI);
1435 else
1436 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +00001437 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001438 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001439 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1440 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001441 } else {
1442 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001443 switch(SelectAddr(Address, Tmp1, offset)) {
1444 default: assert(0 && "Unhandled return value from SelectAddr");
1445 case 0: // imm offset, no frame, no index
1446 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1447 break;
1448 case 1: // imm offset + frame index
1449 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
1450 break;
1451 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +00001452 Opc = IndexedOpForOp(Opc);
1453 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001454 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001455 case 3: {
1456 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1457 GlobalValue *GV = GN->getGlobal();
1458 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1459 }
Nate Begeman04730362005-04-01 04:45:11 +00001460 }
Nate Begeman5e966612005-03-24 06:28:42 +00001461 }
1462 return Result;
1463 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001464
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001465 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001466 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001467 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001468 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001469 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1470 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1471 };
1472 static const unsigned FPR[] = {
1473 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1474 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1475 };
1476
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001477 // Lower the chain for this call.
1478 Select(N.getOperand(0));
1479 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001480
Nate Begemand860aa62005-04-04 22:17:48 +00001481 MachineInstr *CallMI;
1482 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001483 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001484 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001485 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001486 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001487 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001488 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001489 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001490 true);
1491 } else {
1492 Tmp1 = SelectExpr(N.getOperand(1));
1493 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1494 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1495 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1496 .addReg(PPC::R12);
1497 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001498
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001499 // Load the register args to virtual regs
1500 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001501 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001502 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1503
1504 // Copy the virtual registers into the appropriate argument register
1505 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1506 switch(N.getOperand(i+2).getValueType()) {
1507 default: Node->dump(); assert(0 && "Unknown value type for call");
1508 case MVT::i1:
1509 case MVT::i8:
1510 case MVT::i16:
1511 case MVT::i32:
1512 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001513 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001514 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001515 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1516 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001517 ++GPR_idx;
1518 break;
1519 case MVT::f64:
1520 case MVT::f32:
1521 assert(FPR_idx < 13 && "Too many fp args");
1522 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001523 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001524 ++FPR_idx;
1525 break;
1526 }
1527 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001528
Nate Begemand860aa62005-04-04 22:17:48 +00001529 // Put the call instruction in the correct place in the MachineBasicBlock
1530 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001531
1532 switch (Node->getValueType(0)) {
1533 default: assert(0 && "Unknown value type for call result!");
1534 case MVT::Other: return 1;
1535 case MVT::i1:
1536 case MVT::i8:
1537 case MVT::i16:
1538 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001539 if (Node->getValueType(1) == MVT::i32) {
1540 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1541 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1542 } else {
1543 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1544 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001545 break;
1546 case MVT::f32:
1547 case MVT::f64:
1548 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1549 break;
1550 }
1551 return Result+N.ResNo;
1552 }
Nate Begemana9795f82005-03-24 04:41:43 +00001553
1554 case ISD::SIGN_EXTEND:
1555 case ISD::SIGN_EXTEND_INREG:
1556 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001557 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001558 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001559 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001560 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001561 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001562 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001563 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001564 break;
Nate Begeman74747862005-03-29 22:24:51 +00001565 case MVT::i1:
1566 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1567 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001568 }
Nate Begemana9795f82005-03-24 04:41:43 +00001569 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001570
Nate Begemana9795f82005-03-24 04:41:43 +00001571 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001572 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001573 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001574 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001575 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001576 if (MVT::isInteger(DestType))
1577 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1578 else
1579 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001580 return Result;
1581
1582 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001583 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001584 if (isIntImmediate(N.getOperand(1), Tmp2)) {
1585 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001586 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001587 .addImm(31-Tmp2);
1588 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001589 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001590 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1591 }
1592 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001593
Nate Begeman5e966612005-03-24 06:28:42 +00001594 case ISD::SRL:
1595 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001596 if (isIntImmediate(N.getOperand(1), Tmp2)) {
1597 Tmp2 &= 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001598 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001599 .addImm(Tmp2).addImm(31);
1600 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001601 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001602 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1603 }
1604 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001605
Nate Begeman5e966612005-03-24 06:28:42 +00001606 case ISD::SRA:
1607 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner2b48bc62005-08-11 17:56:50 +00001608 if (isIntImmediate(N.getOperand(1), Tmp2)) {
1609 Tmp2 &= 0x1F;
Nate Begeman5e966612005-03-24 06:28:42 +00001610 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1611 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001612 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001613 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1614 }
1615 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001616
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001617 case ISD::CTLZ:
1618 Tmp1 = SelectExpr(N.getOperand(0));
1619 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1620 return Result;
1621
Nate Begemana9795f82005-03-24 04:41:43 +00001622 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001623 if (!MVT::isInteger(DestType)) {
1624 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1625 N.getOperand(0).Val->hasOneUse()) {
1626 ++FusedFP; // Statistic
1627 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1628 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1629 Tmp3 = SelectExpr(N.getOperand(1));
1630 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1631 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1632 return Result;
1633 }
1634 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1635 N.getOperand(1).Val->hasOneUse()) {
1636 ++FusedFP; // Statistic
1637 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1638 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1639 Tmp3 = SelectExpr(N.getOperand(0));
1640 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1641 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1642 return Result;
1643 }
1644 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1645 Tmp1 = SelectExpr(N.getOperand(0));
1646 Tmp2 = SelectExpr(N.getOperand(1));
1647 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1648 return Result;
1649 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001650 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true))
1651 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001652 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001653 Tmp2 = SelectExpr(N.getOperand(1));
1654 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001655 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001656
Nate Begemana9795f82005-03-24 04:41:43 +00001657 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001658 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001659 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1660 unsigned SH, MB, ME;
1661 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1662 unsigned OprOpc;
1663 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1664 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001665 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001666 } else {
1667 Tmp1 = SelectExpr(N.getOperand(0));
1668 isRunOfOnes(Tmp2, MB, ME);
1669 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001670 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001671 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1672 .addImm(MB).addImm(ME);
1673 RecordSuccess = true;
1674 return Result;
1675 } else if (isUInt16(Tmp2)) {
1676 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001677 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001678 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001679 RecordSuccess = true;
1680 return Result;
1681 } else if (isUInt16(Tmp2)) {
1682 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001683 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001684 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001685 RecordSuccess = true;
1686 return Result;
1687 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001688 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001689 if (isOprNot(N.getOperand(0))) {
1690 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1691 Tmp2 = SelectExpr(N.getOperand(1));
1692 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1693 RecordSuccess = false;
1694 return Result;
1695 }
1696 // emit a regular and
1697 Tmp1 = SelectExpr(N.getOperand(0));
1698 Tmp2 = SelectExpr(N.getOperand(1));
1699 Opc = Recording ? PPC::ANDo : PPC::AND;
1700 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001701 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001702 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001703
Nate Begemana9795f82005-03-24 04:41:43 +00001704 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001705 if (SelectBitfieldInsert(N, Result))
1706 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001707 if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI))
1708 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001709 // emit regular or
1710 Tmp1 = SelectExpr(N.getOperand(0));
1711 Tmp2 = SelectExpr(N.getOperand(1));
1712 Opc = Recording ? PPC::ORo : PPC::OR;
1713 RecordSuccess = true;
1714 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001715 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001716
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001717 case ISD::XOR: {
1718 // Check for EQV: xor, (xor a, -1), b
Chris Lattnerdf706e32005-08-10 16:35:46 +00001719 if (isOprNot(N.getOperand(0))) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001720 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1721 Tmp2 = SelectExpr(N.getOperand(1));
1722 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1723 return Result;
1724 }
Chris Lattner837a5212005-04-21 21:09:11 +00001725 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001726 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001727 switch(N.getOperand(0).getOpcode()) {
1728 case ISD::OR:
1729 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1730 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1731 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1732 break;
1733 case ISD::AND:
1734 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1735 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1736 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1737 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001738 case ISD::XOR:
1739 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1740 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1741 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1742 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001743 default:
1744 Tmp1 = SelectExpr(N.getOperand(0));
1745 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1746 break;
1747 }
1748 return Result;
1749 }
Chris Lattnerb4138c42005-08-10 18:11:33 +00001750 if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI))
1751 return Result;
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001752 // emit regular xor
1753 Tmp1 = SelectExpr(N.getOperand(0));
1754 Tmp2 = SelectExpr(N.getOperand(1));
1755 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001756 return Result;
1757 }
1758
Chris Lattner5b909172005-08-08 21:30:29 +00001759 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001760 if (!MVT::isInteger(DestType)) {
1761 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1762 N.getOperand(0).Val->hasOneUse()) {
1763 ++FusedFP; // Statistic
1764 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1765 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1766 Tmp3 = SelectExpr(N.getOperand(1));
1767 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1768 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1769 return Result;
1770 }
1771 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1772 N.getOperand(1).Val->hasOneUse()) {
1773 ++FusedFP; // Statistic
1774 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1775 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1776 Tmp3 = SelectExpr(N.getOperand(0));
1777 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1778 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1779 return Result;
1780 }
1781 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1782 Tmp1 = SelectExpr(N.getOperand(0));
1783 Tmp2 = SelectExpr(N.getOperand(1));
1784 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1785 return Result;
1786 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001787 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Chris Lattnerb4138c42005-08-10 18:11:33 +00001788 Tmp1 = Lo16(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001789 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001790 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001791 return Result;
Chris Lattnerb4138c42005-08-10 18:11:33 +00001792 }
1793 if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true))
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001794 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001795 Tmp1 = SelectExpr(N.getOperand(0));
1796 Tmp2 = SelectExpr(N.getOperand(1));
1797 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001798 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001799
Nate Begeman5e966612005-03-24 06:28:42 +00001800 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001801 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001802 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001803 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001804 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001805 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001806 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001807 switch (DestType) {
1808 default: assert(0 && "Unknown type to ISD::MUL"); break;
1809 case MVT::i32: Opc = PPC::MULLW; break;
1810 case MVT::f32: Opc = PPC::FMULS; break;
1811 case MVT::f64: Opc = PPC::FMUL; break;
1812 }
1813 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001814 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001815 return Result;
1816
Nate Begeman815d6da2005-04-06 00:25:27 +00001817 case ISD::MULHS:
1818 case ISD::MULHU:
1819 Tmp1 = SelectExpr(N.getOperand(0));
1820 Tmp2 = SelectExpr(N.getOperand(1));
1821 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 return Result;
1824
Nate Begemanf3d08f32005-03-29 00:03:27 +00001825 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001826 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001827 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1828 Tmp3 = Log2_32(Tmp3);
Chris Lattner54abfc52005-08-11 17:15:31 +00001829 Tmp1 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001830 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001831 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1832 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001833 return Result;
1834 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1835 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001836 Tmp2 = SelectExpr(N.getOperand(0));
Chris Lattner54abfc52005-08-11 17:15:31 +00001837 Tmp1 = MakeIntReg();
1838 unsigned Tmp4 = MakeIntReg();
Chris Lattnerfd784542005-08-08 21:33:23 +00001839 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1840 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1841 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1842 return Result;
Nate Begeman9f833d32005-04-12 00:10:02 +00001843 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001844 }
1845 // fall thru
1846 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001847 // If this is a divide by constant, we can emit code using some magic
1848 // constants to implement it as a multiply instead.
Chris Lattner59b21c22005-08-09 18:29:55 +00001849 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001850 if (opcode == ISD::SDIV) {
1851 if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) {
1852 ExprMap.erase(N);
1853 return SelectExpr(BuildSDIVSequence(N));
1854 }
1855 } else {
1856 if ((signed)Tmp3 > 1) {
1857 ExprMap.erase(N);
1858 return SelectExpr(BuildUDIVSequence(N));
1859 }
1860 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001861 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001862 Tmp1 = SelectExpr(N.getOperand(0));
1863 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001864 switch (DestType) {
1865 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1866 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1867 case MVT::f32: Opc = PPC::FDIVS; break;
1868 case MVT::f64: Opc = PPC::FDIV; break;
1869 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001870 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 return Result;
1872
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001873 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001874 case ISD::SUB_PARTS: {
1875 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1876 "Not an i64 add/sub!");
1877 // Emit all of the operands.
1878 std::vector<unsigned> InVals;
1879 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1880 InVals.push_back(SelectExpr(N.getOperand(i)));
1881 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001882 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1883 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001884 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001885 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1886 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1887 }
1888 return Result+N.ResNo;
1889 }
1890
1891 case ISD::SHL_PARTS:
1892 case ISD::SRA_PARTS:
1893 case ISD::SRL_PARTS: {
1894 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1895 "Not an i64 shift!");
1896 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1897 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001898 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
Chris Lattner54abfc52005-08-11 17:15:31 +00001899 Tmp1 = MakeIntReg();
1900 Tmp2 = MakeIntReg();
1901 Tmp3 = MakeIntReg();
1902 unsigned Tmp4 = MakeIntReg();
1903 unsigned Tmp5 = MakeIntReg();
1904 unsigned Tmp6 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001905 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1906 if (ISD::SHL_PARTS == opcode) {
1907 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1908 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1909 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1910 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001911 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001912 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1913 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1914 } else if (ISD::SRL_PARTS == opcode) {
1915 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1916 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1917 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1918 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1919 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1920 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1921 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1922 } else {
1923 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1924 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1925 MachineBasicBlock *OldMBB = BB;
1926 MachineFunction *F = BB->getParent();
1927 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1928 F->getBasicBlockList().insert(It, TmpMBB);
1929 F->getBasicBlockList().insert(It, PhiMBB);
1930 BB->addSuccessor(TmpMBB);
1931 BB->addSuccessor(PhiMBB);
1932 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1933 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1934 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1935 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1936 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1937 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1938 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1939 // Select correct least significant half if the shift amount > 32
1940 BB = TmpMBB;
Chris Lattner54abfc52005-08-11 17:15:31 +00001941 unsigned Tmp7 = MakeIntReg();
Nate Begeman27eeb002005-04-02 05:59:34 +00001942 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1943 TmpMBB->addSuccessor(PhiMBB);
1944 BB = PhiMBB;
1945 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1946 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001947 }
1948 return Result+N.ResNo;
1949 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001950
Nate Begemana9795f82005-03-24 04:41:43 +00001951 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001952 case ISD::FP_TO_SINT: {
1953 bool U = (ISD::FP_TO_UINT == opcode);
1954 Tmp1 = SelectExpr(N.getOperand(0));
1955 if (!U) {
Chris Lattner54abfc52005-08-11 17:15:31 +00001956 Tmp2 = MakeFPReg();
Nate Begeman6b559972005-04-01 02:59:27 +00001957 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1958 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1959 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1960 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1961 return Result;
1962 } else {
1963 unsigned Zero = getConstDouble(0.0);
1964 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1965 unsigned Border = getConstDouble(1LL << 31);
Chris Lattner54abfc52005-08-11 17:15:31 +00001966 unsigned UseZero = MakeFPReg();
1967 unsigned UseMaxInt = MakeFPReg();
1968 unsigned UseChoice = MakeFPReg();
1969 unsigned TmpReg = MakeFPReg();
1970 unsigned TmpReg2 = MakeFPReg();
1971 unsigned ConvReg = MakeFPReg();
1972 unsigned IntTmp = MakeIntReg();
1973 unsigned XorReg = MakeIntReg();
Nate Begeman6b559972005-04-01 02:59:27 +00001974 MachineFunction *F = BB->getParent();
1975 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1976 // Update machine-CFG edges
1977 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1978 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1979 MachineBasicBlock *OldMBB = BB;
1980 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1981 F->getBasicBlockList().insert(It, XorMBB);
1982 F->getBasicBlockList().insert(It, PhiMBB);
1983 BB->addSuccessor(XorMBB);
1984 BB->addSuccessor(PhiMBB);
1985 // Convert from floating point to unsigned 32-bit value
1986 // Use 0 if incoming value is < 0.0
1987 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1988 // Use 2**32 - 1 if incoming value is >= 2**32
1989 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1990 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1991 .addReg(MaxInt);
1992 // Subtract 2**31
1993 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1994 // Use difference if >= 2**31
1995 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1996 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1997 .addReg(UseChoice);
1998 // Convert to integer
1999 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2000 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2001 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2002 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2003 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2004
2005 // XorMBB:
2006 // add 2**31 if input was >= 2**31
2007 BB = XorMBB;
2008 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2009 XorMBB->addSuccessor(PhiMBB);
2010
2011 // PhiMBB:
2012 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2013 BB = PhiMBB;
2014 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2015 .addReg(XorReg).addMBB(XorMBB);
2016 return Result;
2017 }
2018 assert(0 && "Should never get here");
2019 return 0;
2020 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002021
Chris Lattner88ac32c2005-08-09 20:21:10 +00002022 case ISD::SETCC: {
2023 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
2024 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
2025 // We can codegen setcc op, imm very efficiently compared to a brcond.
2026 // Check for those cases here.
2027 // setcc op, 0
2028 if (Tmp3 == 0) {
2029 Tmp1 = SelectExpr(Node->getOperand(0));
2030 switch (CC) {
2031 default: Node->dump(); assert(0 && "Unhandled SetCC condition"); abort();
2032 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00002033 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002034 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2035 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2036 .addImm(5).addImm(31);
2037 break;
2038 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00002039 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002040 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2041 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2042 break;
2043 case ISD::SETLT:
2044 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2045 .addImm(31).addImm(31);
2046 break;
2047 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00002048 Tmp2 = MakeIntReg();
2049 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002050 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2051 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2052 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2053 .addImm(31).addImm(31);
2054 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002055 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002056 return Result;
2057 } else if (Tmp3 == ~0U) { // setcc op, -1
2058 Tmp1 = SelectExpr(Node->getOperand(0));
2059 switch (CC) {
2060 default: assert(0 && "Unhandled SetCC condition"); abort();
2061 case ISD::SETEQ:
Chris Lattner54abfc52005-08-11 17:15:31 +00002062 Tmp2 = MakeIntReg();
2063 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002064 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2065 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2066 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
2067 break;
2068 case ISD::SETNE:
Chris Lattner54abfc52005-08-11 17:15:31 +00002069 Tmp2 = MakeIntReg();
2070 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002071 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2072 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2073 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2074 break;
2075 case ISD::SETLT:
Chris Lattner54abfc52005-08-11 17:15:31 +00002076 Tmp2 = MakeIntReg();
2077 Tmp3 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002078 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2079 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2080 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2081 .addImm(31).addImm(31);
2082 break;
2083 case ISD::SETGT:
Chris Lattner54abfc52005-08-11 17:15:31 +00002084 Tmp2 = MakeIntReg();
Chris Lattner88ac32c2005-08-09 20:21:10 +00002085 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2086 .addImm(31).addImm(31);
2087 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2088 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002089 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002090 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002091 }
Nate Begeman33162522005-03-29 21:54:38 +00002092 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002093
Nate Begemanc24d4842005-08-10 20:52:09 +00002094 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
2095 MoveCRtoGPR(CCReg, CC, Result);
Chris Lattner88ac32c2005-08-09 20:21:10 +00002096 return Result;
2097 }
Nate Begemanc24d4842005-08-10 20:52:09 +00002098
2099 case ISD::SELECT_CC: {
2100 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get();
2101 if (!MVT::isInteger(N.getOperand(0).getValueType()) &&
2102 !MVT::isInteger(N.getOperand(2).getValueType()) &&
2103 CC != ISD::SETEQ && CC != ISD::SETNE) {
2104 MVT::ValueType VT = N.getOperand(0).getValueType();
2105 unsigned TV = SelectExpr(N.getOperand(2)); // Use if TRUE
2106 unsigned FV = SelectExpr(N.getOperand(3)); // Use if FALSE
Nate Begemana3fd4002005-07-19 16:51:05 +00002107
Nate Begemanc24d4842005-08-10 20:52:09 +00002108 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00002109 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
Chris Lattner88ac32c2005-08-09 20:21:10 +00002110 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00002111 default: assert(0 && "Invalid FSEL condition"); abort();
2112 case ISD::SETULT:
2113 case ISD::SETLT:
2114 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2115 case ISD::SETUGE:
2116 case ISD::SETGE:
Nate Begemanc24d4842005-08-10 20:52:09 +00002117 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00002118 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2119 return Result;
2120 case ISD::SETUGT:
2121 case ISD::SETGT:
2122 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2123 case ISD::SETULE:
2124 case ISD::SETLE: {
Nate Begemanc24d4842005-08-10 20:52:09 +00002125 if (N.getOperand(0).getOpcode() == ISD::FNEG) {
2126 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Nate Begemana3fd4002005-07-19 16:51:05 +00002127 } else {
2128 Tmp2 = MakeReg(VT);
Nate Begemanc24d4842005-08-10 20:52:09 +00002129 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00002130 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2131 }
2132 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2133 return Result;
2134 }
2135 }
2136 } else {
2137 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanc24d4842005-08-10 20:52:09 +00002138 Tmp1 = SelectExpr(N.getOperand(0)); // Val to compare against
2139 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00002140 Tmp3 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +00002141 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00002142 default: assert(0 && "Invalid FSEL condition"); abort();
2143 case ISD::SETULT:
2144 case ISD::SETLT:
2145 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2146 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2147 return Result;
2148 case ISD::SETUGE:
2149 case ISD::SETGE:
2150 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2151 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2152 return Result;
2153 case ISD::SETUGT:
2154 case ISD::SETGT:
2155 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2156 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2157 return Result;
2158 case ISD::SETULE:
2159 case ISD::SETLE:
2160 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2161 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2162 return Result;
2163 }
2164 }
2165 assert(0 && "Should never get here");
Nate Begemana3fd4002005-07-19 16:51:05 +00002166 }
2167
Nate Begemanc24d4842005-08-10 20:52:09 +00002168 unsigned TrueValue = SelectExpr(N.getOperand(2)); //Use if TRUE
2169 unsigned FalseValue = SelectExpr(N.getOperand(3)); //Use if FALSE
2170 unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC);
2171 Opc = getBCCForSetCC(CC);
2172
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002173 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002174 // value and the MBB to hold the PHI instruction for this SetCC.
2175 MachineBasicBlock *thisMBB = BB;
2176 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2177 ilist<MachineBasicBlock>::iterator It = BB;
2178 ++It;
2179
2180 // thisMBB:
2181 // ...
2182 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002183 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002184 // bCC copy1MBB
2185 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002186 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2187 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002188 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002189 MachineFunction *F = BB->getParent();
2190 F->getBasicBlockList().insert(It, copy0MBB);
2191 F->getBasicBlockList().insert(It, sinkMBB);
2192 // Update machine-CFG edges
2193 BB->addSuccessor(copy0MBB);
2194 BB->addSuccessor(sinkMBB);
2195
2196 // copy0MBB:
2197 // %FalseValue = ...
2198 // # fallthrough to sinkMBB
2199 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002200 // Update machine-CFG edges
2201 BB->addSuccessor(sinkMBB);
2202
2203 // sinkMBB:
2204 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2205 // ...
2206 BB = sinkMBB;
2207 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2208 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002209 return Result;
2210 }
Nate Begemana9795f82005-03-24 04:41:43 +00002211
2212 case ISD::Constant:
2213 switch (N.getValueType()) {
2214 default: assert(0 && "Cannot use constants of this type!");
2215 case MVT::i1:
2216 BuildMI(BB, PPC::LI, 1, Result)
2217 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2218 break;
2219 case MVT::i32:
2220 {
2221 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2222 if (v < 32768 && v >= -32768) {
2223 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2224 } else {
Chris Lattner54abfc52005-08-11 17:15:31 +00002225 Tmp1 = MakeIntReg();
Nate Begeman5e966612005-03-24 06:28:42 +00002226 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2227 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002228 }
2229 }
2230 }
2231 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002232
2233 case ISD::ConstantFP: {
2234 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2235 Result = getConstDouble(CN->getValue(), Result);
2236 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002237 }
2238
Nate Begemana3fd4002005-07-19 16:51:05 +00002239 case ISD::FNEG:
2240 if (!NoExcessFPPrecision &&
2241 ISD::ADD == N.getOperand(0).getOpcode() &&
2242 N.getOperand(0).Val->hasOneUse() &&
2243 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2244 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2245 ++FusedFP; // Statistic
2246 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2247 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2248 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2249 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2250 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2251 } else if (!NoExcessFPPrecision &&
2252 ISD::ADD == N.getOperand(0).getOpcode() &&
2253 N.getOperand(0).Val->hasOneUse() &&
2254 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2255 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2256 ++FusedFP; // Statistic
2257 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2258 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2259 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2260 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2261 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2262 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2263 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2264 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2265 } else {
2266 Tmp1 = SelectExpr(N.getOperand(0));
2267 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2268 }
2269 return Result;
2270
2271 case ISD::FABS:
2272 Tmp1 = SelectExpr(N.getOperand(0));
2273 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2274 return Result;
2275
Nate Begemanadeb43d2005-07-20 22:42:00 +00002276 case ISD::FSQRT:
2277 Tmp1 = SelectExpr(N.getOperand(0));
2278 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2279 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2280 return Result;
2281
Nate Begemana3fd4002005-07-19 16:51:05 +00002282 case ISD::FP_ROUND:
2283 assert (DestType == MVT::f32 &&
2284 N.getOperand(0).getValueType() == MVT::f64 &&
2285 "only f64 to f32 conversion supported here");
2286 Tmp1 = SelectExpr(N.getOperand(0));
2287 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2288 return Result;
2289
2290 case ISD::FP_EXTEND:
2291 assert (DestType == MVT::f64 &&
2292 N.getOperand(0).getValueType() == MVT::f32 &&
2293 "only f32 to f64 conversion supported here");
2294 Tmp1 = SelectExpr(N.getOperand(0));
2295 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2296 return Result;
2297
2298 case ISD::UINT_TO_FP:
2299 case ISD::SINT_TO_FP: {
2300 assert (N.getOperand(0).getValueType() == MVT::i32
2301 && "int to float must operate on i32");
2302 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2303 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Chris Lattner54abfc52005-08-11 17:15:31 +00002304 Tmp2 = MakeFPReg(); // temp reg to load the integer value into
2305 Tmp3 = MakeIntReg(); // temp reg to hold the conversion constant
Nate Begemana3fd4002005-07-19 16:51:05 +00002306
2307 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2308 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2309
2310 if (IsUnsigned) {
2311 unsigned ConstF = getConstDouble(0x1.000000p52);
2312 // Store the hi & low halves of the fp value, currently in int regs
2313 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2314 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2315 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2316 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2317 // Generate the return value with a subtract
2318 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2319 } else {
2320 unsigned ConstF = getConstDouble(0x1.000008p52);
Chris Lattner54abfc52005-08-11 17:15:31 +00002321 unsigned TmpL = MakeIntReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00002322 // Store the hi & low halves of the fp value, currently in int regs
2323 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2324 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2325 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2326 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2327 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2328 // Generate the return value with a subtract
2329 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2330 }
2331 return Result;
2332 }
2333 }
Nate Begemana9795f82005-03-24 04:41:43 +00002334 return 0;
2335}
2336
2337void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00002338 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00002339 unsigned opcode = N.getOpcode();
2340
2341 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2342 return; // Already selected.
2343
2344 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002345
Nate Begemana9795f82005-03-24 04:41:43 +00002346 switch (Node->getOpcode()) {
2347 default:
2348 Node->dump(); std::cerr << "\n";
2349 assert(0 && "Node not handled yet!");
2350 case ISD::EntryToken: return; // Noop
2351 case ISD::TokenFactor:
2352 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2353 Select(Node->getOperand(i));
2354 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002355 case ISD::CALLSEQ_START:
2356 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002357 Select(N.getOperand(0));
2358 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002359 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002360 PPC::ADJCALLSTACKUP;
2361 BuildMI(BB, Opc, 1).addImm(Tmp1);
2362 return;
2363 case ISD::BR: {
2364 MachineBasicBlock *Dest =
2365 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002366 Select(N.getOperand(0));
2367 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2368 return;
2369 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002370 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002371 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002372 SelectBranchCC(N);
2373 return;
2374 case ISD::CopyToReg:
2375 Select(N.getOperand(0));
2376 Tmp1 = SelectExpr(N.getOperand(1));
2377 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002378
Nate Begemana9795f82005-03-24 04:41:43 +00002379 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002380 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002381 N.getOperand(1).getValueType() == MVT::f32)
2382 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2383 else
2384 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2385 }
2386 return;
2387 case ISD::ImplicitDef:
2388 Select(N.getOperand(0));
2389 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2390 return;
2391 case ISD::RET:
2392 switch (N.getNumOperands()) {
2393 default:
2394 assert(0 && "Unknown return instruction!");
2395 case 3:
2396 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2397 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002398 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002399 Select(N.getOperand(0));
2400 Tmp1 = SelectExpr(N.getOperand(1));
2401 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002402 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2403 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002404 break;
2405 case 2:
2406 Select(N.getOperand(0));
2407 Tmp1 = SelectExpr(N.getOperand(1));
2408 switch (N.getOperand(1).getValueType()) {
2409 default:
2410 assert(0 && "Unknown return type!");
2411 case MVT::f64:
2412 case MVT::f32:
2413 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2414 break;
2415 case MVT::i32:
2416 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2417 break;
2418 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002419 case 1:
2420 Select(N.getOperand(0));
2421 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002422 }
2423 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2424 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002425 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00002426 case ISD::STORE: {
2427 SDOperand Chain = N.getOperand(0);
2428 SDOperand Value = N.getOperand(1);
2429 SDOperand Address = N.getOperand(2);
2430 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00002431
Nate Begeman2497e632005-07-21 20:44:43 +00002432 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00002433
Nate Begeman2497e632005-07-21 20:44:43 +00002434 if (opcode == ISD::STORE) {
2435 switch(Value.getValueType()) {
2436 default: assert(0 && "unknown Type in store");
2437 case MVT::i32: Opc = PPC::STW; break;
2438 case MVT::f64: Opc = PPC::STFD; break;
2439 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002440 }
Nate Begeman2497e632005-07-21 20:44:43 +00002441 } else { //ISD::TRUNCSTORE
2442 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
2443 default: assert(0 && "unknown Type in store");
2444 case MVT::i1:
2445 case MVT::i8: Opc = PPC::STB; break;
2446 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002447 }
Nate Begemana9795f82005-03-24 04:41:43 +00002448 }
Nate Begeman2497e632005-07-21 20:44:43 +00002449
2450 if(Address.getOpcode() == ISD::FrameIndex) {
2451 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2452 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00002453 } else {
2454 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002455 switch(SelectAddr(Address, Tmp2, offset)) {
2456 default: assert(0 && "Unhandled return value from SelectAddr");
2457 case 0: // imm offset, no frame, no index
2458 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
2459 break;
2460 case 1: // imm offset + frame index
2461 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
2462 break;
2463 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00002464 Opc = IndexedOpForOp(Opc);
2465 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002466 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00002467 case 3: {
2468 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
2469 GlobalValue *GV = GN->getGlobal();
2470 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
2471 }
Nate Begeman2497e632005-07-21 20:44:43 +00002472 }
2473 }
2474 return;
2475 }
Nate Begemana9795f82005-03-24 04:41:43 +00002476 case ISD::EXTLOAD:
2477 case ISD::SEXTLOAD:
2478 case ISD::ZEXTLOAD:
2479 case ISD::LOAD:
2480 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002481 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002482 case ISD::CALL:
2483 case ISD::DYNAMIC_STACKALLOC:
2484 ExprMap.erase(N);
2485 SelectExpr(N);
2486 return;
2487 }
2488 assert(0 && "Should not be reached!");
2489}
2490
2491
2492/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2493/// into a machine code representation using pattern matching and a machine
2494/// description file.
2495///
2496FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002497 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002498}
2499