blob: b9cdb858b3e01ce66852e3d309ada71d581f4293 [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
Misha Brukman0a3f6772005-08-03 17:29:52 +000039// IsRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
40// any number of 0s on either side. The 1s are allowed to wrap from LSB to
41// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
42// not, since all 1s are not contiguous.
Chris Lattner0561b3f2005-08-02 19:26:06 +000043static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
44 if (isShiftedMask_32(Val)) {
45 // look for the first non-zero bit
46 MB = CountLeadingZeros_32(Val);
47 // look for the first zero bit after the run of ones
48 ME = CountLeadingZeros_32((Val - 1) ^ Val);
49 return true;
50 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
51 // effectively look for the first zero bit
52 ME = CountLeadingZeros_32(Val) - 1;
53 // effectively look for the first one bit after the run of zeros
54 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
55 return true;
56 }
57 // no run present
58 return false;
59}
60
Nate Begemana9795f82005-03-24 04:41:43 +000061//===----------------------------------------------------------------------===//
62// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
63namespace {
64 class PPC32TargetLowering : public TargetLowering {
65 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
66 int ReturnAddrIndex; // FrameIndex for return slot.
67 public:
68 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000069 // Fold away setcc operations if possible.
70 setSetCCIsExpensive();
71
Nate Begemana9795f82005-03-24 04:41:43 +000072 // Set up the register classes.
73 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000074 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000075 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000076
Nate Begeman74d73452005-03-31 00:15:26 +000077 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000078 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
79 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
80 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
81
Nate Begeman74d73452005-03-31 00:15:26 +000082 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
83 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
84 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000085
Nate Begeman815d6da2005-04-06 00:25:27 +000086 // PowerPC has no SREM/UREM instructions
87 setOperationAction(ISD::SREM, MVT::i32, Expand);
88 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000089
Chris Lattner32f3cf62005-05-13 16:20:22 +000090 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000091 setOperationAction(ISD::FSIN , MVT::f64, Expand);
92 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000093 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000094 setOperationAction(ISD::FSIN , MVT::f32, Expand);
95 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000096 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000097
Nate Begemanadeb43d2005-07-20 22:42:00 +000098 // If we're enabling GP optimizations, use hardware square root
Nate Begeman2497e632005-07-21 20:44:43 +000099 if (!GPOPT) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000100 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
101 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
102 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000103
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000104 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000105 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
106 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000107
Chris Lattnercbd06fc2005-04-07 19:41:49 +0000108 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +0000109 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000110 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +0000111
Nate Begemana9795f82005-03-24 04:41:43 +0000112 computeRegisterProperties();
113 }
114
115 /// LowerArguments - This hook must be implemented to indicate how we should
116 /// lower the arguments for the specified function, into the specified DAG.
117 virtual std::vector<SDOperand>
118 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000119
Nate Begemana9795f82005-03-24 04:41:43 +0000120 /// LowerCallTo - This hook lowers an abstract call to a function into an
121 /// actual call.
122 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000123 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000124 bool isTailCall, SDOperand Callee, ArgListTy &Args,
125 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000126
Chris Lattnere0fe2252005-07-05 19:58:54 +0000127 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
128 Value *VAListV, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000129
Nate Begemana9795f82005-03-24 04:41:43 +0000130 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000131 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
132 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000133
Nate Begemana9795f82005-03-24 04:41:43 +0000134 virtual std::pair<SDOperand, SDOperand>
135 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
136 SelectionDAG &DAG);
137 };
138}
139
140
141std::vector<SDOperand>
142PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
143 //
144 // add beautiful description of PPC stack frame format, or at least some docs
145 //
146 MachineFunction &MF = DAG.getMachineFunction();
147 MachineFrameInfo *MFI = MF.getFrameInfo();
148 MachineBasicBlock& BB = MF.front();
149 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000150
151 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000152 // fixed size array of physical args, for the sake of simplicity let the STL
153 // handle tracking them for us.
154 std::vector<unsigned> argVR, argPR, argOp;
155 unsigned ArgOffset = 24;
156 unsigned GPR_remaining = 8;
157 unsigned FPR_remaining = 13;
158 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000159 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000160 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
161 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
162 };
163 static const unsigned FPR[] = {
164 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
165 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
166 };
167
168 // Add DAG nodes to load the arguments... On entry to a function on PPC,
169 // the arguments start at offset 24, although they are likely to be passed
170 // in registers.
171 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
172 SDOperand newroot, argt;
173 unsigned ObjSize;
174 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000175 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000176 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000177
Nate Begemana9795f82005-03-24 04:41:43 +0000178 switch (ObjectVT) {
179 default: assert(0 && "Unhandled argument type!");
180 case MVT::i1:
181 case MVT::i8:
182 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000183 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000184 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000185 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000186 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000187 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000188 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
189 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000190 if (ObjectVT != MVT::i32)
191 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000192 } else {
193 needsLoad = true;
194 }
195 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000196 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000197 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000198 if (GPR_remaining > 0) {
199 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000200 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000201 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
202 // If we have two or more remaining argument registers, then both halves
203 // of the i64 can be sourced from there. Otherwise, the lower half will
204 // have to come off the stack. This can happen when an i64 is preceded
205 // by 28 bytes of arguments.
206 if (GPR_remaining > 1) {
207 MF.addLiveIn(GPR[GPR_idx+1]);
208 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
209 } else {
210 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
211 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000212 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
213 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000214 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000215 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000216 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
217 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000218 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000220 }
221 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000222 case MVT::f32:
223 case MVT::f64:
224 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
225 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000226 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000227 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000228 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000229 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000230 --FPR_remaining;
231 ++FPR_idx;
232 } else {
233 needsLoad = true;
234 }
235 break;
236 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000237
Nate Begemana9795f82005-03-24 04:41:43 +0000238 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000239 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000240 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000241 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000242 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000243 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000244 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
245 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000246 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000247 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000248 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
249 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000250 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000251
Nate Begemana9795f82005-03-24 04:41:43 +0000252 // Every 4 bytes of argument space consumes one of the GPRs available for
253 // argument passing.
254 if (GPR_remaining > 0) {
255 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
256 GPR_remaining -= delta;
257 GPR_idx += delta;
258 }
259 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000260 if (newroot.Val)
261 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000262
Nate Begemana9795f82005-03-24 04:41:43 +0000263 ArgValues.push_back(argt);
264 }
265
Nate Begemana9795f82005-03-24 04:41:43 +0000266 // If the function takes variable number of arguments, make a frame index for
267 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000268 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000269 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000270 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000271 // If this function is vararg, store any remaining integer argument regs
272 // to their spots on the stack so that they may be loaded by deferencing the
273 // result of va_next.
274 std::vector<SDOperand> MemOps;
275 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000276 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000277 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000278 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000279 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000280 MemOps.push_back(Store);
281 // Increment the address by four for the next argument to store
282 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
283 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
284 }
285 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000286 }
Nate Begemana9795f82005-03-24 04:41:43 +0000287
Nate Begemancd08e4c2005-04-09 20:09:12 +0000288 // Finally, inform the code generator which regs we return values in.
289 switch (getValueType(F.getReturnType())) {
290 default: assert(0 && "Unknown type!");
291 case MVT::isVoid: break;
292 case MVT::i1:
293 case MVT::i8:
294 case MVT::i16:
295 case MVT::i32:
296 MF.addLiveOut(PPC::R3);
297 break;
298 case MVT::i64:
299 MF.addLiveOut(PPC::R3);
300 MF.addLiveOut(PPC::R4);
301 break;
302 case MVT::f32:
303 case MVT::f64:
304 MF.addLiveOut(PPC::F1);
305 break;
306 }
307
Nate Begemana9795f82005-03-24 04:41:43 +0000308 return ArgValues;
309}
310
311std::pair<SDOperand, SDOperand>
312PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000313 const Type *RetTy, bool isVarArg,
Jeff Cohen00b168892005-07-27 06:12:32 +0000314 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000315 SDOperand Callee, ArgListTy &Args,
316 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000317 // args_to_use will accumulate outgoing args for the ISD::CALL case in
318 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000319 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000320
321 // Count how many bytes are to be pushed on the stack, including the linkage
322 // area, and parameter passing area.
323 unsigned NumBytes = 24;
324
325 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000326 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000327 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000328 } else {
329 for (unsigned i = 0, e = Args.size(); i != e; ++i)
330 switch (getValueType(Args[i].second)) {
331 default: assert(0 && "Unknown value type!");
332 case MVT::i1:
333 case MVT::i8:
334 case MVT::i16:
335 case MVT::i32:
336 case MVT::f32:
337 NumBytes += 4;
338 break;
339 case MVT::i64:
340 case MVT::f64:
341 NumBytes += 8;
342 break;
343 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000344
345 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000346 // plus 32 bytes of argument space in case any called code gets funky on us.
Chris Lattner0561b3f2005-08-02 19:26:06 +0000347 // (Required by ABI to support var arg)
Nate Begeman307e7442005-03-26 01:28:53 +0000348 if (NumBytes < 56) NumBytes = 56;
349
350 // Adjust the stack pointer for the new arguments...
351 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000352 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000353 DAG.getConstant(NumBytes, getPointerTy()));
354
355 // Set up a copy of the stack pointer for use loading and storing any
356 // arguments that may not fit in the registers available for argument
357 // passing.
358 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
359 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000360
Nate Begeman307e7442005-03-26 01:28:53 +0000361 // Figure out which arguments are going to go in registers, and which in
362 // memory. Also, if this is a vararg function, floating point operations
363 // must be stored to our stack, and loaded into integer regs as well, if
364 // any integer regs are available for argument passing.
365 unsigned ArgOffset = 24;
366 unsigned GPR_remaining = 8;
367 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000368
Nate Begeman74d73452005-03-31 00:15:26 +0000369 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000370 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
371 // PtrOff will be used to store the current argument to the stack if a
372 // register cannot be found for it.
373 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
374 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000375 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000376
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000378 default: assert(0 && "Unexpected ValueType for argument!");
379 case MVT::i1:
380 case MVT::i8:
381 case MVT::i16:
382 // Promote the integer to 32 bits. If the input type is signed use a
383 // sign extend, otherwise use a zero extend.
384 if (Args[i].second->isSigned())
385 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
386 else
387 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
388 // FALL THROUGH
389 case MVT::i32:
390 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000391 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000392 --GPR_remaining;
393 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000394 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000395 Args[i].first, PtrOff,
396 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000397 }
398 ArgOffset += 4;
399 break;
400 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000401 // If we have one free GPR left, we can place the upper half of the i64
402 // in it, and store the other half to the stack. If we have two or more
403 // free GPRs, then we can pass both halves of the i64 in registers.
404 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000405 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000406 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000407 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000408 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000409 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000410 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000411 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000412 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000413 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000414 } else {
415 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
416 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000417 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000418 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000419 }
Nate Begeman307e7442005-03-26 01:28:53 +0000420 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000421 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000422 Args[i].first, PtrOff,
423 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000424 }
425 ArgOffset += 8;
426 break;
427 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000428 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000429 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000430 args_to_use.push_back(Args[i].first);
431 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000432 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000433 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000434 Args[i].first, PtrOff,
435 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000436 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000437 // Float varargs are always shadowed in available integer registers
438 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000439 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
440 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000441 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000442 args_to_use.push_back(Load);
443 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000444 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000445 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000446 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
447 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000448 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
449 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000450 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000451 args_to_use.push_back(Load);
452 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000453 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000454 } else {
455 // If we have any FPRs remaining, we may also have GPRs remaining.
456 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
457 // GPRs.
458 if (GPR_remaining > 0) {
459 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
460 --GPR_remaining;
461 }
462 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
463 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
464 --GPR_remaining;
465 }
Nate Begeman74d73452005-03-31 00:15:26 +0000466 }
Nate Begeman307e7442005-03-26 01:28:53 +0000467 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000468 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000469 Args[i].first, PtrOff,
470 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000471 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000472 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000473 break;
474 }
Nate Begemana9795f82005-03-24 04:41:43 +0000475 }
Nate Begeman74d73452005-03-31 00:15:26 +0000476 if (!MemOps.empty())
477 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000478 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000479
Nate Begemana9795f82005-03-24 04:41:43 +0000480 std::vector<MVT::ValueType> RetVals;
481 MVT::ValueType RetTyVT = getValueType(RetTy);
482 if (RetTyVT != MVT::isVoid)
483 RetVals.push_back(RetTyVT);
484 RetVals.push_back(MVT::Other);
485
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000486 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000487 Chain, Callee, args_to_use), 0);
488 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000489 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000490 DAG.getConstant(NumBytes, getPointerTy()));
491 return std::make_pair(TheCall, Chain);
492}
493
Chris Lattnere0fe2252005-07-05 19:58:54 +0000494SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
495 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000496 // vastart just stores the address of the VarArgsFrameIndex slot into the
497 // memory location argument.
498 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000499 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
500 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000501}
502
Chris Lattnere0fe2252005-07-05 19:58:54 +0000503std::pair<SDOperand,SDOperand>
504PPC32TargetLowering::LowerVAArg(SDOperand Chain,
505 SDOperand VAListP, Value *VAListV,
506 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000507 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000508
509 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000510 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
511 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000512 unsigned Amt;
513 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
514 Amt = 4;
515 else {
516 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
517 "Other types should have been promoted for varargs!");
518 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000519 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000520 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
521 DAG.getConstant(Amt, VAList.getValueType()));
522 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000523 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000524 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000525}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000526
Nate Begemana9795f82005-03-24 04:41:43 +0000527
528std::pair<SDOperand, SDOperand> PPC32TargetLowering::
529LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
530 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000531 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000532 abort();
533}
534
535namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000536Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000537Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000538Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000539//===--------------------------------------------------------------------===//
540/// ISel - PPC32 specific code to select PPC32 machine instructions for
541/// SelectionDAG operations.
542//===--------------------------------------------------------------------===//
543class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000544 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000545 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
546 // for sdiv and udiv until it is put into the future
547 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548
Nate Begemana9795f82005-03-24 04:41:43 +0000549 /// ExprMap - As shared expressions are codegen'd, we keep track of which
550 /// vreg the value is produced in, so we only emit one copy of each compiled
551 /// tree.
552 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000553
554 unsigned GlobalBaseReg;
555 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000556 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000557public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000558 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
559 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000560
Nate Begemanc7b09f12005-03-25 08:34:25 +0000561 /// runOnFunction - Override this function in order to reset our per-function
562 /// variables.
563 virtual bool runOnFunction(Function &Fn) {
564 // Make sure we re-emit a set of the global base reg if necessary
565 GlobalBaseInitialized = false;
566 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000567 }
568
Nate Begemana9795f82005-03-24 04:41:43 +0000569 /// InstructionSelectBasicBlock - This callback is invoked by
570 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
571 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
572 DEBUG(BB->dump());
573 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000574 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000575 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000576
Nate Begemana9795f82005-03-24 04:41:43 +0000577 // Clear state used for selection.
578 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000579 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000580 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000581
582 // dag -> dag expanders for integer divide by constant
583 SDOperand BuildSDIVSequence(SDOperand N);
584 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000585
Nate Begemandffcfcc2005-04-01 00:32:34 +0000586 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000587 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000588 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000589 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000590 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000591 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
592 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000593 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000594 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000595
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000596 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000597 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000598
599 virtual const char *getPassName() const {
600 return "PowerPC Pattern Instruction Selection";
601 }
Nate Begemana9795f82005-03-24 04:41:43 +0000602};
603
Nate Begeman439b4442005-04-05 04:22:58 +0000604/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000605/// the ConstantSDNode N can be used as an immediate to Opcode. The return
606/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000607/// ConstantSDNode, or is not suitable for use by that opcode.
608/// Return value codes for turning into an enum someday:
609/// 1: constant may be used in normal immediate form.
610/// 2: constant may be used in shifted immediate form.
611/// 3: log base 2 of the constant may be used.
612/// 4: constant is suitable for integer division conversion
613/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000614///
Nate Begeman439b4442005-04-05 04:22:58 +0000615static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
616 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000617 if (N.getOpcode() != ISD::Constant) return 0;
618
619 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000620
Nate Begemana9795f82005-03-24 04:41:43 +0000621 switch(Opcode) {
622 default: return 0;
623 case ISD::ADD:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000624 if (isInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begemana9795f82005-03-24 04:41:43 +0000625 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
626 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000627 case ISD::AND: {
628 unsigned MB, ME;
629 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
Chris Lattner0561b3f2005-08-02 19:26:06 +0000630 if (isUInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000631 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
632 break;
633 }
Nate Begemana9795f82005-03-24 04:41:43 +0000634 case ISD::XOR:
635 case ISD::OR:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000636 if (isUInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begemana9795f82005-03-24 04:41:43 +0000637 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
638 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000639 case ISD::MUL:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000640 if (isInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begeman307e7442005-03-26 01:28:53 +0000641 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000642 case ISD::SUB:
643 // handle subtract-from separately from subtract, since subi is really addi
Chris Lattner0561b3f2005-08-02 19:26:06 +0000644 if (U && isInt16(v)) { Imm = v & 0xFFFF; return 1; }
645 if (!U && isInt16(-v)) { Imm = (-v) & 0xFFFF; return 1; }
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000646 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000647 case ISD::SETCC:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000648 if (U && isUInt16(v)) { Imm = v & 0xFFFF; return 1; }
649 if (!U && isInt16(v)) { Imm = v & 0xFFFF; return 1; }
Nate Begeman3e897162005-03-31 23:55:40 +0000650 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000651 case ISD::SDIV:
Chris Lattner0561b3f2005-08-02 19:26:06 +0000652 if (isPowerOf2_32(v)) { Imm = Log2_32(v); return 3; }
653 if (isPowerOf2_32(-v)) { Imm = Log2_32(-v); return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000654 if (v <= -2 || v >= 2) { return 4; }
655 break;
656 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000657 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000658 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000659 }
660 return 0;
661}
Nate Begeman3e897162005-03-31 23:55:40 +0000662
Nate Begemanc7bd4822005-04-11 06:34:10 +0000663/// NodeHasRecordingVariant - If SelectExpr can always produce code for
664/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
665/// return false.
666static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
667 switch(NodeOpcode) {
668 default: return false;
669 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000670 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000671 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000672 }
673}
674
Nate Begeman3e897162005-03-31 23:55:40 +0000675/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
676/// to Condition. If the Condition is unordered or unsigned, the bool argument
677/// U is set to true, otherwise it is set to false.
678static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
679 U = false;
680 switch (Condition) {
681 default: assert(0 && "Unknown condition!"); abort();
682 case ISD::SETEQ: return PPC::BEQ;
683 case ISD::SETNE: return PPC::BNE;
684 case ISD::SETULT: U = true;
685 case ISD::SETLT: return PPC::BLT;
686 case ISD::SETULE: U = true;
687 case ISD::SETLE: return PPC::BLE;
688 case ISD::SETUGT: U = true;
689 case ISD::SETGT: return PPC::BGT;
690 case ISD::SETUGE: U = true;
691 case ISD::SETGE: return PPC::BGE;
692 }
Nate Begeman04730362005-04-01 04:45:11 +0000693 return 0;
694}
695
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000696/// getCROpForOp - Return the condition register opcode (or inverted opcode)
697/// associated with the SelectionDAG opcode.
698static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
699 switch (Opcode) {
700 default: assert(0 && "Unknown opcode!"); abort();
701 case ISD::AND:
702 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
703 if (!Inv1 && !Inv2) return PPC::CRAND;
704 if (Inv1 ^ Inv2) return PPC::CRANDC;
705 case ISD::OR:
706 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
707 if (!Inv1 && !Inv2) return PPC::CROR;
708 if (Inv1 ^ Inv2) return PPC::CRORC;
709 }
710 return 0;
711}
712
713/// getCRIdxForSetCC - Return the index of the condition register field
714/// associated with the SetCC condition, and whether or not the field is
715/// treated as inverted. That is, lt = 0; ge = 0 inverted.
716static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
717 switch (Condition) {
718 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000719 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000720 case ISD::SETLT: Inv = false; return 0;
721 case ISD::SETUGE:
722 case ISD::SETGE: Inv = true; return 0;
723 case ISD::SETUGT:
724 case ISD::SETGT: Inv = false; return 1;
725 case ISD::SETULE:
726 case ISD::SETLE: Inv = true; return 1;
727 case ISD::SETEQ: Inv = false; return 2;
728 case ISD::SETNE: Inv = true; return 2;
729 }
730 return 0;
731}
732
Nate Begeman04730362005-04-01 04:45:11 +0000733/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
734/// and store immediate instructions.
735static unsigned IndexedOpForOp(unsigned Opcode) {
736 switch(Opcode) {
737 default: assert(0 && "Unknown opcode!"); abort();
738 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
739 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
740 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
741 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
742 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
743 case PPC::LFD: return PPC::LFDX;
744 }
745 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000746}
Nate Begeman815d6da2005-04-06 00:25:27 +0000747
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000748// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000749// a multiply.
750struct ms {
751 int m; // magic number
752 int s; // shift amount
753};
754
755struct mu {
756 unsigned int m; // magic number
757 int a; // add indicator
758 int s; // shift amount
759};
760
761/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000762/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000763/// or -1.
764static struct ms magic(int d) {
765 int p;
766 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000767 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000768 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000769
Nate Begeman815d6da2005-04-06 00:25:27 +0000770 ad = abs(d);
771 t = two31 + ((unsigned int)d >> 31);
772 anc = t - 1 - t%ad; // absolute value of nc
773 p = 31; // initialize p
774 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
775 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
776 q2 = two31/ad; // initialize q2 = 2p/abs(d)
777 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
778 do {
779 p = p + 1;
780 q1 = 2*q1; // update q1 = 2p/abs(nc)
781 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
782 if (r1 >= anc) { // must be unsigned comparison
783 q1 = q1 + 1;
784 r1 = r1 - anc;
785 }
786 q2 = 2*q2; // update q2 = 2p/abs(d)
787 r2 = 2*r2; // update r2 = rem(2p/abs(d))
788 if (r2 >= ad) { // must be unsigned comparison
789 q2 = q2 + 1;
790 r2 = r2 - ad;
791 }
792 delta = ad - r2;
793 } while (q1 < delta || (q1 == delta && r1 == 0));
794
795 mag.m = q2 + 1;
796 if (d < 0) mag.m = -mag.m; // resulting magic number
797 mag.s = p - 32; // resulting shift
798 return mag;
799}
800
801/// magicu - calculate the magic numbers required to codegen an integer udiv as
802/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
803static struct mu magicu(unsigned d)
804{
805 int p;
806 unsigned int nc, delta, q1, r1, q2, r2;
807 struct mu magu;
808 magu.a = 0; // initialize "add" indicator
809 nc = - 1 - (-d)%d;
810 p = 31; // initialize p
811 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
812 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
813 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
814 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
815 do {
816 p = p + 1;
817 if (r1 >= nc - r1 ) {
818 q1 = 2*q1 + 1; // update q1
819 r1 = 2*r1 - nc; // update r1
820 }
821 else {
822 q1 = 2*q1; // update q1
823 r1 = 2*r1; // update r1
824 }
825 if (r2 + 1 >= d - r2) {
826 if (q2 >= 0x7FFFFFFF) magu.a = 1;
827 q2 = 2*q2 + 1; // update q2
828 r2 = 2*r2 + 1 - d; // update r2
829 }
830 else {
831 if (q2 >= 0x80000000) magu.a = 1;
832 q2 = 2*q2; // update q2
833 r2 = 2*r2 + 1; // update r2
834 }
835 delta = d - 1 - r2;
836 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
837 magu.m = q2 + 1; // resulting magic number
838 magu.s = p - 32; // resulting shift
839 return magu;
840}
841}
842
843/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
844/// return a DAG expression to select that will generate the same value by
845/// multiplying by a magic number. See:
846/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
847SDOperand ISel::BuildSDIVSequence(SDOperand N) {
848 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
849 ms magics = magic(d);
850 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000851 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000852 ISelDAG->getConstant(magics.m, MVT::i32));
853 // If d > 0 and m < 0, add the numerator
854 if (d > 0 && magics.m < 0)
855 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
856 // If d < 0 and m > 0, subtract the numerator.
857 if (d < 0 && magics.m > 0)
858 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
859 // Shift right algebraic if shift value is nonzero
860 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000861 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000862 ISelDAG->getConstant(magics.s, MVT::i32));
863 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000864 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000865 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000866 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000867}
868
869/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
870/// return a DAG expression to select that will generate the same value by
871/// multiplying by a magic number. See:
872/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
873SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000874 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000875 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
876 mu magics = magicu(d);
877 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000878 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000879 ISelDAG->getConstant(magics.m, MVT::i32));
880 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000881 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000882 ISelDAG->getConstant(magics.s, MVT::i32));
883 } else {
884 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000885 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000886 ISelDAG->getConstant(1, MVT::i32));
887 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000888 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000889 ISelDAG->getConstant(magics.s-1, MVT::i32));
890 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000891 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000892}
893
Nate Begemanc7b09f12005-03-25 08:34:25 +0000894/// getGlobalBaseReg - Output the instructions required to put the
895/// base address to use for accessing globals into a register.
896///
897unsigned ISel::getGlobalBaseReg() {
898 if (!GlobalBaseInitialized) {
899 // Insert the set of GlobalBaseReg into the first MBB of the function
900 MachineBasicBlock &FirstMBB = BB->getParent()->front();
901 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
902 GlobalBaseReg = MakeReg(MVT::i32);
903 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
904 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
905 GlobalBaseInitialized = true;
906 }
907 return GlobalBaseReg;
908}
909
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000910/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000911/// Constant Pool. Optionally takes a register in which to load the value.
912unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
913 unsigned Tmp1 = MakeReg(MVT::i32);
914 if (0 == Result) Result = MakeReg(MVT::f64);
915 MachineConstantPool *CP = BB->getParent()->getConstantPool();
916 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
917 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000918 if (PICEnabled)
919 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
920 .addConstantPoolIndex(CPI);
921 else
922 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000923 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
924 return Result;
925}
926
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000927/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000928/// Inv is true, then invert the result.
929void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
930 unsigned IntCR = MakeReg(MVT::i32);
931 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Nate Begeman2497e632005-07-21 20:44:43 +0000932 BuildMI(BB, GPOPT ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000933 if (Inv) {
934 unsigned Tmp1 = MakeReg(MVT::i32);
935 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
936 .addImm(31).addImm(31);
937 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
938 } else {
939 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
940 .addImm(31).addImm(31);
941 }
942}
943
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000944/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000945/// the rotate left word immediate then mask insert (rlwimi) instruction.
946/// Returns true on success, false if the caller still needs to select OR.
947///
948/// Patterns matched:
949/// 1. or shl, and 5. or and, and
950/// 2. or and, shl 6. or shl, shr
951/// 3. or shr, and 7. or shr, shl
952/// 4. or and, shr
953bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000954 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000955 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000956
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000957 SDOperand Op0 = OR.getOperand(0);
958 SDOperand Op1 = OR.getOperand(1);
959
960 unsigned Op0Opc = Op0.getOpcode();
961 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000962
Nate Begeman7ddecb42005-04-06 23:51:40 +0000963 // Verify that we have the correct opcodes
964 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
965 return false;
966 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
967 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000968
Nate Begeman7ddecb42005-04-06 23:51:40 +0000969 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000970 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000971 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000972 switch(Op0Opc) {
973 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
974 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
975 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
976 }
977 } else {
978 return false;
979 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000980
Nate Begeman7ddecb42005-04-06 23:51:40 +0000981 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000982 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000983 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000984 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000985 case ISD::SHL:
986 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000987 InsMask <<= Amount;
988 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000989 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000990 case ISD::SRL:
991 Amount = CN->getValue();
992 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000993 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000994 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000995 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000996 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +0000997 InsMask &= (unsigned)CN->getValue();
998 break;
999 }
1000 } else {
1001 return false;
1002 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001003
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001004 unsigned Tmp3 = 0;
1005
1006 // If both of the inputs are ANDs and one of them has a logical shift by
1007 // constant as its input, make that the inserted value so that we can combine
1008 // the shift into the rotate part of the rlwimi instruction
1009 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001010 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001011 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001012 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001013 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001014 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001015 CN->getValue() : 32 - CN->getValue();
1016 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1017 }
1018 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1019 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001020 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001021 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1022 std::swap(Op0, Op1);
1023 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +00001024 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001025 CN->getValue() : 32 - CN->getValue();
1026 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1027 }
1028 }
1029 }
1030
Nate Begeman7ddecb42005-04-06 23:51:40 +00001031 // Verify that the Target mask and Insert mask together form a full word mask
1032 // and that the Insert mask is a run of set bits (which implies both are runs
1033 // of set bits). Given that, Select the arguments and generate the rlwimi
1034 // instruction.
1035 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001036 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001037 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001038 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001039 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1040 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001041 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001042 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001043 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1044 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1045 .addImm(0).addImm(31);
1046 return true;
1047 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001048 if (Op0Opc == ISD::AND && fullMask)
1049 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001050 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001051 Tmp1 = SelectExpr(Op0);
1052 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001053 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1054 .addImm(Amount).addImm(MB).addImm(ME);
1055 return true;
1056 }
1057 return false;
1058}
1059
Nate Begeman3664cef2005-04-13 22:14:14 +00001060/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1061/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1062/// wider than the implicit mask, then we can get rid of the AND and let the
1063/// shift do the mask.
1064unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1065 unsigned C;
1066 if (N.getOpcode() == ISD::AND &&
1067 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1068 31 == (C & 0xFFFF) && // ME
1069 26 >= (C >> 16)) // MB
1070 return SelectExpr(N.getOperand(0));
1071 else
1072 return SelectExpr(N);
1073}
1074
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001075unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001076 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001077 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001078 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001079 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001080
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001081 // Allocate a condition register for this expression
1082 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001083
Nate Begemandffcfcc2005-04-01 00:32:34 +00001084 // If the first operand to the select is a SETCC node, then we can fold it
1085 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001086 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001087 bool U;
1088 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001089 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001090
Nate Begeman439b4442005-04-05 04:22:58 +00001091 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001092 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001093 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001094 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001095 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001096 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1097 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001098 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001099 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1100 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001101 RecordSuccess = false;
1102 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1103 if (RecordSuccess) {
1104 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001105 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1106 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001107 }
1108 AlreadySelected = true;
1109 }
1110 // If we could not implicitly set CR0, then emit a compare immediate
1111 // instead.
1112 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001113 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001114 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001115 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001116 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001117 } else {
1118 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1119 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001120 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001121 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001122 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001123 }
1124 } else {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001125 // If this isn't a SetCC, then select the value and compare it against zero,
1126 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001127 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001128 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001129 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001130 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001131 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001132 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001133}
1134
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001135unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001136 unsigned &Idx) {
1137 bool Inv0, Inv1;
1138 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1139
1140 // Allocate a condition register for this expression
1141 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1142
1143 // Check for the operations we support:
1144 switch(N.getOpcode()) {
1145 default:
1146 Opc = PPC::BNE;
1147 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1148 Tmp1 = SelectExpr(N);
1149 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1150 break;
1151 case ISD::OR:
1152 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001153 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1154 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1155 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1156 if (Inv0 && !Inv1) {
1157 std::swap(Tmp1, Tmp2);
1158 std::swap(Idx0, Idx1);
1159 Opc = Opc1;
1160 }
1161 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1162 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1163 .addReg(Tmp2).addImm(Idx1);
1164 Inv = false;
1165 Idx = Idx0;
1166 break;
1167 case ISD::SETCC:
1168 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1169 Result = Tmp1;
1170 break;
1171 }
1172 return Result;
1173}
1174
Nate Begemandffcfcc2005-04-01 00:32:34 +00001175/// Check to see if the load is a constant offset from a base register
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001176unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001177{
Nate Begeman96fc6812005-03-31 02:05:53 +00001178 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001179 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001180 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Nate Begeman439b4442005-04-05 04:22:58 +00001181 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001182 offset = imm;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001183 if (isFrame) {
1184 ++FrameOff;
1185 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
1186 return 1;
1187 } else {
1188 Reg = SelectExpr(N.getOperand(0));
1189 return 0;
1190 }
1191 } else {
1192 Reg = SelectExpr(N.getOperand(0));
1193 offset = SelectExpr(N.getOperand(1));
1194 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001195 }
Nate Begeman04730362005-04-01 04:45:11 +00001196 }
Nate Begemana9795f82005-03-24 04:41:43 +00001197 Reg = SelectExpr(N);
1198 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001199 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +00001200}
1201
1202void ISel::SelectBranchCC(SDOperand N)
1203{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001204 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001205 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001206
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001207 bool Inv;
1208 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001209 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001210 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001211
Nate Begeman439009c2005-06-15 18:22:43 +00001212 // Iterate to the next basic block
1213 ilist<MachineBasicBlock>::iterator It = BB;
1214 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001215
1216 // If this is a two way branch, then grab the fallthrough basic block argument
1217 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1218 // if necessary by the branch selection pass. Otherwise, emit a standard
1219 // conditional branch.
1220 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001221 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001222 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1223 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001224 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001225 .addMBB(Dest).addMBB(Fallthrough);
1226 if (Fallthrough != It)
1227 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1228 } else {
1229 if (Fallthrough != It) {
1230 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001231 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001232 .addMBB(Fallthrough).addMBB(Dest);
1233 }
1234 }
1235 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001236 // If the fallthrough path is off the end of the function, which would be
1237 // undefined behavior, set it to be the same as the current block because
1238 // we have nothing better to set it to, and leaving it alone will cause the
1239 // PowerPC Branch Selection pass to crash.
1240 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001241 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001242 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001243 }
Nate Begemana9795f82005-03-24 04:41:43 +00001244 return;
1245}
1246
Nate Begemanc7bd4822005-04-11 06:34:10 +00001247unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001248 unsigned Result;
1249 unsigned Tmp1, Tmp2, Tmp3;
1250 unsigned Opc = 0;
1251 unsigned opcode = N.getOpcode();
1252
1253 SDNode *Node = N.Val;
1254 MVT::ValueType DestType = N.getValueType();
1255
Nate Begemana43b1762005-06-14 03:55:23 +00001256 if (Node->getOpcode() == ISD::CopyFromReg &&
Chris Lattner988b1dd2005-07-28 05:23:43 +00001257 (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1258 cast<RegSDNode>(Node)->getReg() == PPC::R1))
Nate Begemana43b1762005-06-14 03:55:23 +00001259 // Just use the specified register as our input.
1260 return cast<RegSDNode>(Node)->getReg();
1261
Nate Begemana9795f82005-03-24 04:41:43 +00001262 unsigned &Reg = ExprMap[N];
1263 if (Reg) return Reg;
1264
Nate Begeman27eeb002005-04-02 05:59:34 +00001265 switch (N.getOpcode()) {
1266 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001267 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001268 MakeReg(N.getValueType()) : 1;
1269 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001270 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001271 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001272 // If this is a call instruction, make sure to prepare ALL of the result
1273 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001274 if (Node->getNumValues() == 1)
1275 Reg = Result = 1; // Void call, just a chain.
1276 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001277 Result = MakeReg(Node->getValueType(0));
1278 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001279 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001280 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001281 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001282 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001283 break;
1284 case ISD::ADD_PARTS:
1285 case ISD::SUB_PARTS:
1286 case ISD::SHL_PARTS:
1287 case ISD::SRL_PARTS:
1288 case ISD::SRA_PARTS:
1289 Result = MakeReg(Node->getValueType(0));
1290 ExprMap[N.getValue(0)] = Result;
1291 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1292 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1293 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001294 }
1295
Nate Begemana9795f82005-03-24 04:41:43 +00001296 switch (opcode) {
1297 default:
1298 Node->dump();
1299 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001300 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001301 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1302 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001303 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001304 // Generate both result values. FIXME: Need a better commment here?
1305 if (Result != 1)
1306 ExprMap[N.getValue(1)] = 1;
1307 else
1308 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1309
1310 // FIXME: We are currently ignoring the requested alignment for handling
1311 // greater than the stack alignment. This will need to be revisited at some
1312 // point. Align = N.getOperand(2);
1313 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1314 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1315 std::cerr << "Cannot allocate stack object with greater alignment than"
1316 << " the stack alignment yet!";
1317 abort();
1318 }
1319 Select(N.getOperand(0));
1320 Tmp1 = SelectExpr(N.getOperand(1));
1321 // Subtract size from stack pointer, thereby allocating some space.
1322 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1323 // Put a pointer to the space into the result register by copying the SP
1324 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1325 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001326
1327 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001328 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1329 Tmp2 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001330 if (PICEnabled)
1331 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
1332 .addConstantPoolIndex(Tmp1);
1333 else
1334 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001335 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1336 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001337
1338 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001339 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001340 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001341 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001342
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001343 case ISD::GlobalAddress: {
1344 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001345 Tmp1 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001346 if (PICEnabled)
1347 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1348 .addGlobalAddress(GV);
1349 else
Chris Lattner4015ea82005-07-28 04:42:11 +00001350 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001351 if (GV->hasWeakLinkage() || GV->isExternal()) {
1352 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1353 } else {
1354 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1355 }
1356 return Result;
1357 }
1358
Nate Begeman5e966612005-03-24 06:28:42 +00001359 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001360 case ISD::EXTLOAD:
1361 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001362 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001363 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001364 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001365 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001366
Nate Begeman5e966612005-03-24 06:28:42 +00001367 // Make sure we generate both values.
1368 if (Result != 1)
1369 ExprMap[N.getValue(1)] = 1; // Generate the token
1370 else
1371 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1372
1373 SDOperand Chain = N.getOperand(0);
1374 SDOperand Address = N.getOperand(1);
1375 Select(Chain);
1376
Nate Begeman9db505c2005-03-28 19:36:43 +00001377 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001378 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001379 case MVT::i1: Opc = PPC::LBZ; break;
1380 case MVT::i8: Opc = PPC::LBZ; break;
1381 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1382 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001383 case MVT::f32: Opc = PPC::LFS; break;
1384 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001385 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001386
Nate Begeman74d73452005-03-31 00:15:26 +00001387 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1388 Tmp1 = MakeReg(MVT::i32);
1389 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +00001390 if (PICEnabled)
1391 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1392 .addConstantPoolIndex(CPI);
1393 else
1394 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +00001395 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001396 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001397 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1398 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001399 } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){
1400 GlobalValue *GV = GN->getGlobal();
1401 Tmp1 = MakeReg(MVT::i32);
1402 if (PICEnabled)
1403 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1404 .addGlobalAddress(GV);
1405 else
Chris Lattner4015ea82005-07-28 04:42:11 +00001406 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman2497e632005-07-21 20:44:43 +00001407 if (GV->hasWeakLinkage() || GV->isExternal()) {
1408 Tmp2 = MakeReg(MVT::i32);
1409 BuildMI(BB, PPC::LWZ, 2, Tmp2).addGlobalAddress(GV).addReg(Tmp1);
Nate Begeman7b4f0a82005-07-25 21:15:28 +00001410 BuildMI(BB, Opc, 2, Result).addSImm(0).addReg(Tmp2);
1411 } else {
1412 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001413 }
Nate Begeman5e966612005-03-24 06:28:42 +00001414 } else {
1415 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001416 switch(SelectAddr(Address, Tmp1, offset)) {
1417 default: assert(0 && "Unhandled return value from SelectAddr");
1418 case 0: // imm offset, no frame, no index
1419 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1420 break;
1421 case 1: // imm offset + frame index
1422 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
1423 break;
1424 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +00001425 Opc = IndexedOpForOp(Opc);
1426 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001427 break;
Nate Begeman04730362005-04-01 04:45:11 +00001428 }
Nate Begeman5e966612005-03-24 06:28:42 +00001429 }
1430 return Result;
1431 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001432
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001433 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001434 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001435 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001436 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001437 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1438 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1439 };
1440 static const unsigned FPR[] = {
1441 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1442 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1443 };
1444
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001445 // Lower the chain for this call.
1446 Select(N.getOperand(0));
1447 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001448
Nate Begemand860aa62005-04-04 22:17:48 +00001449 MachineInstr *CallMI;
1450 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001451 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001452 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001453 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001454 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001455 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001456 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001457 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001458 true);
1459 } else {
1460 Tmp1 = SelectExpr(N.getOperand(1));
1461 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1462 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1463 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1464 .addReg(PPC::R12);
1465 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001466
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001467 // Load the register args to virtual regs
1468 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001469 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001470 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1471
1472 // Copy the virtual registers into the appropriate argument register
1473 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1474 switch(N.getOperand(i+2).getValueType()) {
1475 default: Node->dump(); assert(0 && "Unknown value type for call");
1476 case MVT::i1:
1477 case MVT::i8:
1478 case MVT::i16:
1479 case MVT::i32:
1480 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001481 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001482 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001483 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1484 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001485 ++GPR_idx;
1486 break;
1487 case MVT::f64:
1488 case MVT::f32:
1489 assert(FPR_idx < 13 && "Too many fp args");
1490 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001491 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001492 ++FPR_idx;
1493 break;
1494 }
1495 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001496
Nate Begemand860aa62005-04-04 22:17:48 +00001497 // Put the call instruction in the correct place in the MachineBasicBlock
1498 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001499
1500 switch (Node->getValueType(0)) {
1501 default: assert(0 && "Unknown value type for call result!");
1502 case MVT::Other: return 1;
1503 case MVT::i1:
1504 case MVT::i8:
1505 case MVT::i16:
1506 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001507 if (Node->getValueType(1) == MVT::i32) {
1508 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1509 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1510 } else {
1511 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1512 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001513 break;
1514 case MVT::f32:
1515 case MVT::f64:
1516 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1517 break;
1518 }
1519 return Result+N.ResNo;
1520 }
Nate Begemana9795f82005-03-24 04:41:43 +00001521
1522 case ISD::SIGN_EXTEND:
1523 case ISD::SIGN_EXTEND_INREG:
1524 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001525 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001526 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001527 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001528 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001529 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001530 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001531 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001532 break;
Nate Begeman74747862005-03-29 22:24:51 +00001533 case MVT::i1:
1534 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1535 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001536 }
Nate Begemana9795f82005-03-24 04:41:43 +00001537 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001538
Nate Begemana9795f82005-03-24 04:41:43 +00001539 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001540 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001541 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001542 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001543 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001544 if (MVT::isInteger(DestType))
1545 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1546 else
1547 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001548 return Result;
1549
1550 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001551 Tmp1 = SelectExpr(N.getOperand(0));
1552 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1553 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001554 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001555 .addImm(31-Tmp2);
1556 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001557 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001558 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1559 }
1560 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001561
Nate Begeman5e966612005-03-24 06:28:42 +00001562 case ISD::SRL:
1563 Tmp1 = SelectExpr(N.getOperand(0));
1564 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1565 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001566 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001567 .addImm(Tmp2).addImm(31);
1568 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001569 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001570 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1571 }
1572 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001573
Nate Begeman5e966612005-03-24 06:28:42 +00001574 case ISD::SRA:
1575 Tmp1 = SelectExpr(N.getOperand(0));
1576 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1577 Tmp2 = CN->getValue() & 0x1F;
1578 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1579 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001580 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001581 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1582 }
1583 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001584
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001585 case ISD::CTLZ:
1586 Tmp1 = SelectExpr(N.getOperand(0));
1587 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1588 return Result;
1589
Nate Begemana9795f82005-03-24 04:41:43 +00001590 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001591 if (!MVT::isInteger(DestType)) {
1592 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1593 N.getOperand(0).Val->hasOneUse()) {
1594 ++FusedFP; // Statistic
1595 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1596 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1597 Tmp3 = SelectExpr(N.getOperand(1));
1598 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1599 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1600 return Result;
1601 }
1602 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1603 N.getOperand(1).Val->hasOneUse()) {
1604 ++FusedFP; // Statistic
1605 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1606 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1607 Tmp3 = SelectExpr(N.getOperand(0));
1608 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1609 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1610 return Result;
1611 }
1612 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1613 Tmp1 = SelectExpr(N.getOperand(0));
1614 Tmp2 = SelectExpr(N.getOperand(1));
1615 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1616 return Result;
1617 }
Nate Begemana9795f82005-03-24 04:41:43 +00001618 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001619 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001620 default: assert(0 && "unhandled result code");
1621 case 0: // No immediate
1622 Tmp2 = SelectExpr(N.getOperand(1));
1623 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1624 break;
1625 case 1: // Low immediate
1626 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1627 break;
1628 case 2: // Shifted immediate
1629 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1630 break;
1631 }
1632 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001633
Nate Begemana9795f82005-03-24 04:41:43 +00001634 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001635 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1636 default: assert(0 && "unhandled result code");
1637 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001638 // Check for andc: and, (xor a, -1), b
1639 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1640 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1641 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1642 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1643 Tmp2 = SelectExpr(N.getOperand(1));
1644 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1645 return Result;
1646 }
1647 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001648 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001649 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001650 Opc = Recording ? PPC::ANDo : PPC::AND;
1651 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001652 break;
1653 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001654 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001655 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1656 break;
1657 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001658 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001659 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1660 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001661 case 5: // Bitfield mask
1662 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1663 Tmp3 = Tmp2 >> 16; // MB
1664 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001665
Nate Begeman3dee1752005-07-27 23:11:27 +00001666 // FIXME: Catch SHL-AND in addition to SRL-AND in this block.
Chris Lattnercafb67b2005-05-09 17:39:48 +00001667 if (N.getOperand(0).getOpcode() == ISD::SRL)
1668 if (ConstantSDNode *SA =
1669 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1670
1671 // We can fold the RLWINM and the SRL together if the mask is
1672 // clearing the top bits which are rotated around.
1673 unsigned RotAmt = 32-(SA->getValue() & 31);
1674 if (Tmp2 <= RotAmt) {
1675 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1676 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1677 .addImm(Tmp3).addImm(Tmp2);
1678 break;
1679 }
1680 }
1681
1682 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001683 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1684 .addImm(Tmp3).addImm(Tmp2);
1685 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001686 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001687 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001688 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001689
Nate Begemana9795f82005-03-24 04:41:43 +00001690 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001691 if (SelectBitfieldInsert(N, Result))
1692 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001693 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001694 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001695 default: assert(0 && "unhandled result code");
1696 case 0: // No immediate
1697 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001698 Opc = Recording ? PPC::ORo : PPC::OR;
1699 RecordSuccess = true;
1700 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001701 break;
1702 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001703 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001704 break;
1705 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001706 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001707 break;
1708 }
1709 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001710
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001711 case ISD::XOR: {
1712 // Check for EQV: xor, (xor a, -1), b
1713 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1714 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1715 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001716 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1717 Tmp2 = SelectExpr(N.getOperand(1));
1718 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1719 return Result;
1720 }
Chris Lattner837a5212005-04-21 21:09:11 +00001721 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001722 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1723 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001724 switch(N.getOperand(0).getOpcode()) {
1725 case ISD::OR:
1726 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1727 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1728 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1729 break;
1730 case ISD::AND:
1731 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1732 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1733 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1734 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001735 case ISD::XOR:
1736 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1737 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1738 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1739 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001740 default:
1741 Tmp1 = SelectExpr(N.getOperand(0));
1742 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1743 break;
1744 }
1745 return Result;
1746 }
1747 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001748 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001749 default: assert(0 && "unhandled result code");
1750 case 0: // No immediate
1751 Tmp2 = SelectExpr(N.getOperand(1));
1752 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1753 break;
1754 case 1: // Low immediate
1755 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1756 break;
1757 case 2: // Shifted immediate
1758 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1759 break;
1760 }
1761 return Result;
1762 }
1763
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001764 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001765 if (!MVT::isInteger(DestType)) {
1766 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1767 N.getOperand(0).Val->hasOneUse()) {
1768 ++FusedFP; // Statistic
1769 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1770 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1771 Tmp3 = SelectExpr(N.getOperand(1));
1772 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1773 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1774 return Result;
1775 }
1776 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1777 N.getOperand(1).Val->hasOneUse()) {
1778 ++FusedFP; // Statistic
1779 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1780 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1781 Tmp3 = SelectExpr(N.getOperand(0));
1782 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1783 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1784 return Result;
1785 }
1786 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1787 Tmp1 = SelectExpr(N.getOperand(0));
1788 Tmp2 = SelectExpr(N.getOperand(1));
1789 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1790 return Result;
1791 }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001792 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
1793 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001794 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001795 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00001796 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001797 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1798 } else {
1799 Tmp1 = SelectExpr(N.getOperand(0));
1800 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001801 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1802 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001803 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001804
Nate Begeman5e966612005-03-24 06:28:42 +00001805 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001806 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001807 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001808 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1809 else {
1810 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001811 switch (DestType) {
1812 default: assert(0 && "Unknown type to ISD::MUL"); break;
1813 case MVT::i32: Opc = PPC::MULLW; break;
1814 case MVT::f32: Opc = PPC::FMULS; break;
1815 case MVT::f64: Opc = PPC::FMUL; break;
1816 }
1817 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001818 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001819 return Result;
1820
Nate Begeman815d6da2005-04-06 00:25:27 +00001821 case ISD::MULHS:
1822 case ISD::MULHU:
1823 Tmp1 = SelectExpr(N.getOperand(0));
1824 Tmp2 = SelectExpr(N.getOperand(1));
1825 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1826 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1827 return Result;
1828
Nate Begemanf3d08f32005-03-29 00:03:27 +00001829 case ISD::SDIV:
1830 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001831 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1832 default: break;
1833 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1834 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001835 Tmp1 = MakeReg(MVT::i32);
1836 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001837 if ((int)Tmp3 < 0) {
1838 unsigned Tmp4 = MakeReg(MVT::i32);
1839 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 } else {
1843 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1844 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1845 }
Nate Begeman80196b12005-04-05 00:15:08 +00001846 return Result;
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.
Nate Begeman27b4c232005-04-06 06:44:57 +00001849 case 4:
1850 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001851 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00001852 return SelectExpr(BuildSDIVSequence(N));
1853 else
1854 return SelectExpr(BuildUDIVSequence(N));
Jeff Cohen00b168892005-07-27 06:12:32 +00001855 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001856 Tmp1 = SelectExpr(N.getOperand(0));
1857 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001858 switch (DestType) {
1859 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1860 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1861 case MVT::f32: Opc = PPC::FDIVS; break;
1862 case MVT::f64: Opc = PPC::FDIV; break;
1863 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1865 return Result;
1866
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001867 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001868 case ISD::SUB_PARTS: {
1869 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1870 "Not an i64 add/sub!");
1871 // Emit all of the operands.
1872 std::vector<unsigned> InVals;
1873 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1874 InVals.push_back(SelectExpr(N.getOperand(i)));
1875 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001876 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1877 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001878 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001879 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1880 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1881 }
1882 return Result+N.ResNo;
1883 }
1884
1885 case ISD::SHL_PARTS:
1886 case ISD::SRA_PARTS:
1887 case ISD::SRL_PARTS: {
1888 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1889 "Not an i64 shift!");
1890 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1891 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001892 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1893 Tmp1 = MakeReg(MVT::i32);
1894 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001895 Tmp3 = MakeReg(MVT::i32);
1896 unsigned Tmp4 = MakeReg(MVT::i32);
1897 unsigned Tmp5 = MakeReg(MVT::i32);
1898 unsigned Tmp6 = MakeReg(MVT::i32);
1899 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1900 if (ISD::SHL_PARTS == opcode) {
1901 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1902 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1903 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1904 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001905 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001906 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1907 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1908 } else if (ISD::SRL_PARTS == opcode) {
1909 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1910 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1911 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1912 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1913 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1914 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1915 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1916 } else {
1917 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1918 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1919 MachineBasicBlock *OldMBB = BB;
1920 MachineFunction *F = BB->getParent();
1921 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1922 F->getBasicBlockList().insert(It, TmpMBB);
1923 F->getBasicBlockList().insert(It, PhiMBB);
1924 BB->addSuccessor(TmpMBB);
1925 BB->addSuccessor(PhiMBB);
1926 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1927 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1928 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1929 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1930 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1931 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1932 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1933 // Select correct least significant half if the shift amount > 32
1934 BB = TmpMBB;
1935 unsigned Tmp7 = MakeReg(MVT::i32);
1936 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1937 TmpMBB->addSuccessor(PhiMBB);
1938 BB = PhiMBB;
1939 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1940 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001941 }
1942 return Result+N.ResNo;
1943 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001944
Nate Begemana9795f82005-03-24 04:41:43 +00001945 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001946 case ISD::FP_TO_SINT: {
1947 bool U = (ISD::FP_TO_UINT == opcode);
1948 Tmp1 = SelectExpr(N.getOperand(0));
1949 if (!U) {
1950 Tmp2 = MakeReg(MVT::f64);
1951 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1952 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1953 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1954 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1955 return Result;
1956 } else {
1957 unsigned Zero = getConstDouble(0.0);
1958 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1959 unsigned Border = getConstDouble(1LL << 31);
1960 unsigned UseZero = MakeReg(MVT::f64);
1961 unsigned UseMaxInt = MakeReg(MVT::f64);
1962 unsigned UseChoice = MakeReg(MVT::f64);
1963 unsigned TmpReg = MakeReg(MVT::f64);
1964 unsigned TmpReg2 = MakeReg(MVT::f64);
1965 unsigned ConvReg = MakeReg(MVT::f64);
1966 unsigned IntTmp = MakeReg(MVT::i32);
1967 unsigned XorReg = MakeReg(MVT::i32);
1968 MachineFunction *F = BB->getParent();
1969 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1970 // Update machine-CFG edges
1971 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1972 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1973 MachineBasicBlock *OldMBB = BB;
1974 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1975 F->getBasicBlockList().insert(It, XorMBB);
1976 F->getBasicBlockList().insert(It, PhiMBB);
1977 BB->addSuccessor(XorMBB);
1978 BB->addSuccessor(PhiMBB);
1979 // Convert from floating point to unsigned 32-bit value
1980 // Use 0 if incoming value is < 0.0
1981 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1982 // Use 2**32 - 1 if incoming value is >= 2**32
1983 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1984 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1985 .addReg(MaxInt);
1986 // Subtract 2**31
1987 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1988 // Use difference if >= 2**31
1989 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1990 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1991 .addReg(UseChoice);
1992 // Convert to integer
1993 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1994 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1995 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1996 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1997 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1998
1999 // XorMBB:
2000 // add 2**31 if input was >= 2**31
2001 BB = XorMBB;
2002 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2003 XorMBB->addSuccessor(PhiMBB);
2004
2005 // PhiMBB:
2006 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2007 BB = PhiMBB;
2008 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2009 .addReg(XorReg).addMBB(XorMBB);
2010 return Result;
2011 }
2012 assert(0 && "Should never get here");
2013 return 0;
2014 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002015
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002016 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002017 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002018 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002019 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002020 // We can codegen setcc op, imm very efficiently compared to a brcond.
2021 // Check for those cases here.
2022 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002023 if (CN->getValue() == 0) {
2024 Tmp1 = SelectExpr(SetCC->getOperand(0));
2025 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002026 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002027 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002028 Tmp2 = MakeReg(MVT::i32);
2029 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2030 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2031 .addImm(5).addImm(31);
2032 break;
2033 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002034 Tmp2 = MakeReg(MVT::i32);
2035 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2036 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2037 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002038 case ISD::SETLT:
2039 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2040 .addImm(31).addImm(31);
2041 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002042 case ISD::SETGT:
2043 Tmp2 = MakeReg(MVT::i32);
2044 Tmp3 = MakeReg(MVT::i32);
2045 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2046 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2047 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2048 .addImm(31).addImm(31);
2049 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002050 }
2051 return Result;
2052 }
2053 // setcc op, -1
2054 if (CN->isAllOnesValue()) {
2055 Tmp1 = SelectExpr(SetCC->getOperand(0));
2056 switch (SetCC->getCondition()) {
2057 default: assert(0 && "Unhandled SetCC condition"); abort();
2058 case ISD::SETEQ:
2059 Tmp2 = MakeReg(MVT::i32);
2060 Tmp3 = MakeReg(MVT::i32);
2061 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2062 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2063 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002064 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002065 case ISD::SETNE:
2066 Tmp2 = MakeReg(MVT::i32);
2067 Tmp3 = MakeReg(MVT::i32);
2068 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2069 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2070 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2071 break;
2072 case ISD::SETLT:
2073 Tmp2 = MakeReg(MVT::i32);
2074 Tmp3 = MakeReg(MVT::i32);
2075 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2076 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2077 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2078 .addImm(31).addImm(31);
2079 break;
2080 case ISD::SETGT:
2081 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002082 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2083 .addImm(31).addImm(31);
2084 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2085 break;
2086 }
2087 return Result;
2088 }
2089 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002090
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002091 bool Inv;
2092 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2093 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002094 return Result;
2095 }
2096 assert(0 && "Is this legal?");
2097 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002098
Nate Begeman74747862005-03-29 22:24:51 +00002099 case ISD::SELECT: {
Nate Begemana3fd4002005-07-19 16:51:05 +00002100 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
2101 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
2102 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
2103 !MVT::isInteger(N.getOperand(1).getValueType()) &&
2104 !MVT::isInteger(N.getOperand(2).getValueType()) &&
2105 SetCC->getCondition() != ISD::SETEQ &&
2106 SetCC->getCondition() != ISD::SETNE) {
2107 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
2108 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2109 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2110
2111 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
2112 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
2113 switch(SetCC->getCondition()) {
2114 default: assert(0 && "Invalid FSEL condition"); abort();
2115 case ISD::SETULT:
2116 case ISD::SETLT:
2117 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2118 case ISD::SETUGE:
2119 case ISD::SETGE:
2120 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2121 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2122 return Result;
2123 case ISD::SETUGT:
2124 case ISD::SETGT:
2125 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2126 case ISD::SETULE:
2127 case ISD::SETLE: {
2128 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
2129 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
2130 } else {
2131 Tmp2 = MakeReg(VT);
2132 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2133 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2134 }
2135 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2136 return Result;
2137 }
2138 }
2139 } else {
2140 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
2141 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2142 Tmp2 = SelectExpr(SetCC->getOperand(1));
2143 Tmp3 = MakeReg(VT);
2144 switch(SetCC->getCondition()) {
2145 default: assert(0 && "Invalid FSEL condition"); abort();
2146 case ISD::SETULT:
2147 case ISD::SETLT:
2148 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2149 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2150 return Result;
2151 case ISD::SETUGE:
2152 case ISD::SETGE:
2153 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2154 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2155 return Result;
2156 case ISD::SETUGT:
2157 case ISD::SETGT:
2158 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2159 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2160 return Result;
2161 case ISD::SETULE:
2162 case ISD::SETLE:
2163 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2164 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2165 return Result;
2166 }
2167 }
2168 assert(0 && "Should never get here");
2169 return 0;
2170 }
2171
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002172 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002173 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2174 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002175 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002176
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002177 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002178 // value and the MBB to hold the PHI instruction for this SetCC.
2179 MachineBasicBlock *thisMBB = BB;
2180 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2181 ilist<MachineBasicBlock>::iterator It = BB;
2182 ++It;
2183
2184 // thisMBB:
2185 // ...
2186 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002187 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002188 // bCC copy1MBB
2189 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002190 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2191 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002192 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002193 MachineFunction *F = BB->getParent();
2194 F->getBasicBlockList().insert(It, copy0MBB);
2195 F->getBasicBlockList().insert(It, sinkMBB);
2196 // Update machine-CFG edges
2197 BB->addSuccessor(copy0MBB);
2198 BB->addSuccessor(sinkMBB);
2199
2200 // copy0MBB:
2201 // %FalseValue = ...
2202 // # fallthrough to sinkMBB
2203 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002204 // Update machine-CFG edges
2205 BB->addSuccessor(sinkMBB);
2206
2207 // sinkMBB:
2208 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2209 // ...
2210 BB = sinkMBB;
2211 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2212 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002213 return Result;
2214 }
Nate Begemana9795f82005-03-24 04:41:43 +00002215
2216 case ISD::Constant:
2217 switch (N.getValueType()) {
2218 default: assert(0 && "Cannot use constants of this type!");
2219 case MVT::i1:
2220 BuildMI(BB, PPC::LI, 1, Result)
2221 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2222 break;
2223 case MVT::i32:
2224 {
2225 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2226 if (v < 32768 && v >= -32768) {
2227 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2228 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002229 Tmp1 = MakeReg(MVT::i32);
2230 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2231 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002232 }
2233 }
2234 }
2235 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002236
2237 case ISD::ConstantFP: {
2238 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2239 Result = getConstDouble(CN->getValue(), Result);
2240 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002241 }
2242
Nate Begemana3fd4002005-07-19 16:51:05 +00002243 case ISD::FNEG:
2244 if (!NoExcessFPPrecision &&
2245 ISD::ADD == N.getOperand(0).getOpcode() &&
2246 N.getOperand(0).Val->hasOneUse() &&
2247 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2248 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2249 ++FusedFP; // Statistic
2250 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2251 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2252 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2253 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2254 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2255 } else if (!NoExcessFPPrecision &&
2256 ISD::ADD == N.getOperand(0).getOpcode() &&
2257 N.getOperand(0).Val->hasOneUse() &&
2258 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2259 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2260 ++FusedFP; // Statistic
2261 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2262 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2263 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2264 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2265 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2266 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2267 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2268 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2269 } else {
2270 Tmp1 = SelectExpr(N.getOperand(0));
2271 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2272 }
2273 return Result;
2274
2275 case ISD::FABS:
2276 Tmp1 = SelectExpr(N.getOperand(0));
2277 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2278 return Result;
2279
Nate Begemanadeb43d2005-07-20 22:42:00 +00002280 case ISD::FSQRT:
2281 Tmp1 = SelectExpr(N.getOperand(0));
2282 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2283 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2284 return Result;
2285
Nate Begemana3fd4002005-07-19 16:51:05 +00002286 case ISD::FP_ROUND:
2287 assert (DestType == MVT::f32 &&
2288 N.getOperand(0).getValueType() == MVT::f64 &&
2289 "only f64 to f32 conversion supported here");
2290 Tmp1 = SelectExpr(N.getOperand(0));
2291 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2292 return Result;
2293
2294 case ISD::FP_EXTEND:
2295 assert (DestType == MVT::f64 &&
2296 N.getOperand(0).getValueType() == MVT::f32 &&
2297 "only f32 to f64 conversion supported here");
2298 Tmp1 = SelectExpr(N.getOperand(0));
2299 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2300 return Result;
2301
2302 case ISD::UINT_TO_FP:
2303 case ISD::SINT_TO_FP: {
2304 assert (N.getOperand(0).getValueType() == MVT::i32
2305 && "int to float must operate on i32");
2306 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2307 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2308 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2309 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2310
2311 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2312 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2313
2314 if (IsUnsigned) {
2315 unsigned ConstF = getConstDouble(0x1.000000p52);
2316 // Store the hi & low halves of the fp value, currently in int regs
2317 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2318 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2319 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2320 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2321 // Generate the return value with a subtract
2322 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2323 } else {
2324 unsigned ConstF = getConstDouble(0x1.000008p52);
2325 unsigned TmpL = MakeReg(MVT::i32);
2326 // Store the hi & low halves of the fp value, currently in int regs
2327 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2328 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2329 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2330 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2331 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2332 // Generate the return value with a subtract
2333 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2334 }
2335 return Result;
2336 }
2337 }
Nate Begemana9795f82005-03-24 04:41:43 +00002338 return 0;
2339}
2340
2341void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00002342 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00002343 unsigned opcode = N.getOpcode();
2344
2345 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2346 return; // Already selected.
2347
2348 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002349
Nate Begemana9795f82005-03-24 04:41:43 +00002350 switch (Node->getOpcode()) {
2351 default:
2352 Node->dump(); std::cerr << "\n";
2353 assert(0 && "Node not handled yet!");
2354 case ISD::EntryToken: return; // Noop
2355 case ISD::TokenFactor:
2356 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2357 Select(Node->getOperand(i));
2358 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002359 case ISD::CALLSEQ_START:
2360 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002361 Select(N.getOperand(0));
2362 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002363 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002364 PPC::ADJCALLSTACKUP;
2365 BuildMI(BB, Opc, 1).addImm(Tmp1);
2366 return;
2367 case ISD::BR: {
2368 MachineBasicBlock *Dest =
2369 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002370 Select(N.getOperand(0));
2371 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2372 return;
2373 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002374 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002375 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002376 SelectBranchCC(N);
2377 return;
2378 case ISD::CopyToReg:
2379 Select(N.getOperand(0));
2380 Tmp1 = SelectExpr(N.getOperand(1));
2381 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002382
Nate Begemana9795f82005-03-24 04:41:43 +00002383 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002384 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002385 N.getOperand(1).getValueType() == MVT::f32)
2386 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2387 else
2388 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2389 }
2390 return;
2391 case ISD::ImplicitDef:
2392 Select(N.getOperand(0));
2393 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2394 return;
2395 case ISD::RET:
2396 switch (N.getNumOperands()) {
2397 default:
2398 assert(0 && "Unknown return instruction!");
2399 case 3:
2400 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2401 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002402 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002403 Select(N.getOperand(0));
2404 Tmp1 = SelectExpr(N.getOperand(1));
2405 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002406 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2407 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002408 break;
2409 case 2:
2410 Select(N.getOperand(0));
2411 Tmp1 = SelectExpr(N.getOperand(1));
2412 switch (N.getOperand(1).getValueType()) {
2413 default:
2414 assert(0 && "Unknown return type!");
2415 case MVT::f64:
2416 case MVT::f32:
2417 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2418 break;
2419 case MVT::i32:
2420 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2421 break;
2422 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002423 case 1:
2424 Select(N.getOperand(0));
2425 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002426 }
2427 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2428 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002429 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00002430 case ISD::STORE: {
2431 SDOperand Chain = N.getOperand(0);
2432 SDOperand Value = N.getOperand(1);
2433 SDOperand Address = N.getOperand(2);
2434 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00002435
Nate Begeman2497e632005-07-21 20:44:43 +00002436 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00002437
Nate Begeman2497e632005-07-21 20:44:43 +00002438 if (opcode == ISD::STORE) {
2439 switch(Value.getValueType()) {
2440 default: assert(0 && "unknown Type in store");
2441 case MVT::i32: Opc = PPC::STW; break;
2442 case MVT::f64: Opc = PPC::STFD; break;
2443 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002444 }
Nate Begeman2497e632005-07-21 20:44:43 +00002445 } else { //ISD::TRUNCSTORE
2446 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
2447 default: assert(0 && "unknown Type in store");
2448 case MVT::i1:
2449 case MVT::i8: Opc = PPC::STB; break;
2450 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002451 }
Nate Begemana9795f82005-03-24 04:41:43 +00002452 }
Nate Begeman2497e632005-07-21 20:44:43 +00002453
2454 if(Address.getOpcode() == ISD::FrameIndex) {
2455 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2456 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
2457 } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){
2458 GlobalValue *GV = GN->getGlobal();
2459 Tmp2 = MakeReg(MVT::i32);
2460 if (PICEnabled)
2461 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
2462 .addGlobalAddress(GV);
2463 else
Chris Lattner4015ea82005-07-28 04:42:11 +00002464 BuildMI(BB, PPC::LIS, 1, Tmp2).addGlobalAddress(GV);
Nate Begeman2497e632005-07-21 20:44:43 +00002465 if (GV->hasWeakLinkage() || GV->isExternal()) {
2466 Tmp3 = MakeReg(MVT::i32);
2467 BuildMI(BB, PPC::LWZ, 2, Tmp3).addGlobalAddress(GV).addReg(Tmp2);
Nate Begeman7b4f0a82005-07-25 21:15:28 +00002468 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(0).addReg(Tmp3);
2469 } else {
2470 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00002471 }
Nate Begeman2497e632005-07-21 20:44:43 +00002472 } else {
2473 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002474 switch(SelectAddr(Address, Tmp2, offset)) {
2475 default: assert(0 && "Unhandled return value from SelectAddr");
2476 case 0: // imm offset, no frame, no index
2477 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
2478 break;
2479 case 1: // imm offset + frame index
2480 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
2481 break;
2482 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00002483 Opc = IndexedOpForOp(Opc);
2484 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002485 break;
Nate Begeman2497e632005-07-21 20:44:43 +00002486 }
2487 }
2488 return;
2489 }
Nate Begemana9795f82005-03-24 04:41:43 +00002490 case ISD::EXTLOAD:
2491 case ISD::SEXTLOAD:
2492 case ISD::ZEXTLOAD:
2493 case ISD::LOAD:
2494 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002495 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002496 case ISD::CALL:
2497 case ISD::DYNAMIC_STACKALLOC:
2498 ExprMap.erase(N);
2499 SelectExpr(N);
2500 return;
2501 }
2502 assert(0 && "Should not be reached!");
2503}
2504
2505
2506/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2507/// into a machine code representation using pattern matching and a machine
2508/// description file.
2509///
2510FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002511 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002512}
2513