Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 5 | // This file was developed by Nate Begeman and is distributed under |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 11 | // Magic number generation for integer divide from the PowerPC Compiler Writer's |
| 12 | // Guide, section 3.2.3.5 |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "PowerPC.h" |
| 17 | #include "PowerPCInstrBuilder.h" |
| 18 | #include "PowerPCInstrInfo.h" |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 19 | #include "PPC32TargetMachine.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 20 | #include "PPC32ISelLowering.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
| 28 | #include "llvm/Target/TargetData.h" |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetOptions.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/MathExtras.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include <set> |
| 34 | #include <algorithm> |
| 35 | using namespace llvm; |
| 36 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 37 | namespace { |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 38 | Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted"); |
| 39 | Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); |
| 40 | Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 41 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 42 | //===--------------------------------------------------------------------===// |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 43 | // ISel - PPC32 specific code to select PPC32 machine instructions for |
| 44 | // SelectionDAG operations. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 45 | //===--------------------------------------------------------------------===// |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 46 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 47 | class ISel : public SelectionDAGISel { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 48 | PPC32TargetLowering PPC32Lowering; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 49 | SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform |
| 50 | // for sdiv and udiv until it is put into the future |
| 51 | // dag combiner. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 52 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 53 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 54 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 55 | /// tree. |
| 56 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 57 | |
| 58 | unsigned GlobalBaseReg; |
| 59 | bool GlobalBaseInitialized; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 60 | bool RecordSuccess; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 61 | public: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 62 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM), |
| 63 | ISelDAG(0) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 64 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 65 | /// runOnFunction - Override this function in order to reset our per-function |
| 66 | /// variables. |
| 67 | virtual bool runOnFunction(Function &Fn) { |
| 68 | // Make sure we re-emit a set of the global base reg if necessary |
| 69 | GlobalBaseInitialized = false; |
| 70 | return SelectionDAGISel::runOnFunction(Fn); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 73 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 74 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 75 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 76 | DEBUG(BB->dump()); |
| 77 | // Codegen the basic block. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 78 | ISelDAG = &DAG; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 79 | Select(DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 80 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 81 | // Clear state used for selection. |
| 82 | ExprMap.clear(); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 83 | ISelDAG = 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 84 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 86 | // convenience functions for virtual register creation |
| 87 | inline unsigned MakeIntReg() { |
| 88 | return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass); |
| 89 | } |
| 90 | inline unsigned MakeFPReg() { |
| 91 | return RegMap->createVirtualRegister(PPC32::FPRCRegisterClass); |
| 92 | } |
| 93 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 94 | // dag -> dag expanders for integer divide by constant |
| 95 | SDOperand BuildSDIVSequence(SDOperand N); |
| 96 | SDOperand BuildUDIVSequence(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 97 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 98 | unsigned getGlobalBaseReg(); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 99 | void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 100 | bool SelectBitfieldInsert(SDOperand OR, unsigned Result); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 101 | unsigned FoldIfWideZeroExtend(SDOperand N); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 102 | unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC); |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 103 | bool SelectIntImmediateExpr(SDOperand N, unsigned Result, |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 104 | unsigned OCHi, unsigned OCLo, |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 105 | bool IsArithmetic = false, bool Negate = false); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 106 | unsigned SelectExpr(SDOperand N, bool Recording=false); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 107 | void Select(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 108 | |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 109 | unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 110 | void SelectBranchCC(SDOperand N); |
Chris Lattner | 3f27013 | 2005-08-02 19:07:49 +0000 | [diff] [blame] | 111 | |
| 112 | virtual const char *getPassName() const { |
| 113 | return "PowerPC Pattern Instruction Selection"; |
| 114 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Chris Lattner | 02efa6c | 2005-08-08 21:08:09 +0000 | [diff] [blame] | 117 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 118 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 119 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 120 | // not, since all 1s are not contiguous. |
| 121 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 122 | if (isShiftedMask_32(Val)) { |
| 123 | // look for the first non-zero bit |
| 124 | MB = CountLeadingZeros_32(Val); |
| 125 | // look for the first zero bit after the run of ones |
| 126 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 127 | return true; |
| 128 | } else if (isShiftedMask_32(Val = ~Val)) { // invert mask |
| 129 | // effectively look for the first zero bit |
| 130 | ME = CountLeadingZeros_32(Val) - 1; |
| 131 | // effectively look for the first one bit after the run of zeros |
| 132 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 133 | return true; |
| 134 | } |
| 135 | // no run present |
| 136 | return false; |
| 137 | } |
| 138 | |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 139 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 140 | // and mask opcode and mask operation. |
| 141 | static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask, |
| 142 | bool IsShiftMask, |
| 143 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 144 | if (Shift > 31) return false; |
| 145 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 146 | |
| 147 | if (Opcode == ISD::SHL) { // shift left |
| 148 | // apply shift to mask if it comes first |
| 149 | if (IsShiftMask) Mask = Mask << Shift; |
| 150 | // determine which bits are made indeterminant by shift |
| 151 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
| 152 | } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights |
| 153 | // apply shift to mask if it comes first |
| 154 | if (IsShiftMask) Mask = Mask >> Shift; |
| 155 | // determine which bits are made indeterminant by shift |
| 156 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 157 | // adjust for the left rotate |
| 158 | Shift = 32 - Shift; |
| 159 | } |
| 160 | |
| 161 | // if the mask doesn't intersect any Indeterminant bits |
Jim Laskey | cf083e3 | 2005-08-12 23:52:46 +0000 | [diff] [blame] | 162 | if (Mask && !(Mask & Indeterminant)) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 163 | SH = Shift; |
| 164 | // make sure the mask is still a mask (wrap arounds may not be) |
| 165 | return isRunOfOnes(Mask, MB, ME); |
| 166 | } |
| 167 | |
| 168 | // can't do it |
| 169 | return false; |
| 170 | } |
| 171 | |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 172 | // isIntImmediate - This method tests to see if a constant operand. |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 173 | // If so Imm will receive the 32 bit value. |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 174 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 175 | // test for constant |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 176 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 177 | // retrieve value |
Jim Laskey | b454cfd | 2005-08-18 00:15:15 +0000 | [diff] [blame] | 178 | Imm = (unsigned)CN->getValue(); |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 179 | // passes muster |
| 180 | return true; |
| 181 | } |
| 182 | // not a constant |
| 183 | return false; |
| 184 | } |
| 185 | |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 186 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 187 | // opcode and that it has a immediate integer right operand. |
| 188 | // If so Imm will receive the 32 bit value. |
| 189 | static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) { |
| 190 | return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm); |
| 191 | } |
| 192 | |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 193 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 194 | // a immediate shift count less than 32. |
| 195 | static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) { |
| 196 | Opc = N.getOpcode(); |
| 197 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 198 | isIntImmediate(N.getOperand(1), SH) && SH < 32; |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 202 | static bool isOprNot(SDOperand N) { |
| 203 | unsigned Imm; |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 204 | return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Immediate constant composers. |
| 208 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 209 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 210 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 211 | // arithmethically. |
| 212 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 213 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 214 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 215 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 216 | /// NodeHasRecordingVariant - If SelectExpr can always produce code for |
| 217 | /// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise, |
| 218 | /// return false. |
| 219 | static bool NodeHasRecordingVariant(unsigned NodeOpcode) { |
| 220 | switch(NodeOpcode) { |
| 221 | default: return false; |
| 222 | case ISD::AND: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 223 | case ISD::OR: |
Chris Lattner | 519f40b | 2005-04-13 02:46:17 +0000 | [diff] [blame] | 224 | return true; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 228 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 229 | /// to Condition. |
| 230 | static unsigned getBCCForSetCC(ISD::CondCode CC) { |
| 231 | switch (CC) { |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 232 | default: assert(0 && "Unknown condition!"); abort(); |
| 233 | case ISD::SETEQ: return PPC::BEQ; |
| 234 | case ISD::SETNE: return PPC::BNE; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 235 | case ISD::SETULT: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 236 | case ISD::SETLT: return PPC::BLT; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 237 | case ISD::SETULE: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 238 | case ISD::SETLE: return PPC::BLE; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 239 | case ISD::SETUGT: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 240 | case ISD::SETGT: return PPC::BGT; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 241 | case ISD::SETUGE: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 242 | case ISD::SETGE: return PPC::BGE; |
| 243 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 247 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 248 | /// associated with the SetCC condition, and whether or not the field is |
| 249 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 250 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) { |
| 251 | switch (CC) { |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 252 | default: assert(0 && "Unknown condition!"); abort(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 253 | case ISD::SETULT: |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 254 | case ISD::SETLT: Inv = false; return 0; |
| 255 | case ISD::SETUGE: |
| 256 | case ISD::SETGE: Inv = true; return 0; |
| 257 | case ISD::SETUGT: |
| 258 | case ISD::SETGT: Inv = false; return 1; |
| 259 | case ISD::SETULE: |
| 260 | case ISD::SETLE: Inv = true; return 1; |
| 261 | case ISD::SETEQ: Inv = false; return 2; |
| 262 | case ISD::SETNE: Inv = true; return 2; |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 267 | /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load |
| 268 | /// and store immediate instructions. |
| 269 | static unsigned IndexedOpForOp(unsigned Opcode) { |
| 270 | switch(Opcode) { |
| 271 | default: assert(0 && "Unknown opcode!"); abort(); |
| 272 | case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX; |
| 273 | case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX; |
| 274 | case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX; |
| 275 | case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX; |
| 276 | case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX; |
| 277 | case PPC::LFD: return PPC::LFDX; |
| 278 | } |
| 279 | return 0; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 280 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 281 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 282 | // Structure used to return the necessary information to codegen an SDIV as |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 283 | // a multiply. |
| 284 | struct ms { |
| 285 | int m; // magic number |
| 286 | int s; // shift amount |
| 287 | }; |
| 288 | |
| 289 | struct mu { |
| 290 | unsigned int m; // magic number |
| 291 | int a; // add indicator |
| 292 | int s; // shift amount |
| 293 | }; |
| 294 | |
| 295 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 296 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 297 | /// or -1. |
| 298 | static struct ms magic(int d) { |
| 299 | int p; |
| 300 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 301 | const unsigned int two31 = 0x80000000U; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 302 | struct ms mag; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 303 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 304 | ad = abs(d); |
| 305 | t = two31 + ((unsigned int)d >> 31); |
| 306 | anc = t - 1 - t%ad; // absolute value of nc |
| 307 | p = 31; // initialize p |
| 308 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 309 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 310 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 311 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 312 | do { |
| 313 | p = p + 1; |
| 314 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 315 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 316 | if (r1 >= anc) { // must be unsigned comparison |
| 317 | q1 = q1 + 1; |
| 318 | r1 = r1 - anc; |
| 319 | } |
| 320 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 321 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 322 | if (r2 >= ad) { // must be unsigned comparison |
| 323 | q2 = q2 + 1; |
| 324 | r2 = r2 - ad; |
| 325 | } |
| 326 | delta = ad - r2; |
| 327 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 328 | |
| 329 | mag.m = q2 + 1; |
| 330 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 331 | mag.s = p - 32; // resulting shift |
| 332 | return mag; |
| 333 | } |
| 334 | |
| 335 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 336 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 337 | static struct mu magicu(unsigned d) |
| 338 | { |
| 339 | int p; |
| 340 | unsigned int nc, delta, q1, r1, q2, r2; |
| 341 | struct mu magu; |
| 342 | magu.a = 0; // initialize "add" indicator |
| 343 | nc = - 1 - (-d)%d; |
| 344 | p = 31; // initialize p |
| 345 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 346 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 347 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 348 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 349 | do { |
| 350 | p = p + 1; |
| 351 | if (r1 >= nc - r1 ) { |
| 352 | q1 = 2*q1 + 1; // update q1 |
| 353 | r1 = 2*r1 - nc; // update r1 |
| 354 | } |
| 355 | else { |
| 356 | q1 = 2*q1; // update q1 |
| 357 | r1 = 2*r1; // update r1 |
| 358 | } |
| 359 | if (r2 + 1 >= d - r2) { |
| 360 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 361 | q2 = 2*q2 + 1; // update q2 |
| 362 | r2 = 2*r2 + 1 - d; // update r2 |
| 363 | } |
| 364 | else { |
| 365 | if (q2 >= 0x80000000) magu.a = 1; |
| 366 | q2 = 2*q2; // update q2 |
| 367 | r2 = 2*r2 + 1; // update r2 |
| 368 | } |
| 369 | delta = d - 1 - r2; |
| 370 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 371 | magu.m = q2 + 1; // resulting magic number |
| 372 | magu.s = p - 32; // resulting shift |
| 373 | return magu; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 378 | /// return a DAG expression to select that will generate the same value by |
| 379 | /// multiplying by a magic number. See: |
| 380 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 381 | SDOperand ISel::BuildSDIVSequence(SDOperand N) { |
| 382 | int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 383 | ms magics = magic(d); |
| 384 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 385 | SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 386 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 387 | // If d > 0 and m < 0, add the numerator |
| 388 | if (d > 0 && magics.m < 0) |
| 389 | Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0)); |
| 390 | // If d < 0 and m > 0, subtract the numerator. |
| 391 | if (d < 0 && magics.m > 0) |
| 392 | Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0)); |
| 393 | // Shift right algebraic if shift value is nonzero |
| 394 | if (magics.s > 0) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 395 | Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 396 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 397 | // Extract the sign bit and add it to the quotient |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 398 | SDOperand T = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 399 | ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32)); |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 400 | return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 404 | /// return a DAG expression to select that will generate the same value by |
| 405 | /// multiplying by a magic number. See: |
| 406 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 407 | SDOperand ISel::BuildUDIVSequence(SDOperand N) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 408 | unsigned d = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 409 | (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 410 | mu magics = magicu(d); |
| 411 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 412 | SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 413 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 414 | if (magics.a == 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 415 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 416 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 417 | } else { |
| 418 | SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 419 | NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 420 | ISelDAG->getConstant(1, MVT::i32)); |
| 421 | NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 422 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 423 | ISelDAG->getConstant(magics.s-1, MVT::i32)); |
| 424 | } |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 425 | return Q; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 428 | /// getGlobalBaseReg - Output the instructions required to put the |
| 429 | /// base address to use for accessing globals into a register. |
| 430 | /// |
| 431 | unsigned ISel::getGlobalBaseReg() { |
| 432 | if (!GlobalBaseInitialized) { |
| 433 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 434 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 435 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 436 | GlobalBaseReg = MakeIntReg(); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 437 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
Chris Lattner | 3f852b4 | 2005-08-18 23:24:50 +0000 | [diff] [blame] | 438 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 439 | GlobalBaseInitialized = true; |
| 440 | } |
| 441 | return GlobalBaseReg; |
| 442 | } |
| 443 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 444 | /// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 445 | /// Inv is true, then invert the result. |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 446 | void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){ |
| 447 | bool Inv; |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 448 | unsigned IntCR = MakeIntReg(); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 449 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 450 | BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 451 | bool GPOpt = |
| 452 | TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor(); |
Nate Begeman | 27d53ba | 2005-08-19 03:42:28 +0000 | [diff] [blame] | 453 | if (GPOpt) |
| 454 | BuildMI(BB, PPC::MFOCRF, 1, IntCR).addReg(PPC::CR7); |
| 455 | else |
| 456 | BuildMI(BB, PPC::MFCR, 0, IntCR); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 457 | if (Inv) { |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 458 | unsigned Tmp1 = MakeIntReg(); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 459 | BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx)) |
| 460 | .addImm(31).addImm(31); |
| 461 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1); |
| 462 | } else { |
| 463 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx)) |
| 464 | .addImm(31).addImm(31); |
| 465 | } |
| 466 | } |
| 467 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 468 | /// SelectBitfieldInsert - turn an or of two masked values into |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 469 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 470 | /// Returns true on success, false if the caller still needs to select OR. |
| 471 | /// |
| 472 | /// Patterns matched: |
| 473 | /// 1. or shl, and 5. or and, and |
| 474 | /// 2. or and, shl 6. or shl, shr |
| 475 | /// 3. or shr, and 7. or shr, shl |
| 476 | /// 4. or and, shr |
| 477 | bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 478 | bool IsRotate = false; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 479 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0; |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 480 | unsigned Value; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 481 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 482 | SDOperand Op0 = OR.getOperand(0); |
| 483 | SDOperand Op1 = OR.getOperand(1); |
| 484 | |
| 485 | unsigned Op0Opc = Op0.getOpcode(); |
| 486 | unsigned Op1Opc = Op1.getOpcode(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 487 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 488 | // Verify that we have the correct opcodes |
| 489 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 490 | return false; |
| 491 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 492 | return false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 493 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 494 | // Generate Mask value for Target |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 495 | if (isIntImmediate(Op0.getOperand(1), Value)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 496 | switch(Op0Opc) { |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 497 | case ISD::SHL: TgtMask <<= Value; break; |
| 498 | case ISD::SRL: TgtMask >>= Value; break; |
| 499 | case ISD::AND: TgtMask &= Value; break; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 500 | } |
| 501 | } else { |
| 502 | return false; |
| 503 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 504 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 505 | // Generate Mask value for Insert |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 506 | if (isIntImmediate(Op1.getOperand(1), Value)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 507 | switch(Op1Opc) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 508 | case ISD::SHL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 509 | Amount = Value; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 510 | InsMask <<= Amount; |
| 511 | if (Op0Opc == ISD::SRL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 512 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 513 | case ISD::SRL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 514 | Amount = Value; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 515 | InsMask >>= Amount; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 516 | Amount = 32-Amount; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 517 | if (Op0Opc == ISD::SHL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 518 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 519 | case ISD::AND: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 520 | InsMask &= Value; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } else { |
| 524 | return false; |
| 525 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 526 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 527 | unsigned Tmp3 = 0; |
| 528 | |
| 529 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 530 | // constant as its input, make that the inserted value so that we can combine |
| 531 | // the shift into the rotate part of the rlwimi instruction |
| 532 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 533 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 534 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 535 | if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 536 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 537 | Value : 32 - Value; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 538 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 539 | } |
| 540 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 541 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 542 | if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) { |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 543 | std::swap(Op0, Op1); |
| 544 | std::swap(TgtMask, InsMask); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 545 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 546 | Value : 32 - Value; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 547 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 552 | // Verify that the Target mask and Insert mask together form a full word mask |
| 553 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 554 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 555 | // instruction. |
| 556 | unsigned MB, ME; |
Chris Lattner | 02efa6c | 2005-08-08 21:08:09 +0000 | [diff] [blame] | 557 | if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 558 | unsigned Tmp1, Tmp2; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 559 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 560 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 561 | // where both bitfield halves are sourced from the same value. |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 562 | if (IsRotate && fullMask && |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 563 | OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 564 | Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0)); |
| 565 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount) |
| 566 | .addImm(0).addImm(31); |
| 567 | return true; |
| 568 | } |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 569 | if (Op0Opc == ISD::AND && fullMask) |
| 570 | Tmp1 = SelectExpr(Op0.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 571 | else |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 572 | Tmp1 = SelectExpr(Op0); |
| 573 | Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 574 | BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2) |
| 575 | .addImm(Amount).addImm(MB).addImm(ME); |
| 576 | return true; |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 581 | /// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the |
| 582 | /// low six bits. If the shift amount is an ISD::AND node with a mask that is |
| 583 | /// wider than the implicit mask, then we can get rid of the AND and let the |
| 584 | /// shift do the mask. |
| 585 | unsigned ISel::FoldIfWideZeroExtend(SDOperand N) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 586 | unsigned C; |
| 587 | if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63) |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 588 | return SelectExpr(N.getOperand(0)); |
| 589 | else |
| 590 | return SelectExpr(N); |
| 591 | } |
| 592 | |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 593 | unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 594 | unsigned Result, Tmp1, Tmp2; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 595 | bool AlreadySelected = false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 596 | static const unsigned CompareOpcodes[] = |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 597 | { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 598 | |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 599 | // Allocate a condition register for this expression |
| 600 | Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 601 | |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 602 | // Use U to determine whether the SETCC immediate range is signed or not. |
| 603 | bool U = ISD::isUnsignedIntSetCC(CC); |
| 604 | if (isIntImmediate(RHS, Tmp2) && |
| 605 | ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) { |
| 606 | Tmp2 = Lo16(Tmp2); |
| 607 | // For comparisons against zero, we can implicity set CR0 if a recording |
| 608 | // variant (e.g. 'or.' instead of 'or') of the instruction that defines |
| 609 | // operand zero of the SetCC node is available. |
| 610 | if (Tmp2 == 0 && |
| 611 | NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) { |
| 612 | RecordSuccess = false; |
| 613 | Tmp1 = SelectExpr(LHS, true); |
| 614 | if (RecordSuccess) { |
| 615 | ++Recorded; |
| 616 | BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0); |
| 617 | return Result; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 618 | } |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 619 | AlreadySelected = true; |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 620 | } |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 621 | // If we could not implicitly set CR0, then emit a compare immediate |
| 622 | // instead. |
| 623 | if (!AlreadySelected) Tmp1 = SelectExpr(LHS); |
| 624 | if (U) |
| 625 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 626 | else |
| 627 | BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 628 | } else { |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 629 | bool IsInteger = MVT::isInteger(LHS.getValueType()); |
| 630 | unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U]; |
| 631 | Tmp1 = SelectExpr(LHS); |
| 632 | Tmp2 = SelectExpr(RHS); |
| 633 | BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 634 | } |
| 635 | return Result; |
| 636 | } |
| 637 | |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 638 | /// Check to see if the load is a constant offset from a base register. |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 639 | unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 640 | { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 641 | unsigned imm = 0, opcode = N.getOpcode(); |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 642 | if (N.getOpcode() == ISD::ADD) { |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 643 | bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex; |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 644 | if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) { |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 645 | offset = Lo16(imm); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 646 | if (isFrame) { |
| 647 | ++FrameOff; |
| 648 | Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex(); |
| 649 | return 1; |
| 650 | } else { |
| 651 | Reg = SelectExpr(N.getOperand(0)); |
| 652 | return 0; |
| 653 | } |
| 654 | } else { |
| 655 | Reg = SelectExpr(N.getOperand(0)); |
| 656 | offset = SelectExpr(N.getOperand(1)); |
| 657 | return 2; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 658 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 659 | } |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 660 | // Now check if we're dealing with a global, and whether or not we should emit |
| 661 | // an optimized load or store for statics. |
| 662 | if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) { |
| 663 | GlobalValue *GV = GN->getGlobal(); |
| 664 | if (!GV->hasWeakLinkage() && !GV->isExternal()) { |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 665 | unsigned GlobalHi = MakeIntReg(); |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 666 | if (PICEnabled) |
| 667 | BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg()) |
| 668 | .addGlobalAddress(GV); |
| 669 | else |
| 670 | BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV); |
| 671 | Reg = GlobalHi; |
| 672 | offset = 0; |
| 673 | return 3; |
| 674 | } |
| 675 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 676 | Reg = SelectExpr(N); |
| 677 | offset = 0; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 678 | return 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | void ISel::SelectBranchCC(SDOperand N) |
| 682 | { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 683 | MachineBasicBlock *Dest = |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 684 | cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock(); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 685 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 686 | Select(N.getOperand(0)); //chain |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 687 | ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get(); |
| 688 | unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 689 | unsigned Opc = getBCCForSetCC(CC); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 690 | |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 691 | // If this is a two way branch, then grab the fallthrough basic block argument |
| 692 | // and build a PowerPC branch pseudo-op, suitable for long branch conversion |
| 693 | // if necessary by the branch selection pass. Otherwise, emit a standard |
| 694 | // conditional branch. |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 695 | if (N.getOpcode() == ISD::BRTWOWAY_CC) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 696 | MachineBasicBlock *Fallthrough = |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 697 | cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock(); |
Chris Lattner | f913d3f | 2005-08-21 19:03:28 +0000 | [diff] [blame] | 698 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
| 699 | .addMBB(Dest).addMBB(Fallthrough); |
| 700 | BuildMI(BB, PPC::B, 1).addMBB(Fallthrough); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 701 | } else { |
Chris Lattner | f913d3f | 2005-08-21 19:03:28 +0000 | [diff] [blame] | 702 | // Iterate to the next basic block |
| 703 | ilist<MachineBasicBlock>::iterator It = BB; |
| 704 | ++It; |
| 705 | |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 706 | // If the fallthrough path is off the end of the function, which would be |
| 707 | // undefined behavior, set it to be the same as the current block because |
| 708 | // we have nothing better to set it to, and leaving it alone will cause the |
| 709 | // PowerPC Branch Selection pass to crash. |
| 710 | if (It == BB->getParent()->end()) It = Dest; |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 711 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | 27499e3 | 2005-04-10 01:48:29 +0000 | [diff] [blame] | 712 | .addMBB(Dest).addMBB(It); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 713 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 714 | return; |
| 715 | } |
| 716 | |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 717 | // SelectIntImmediateExpr - Choose code for opcodes with immediate value. |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 718 | bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result, |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 719 | unsigned OCHi, unsigned OCLo, |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 720 | bool IsArithmetic, bool Negate) { |
| 721 | // check constant |
| 722 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 723 | // exit if not a constant |
| 724 | if (!CN) return false; |
| 725 | // extract immediate |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 726 | unsigned C = (unsigned)CN->getValue(); |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 727 | // negate if required (ISD::SUB) |
| 728 | if (Negate) C = -C; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 729 | // get the hi and lo portions of constant |
| 730 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 731 | unsigned Lo = Lo16(C); |
| 732 | // assume no intermediate result from lo instruction (same as final result) |
| 733 | unsigned Tmp = Result; |
| 734 | // check if two instructions are needed |
| 735 | if (Hi && Lo) { |
| 736 | // exit if usage indicates it would be better to load immediate into a |
| 737 | // register |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 738 | if (CN->use_size() > 2) return false; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 739 | // need intermediate result for two instructions |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 740 | Tmp = MakeIntReg(); |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 741 | } |
| 742 | // get first operand |
| 743 | unsigned Opr0 = SelectExpr(N.getOperand(0)); |
| 744 | // is a lo instruction needed |
| 745 | if (Lo) { |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 746 | // generate instruction for lo portion |
| 747 | BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo); |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 748 | // need to switch out first operand for hi instruction |
| 749 | Opr0 = Tmp; |
| 750 | } |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 751 | // is a hi instruction needed |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 752 | if (Hi) { |
| 753 | // generate instruction for hi portion |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 754 | BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi); |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 755 | } |
| 756 | return true; |
| 757 | } |
| 758 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 759 | unsigned ISel::SelectExpr(SDOperand N, bool Recording) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 760 | unsigned Result; |
| 761 | unsigned Tmp1, Tmp2, Tmp3; |
| 762 | unsigned Opc = 0; |
| 763 | unsigned opcode = N.getOpcode(); |
| 764 | |
| 765 | SDNode *Node = N.Val; |
| 766 | MVT::ValueType DestType = N.getValueType(); |
| 767 | |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 768 | if (Node->getOpcode() == ISD::CopyFromReg) { |
| 769 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 770 | // Just use the specified register as our input. |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 771 | if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1) |
| 772 | return Reg; |
| 773 | } |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 774 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 775 | unsigned &Reg = ExprMap[N]; |
| 776 | if (Reg) return Reg; |
| 777 | |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 778 | switch (N.getOpcode()) { |
| 779 | default: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 780 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 781 | MakeReg(N.getValueType()) : 1; |
| 782 | break; |
Chris Lattner | 5dd7fea | 2005-08-31 17:48:04 +0000 | [diff] [blame] | 783 | case ISD::AssertSext: |
| 784 | case ISD::AssertZext: |
| 785 | // Don't allocate a vreg for these nodes. |
| 786 | return Reg = SelectExpr(N.getOperand(0)); |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 787 | case ISD::TAILCALL: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 788 | case ISD::CALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 789 | // If this is a call instruction, make sure to prepare ALL of the result |
| 790 | // values as well as the chain. |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 791 | if (Node->getNumValues() == 1) |
| 792 | Reg = Result = 1; // Void call, just a chain. |
| 793 | else { |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 794 | Result = MakeReg(Node->getValueType(0)); |
| 795 | ExprMap[N.getValue(0)] = Result; |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 796 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 797 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 798 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 799 | } |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 800 | break; |
| 801 | case ISD::ADD_PARTS: |
| 802 | case ISD::SUB_PARTS: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 803 | Result = MakeReg(Node->getValueType(0)); |
| 804 | ExprMap[N.getValue(0)] = Result; |
| 805 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 806 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 807 | break; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 810 | switch (opcode) { |
| 811 | default: |
Nate Begeman | 5a01481 | 2005-08-14 01:17:16 +0000 | [diff] [blame] | 812 | Node->dump(); std::cerr << '\n'; |
| 813 | assert(0 && "Node not handled!\n"); |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 814 | case PPCISD::FSEL: |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 815 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 816 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 817 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 818 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 819 | return Result; |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 820 | case PPCISD::FCFID: |
| 821 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 822 | BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp1); |
| 823 | return Result; |
| 824 | case PPCISD::FCTIDZ: |
| 825 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 826 | BuildMI(BB, PPC::FCTIDZ, 1, Result).addReg(Tmp1); |
| 827 | return Result; |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 828 | case PPCISD::FCTIWZ: |
| 829 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 830 | BuildMI(BB, PPC::FCTIWZ, 1, Result).addReg(Tmp1); |
| 831 | return Result; |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 832 | case ISD::UNDEF: |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 833 | if (Node->getValueType(0) == MVT::i32) |
| 834 | BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Result); |
| 835 | else |
| 836 | BuildMI(BB, PPC::IMPLICIT_DEF_FP, 0, Result); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 837 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 838 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 839 | // Generate both result values. FIXME: Need a better commment here? |
| 840 | if (Result != 1) |
| 841 | ExprMap[N.getValue(1)] = 1; |
| 842 | else |
| 843 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 844 | |
| 845 | // FIXME: We are currently ignoring the requested alignment for handling |
| 846 | // greater than the stack alignment. This will need to be revisited at some |
| 847 | // point. Align = N.getOperand(2); |
| 848 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 849 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 850 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 851 | << " the stack alignment yet!"; |
| 852 | abort(); |
| 853 | } |
| 854 | Select(N.getOperand(0)); |
| 855 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 856 | // Subtract size from stack pointer, thereby allocating some space. |
| 857 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 858 | // Put a pointer to the space into the result register by copying the SP |
| 859 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 860 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 861 | |
| 862 | case ISD::ConstantPool: |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 863 | Tmp1 = BB->getParent()->getConstantPool()-> |
| 864 | getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get()); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 865 | Tmp2 = MakeIntReg(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 866 | if (PICEnabled) |
| 867 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 868 | .addConstantPoolIndex(Tmp1); |
| 869 | else |
| 870 | BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 871 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 872 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 873 | |
| 874 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 875 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 876 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 877 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 878 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 879 | case ISD::GlobalAddress: { |
| 880 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 881 | Tmp1 = MakeIntReg(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 882 | if (PICEnabled) |
| 883 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 884 | .addGlobalAddress(GV); |
| 885 | else |
Chris Lattner | 4015ea8 | 2005-07-28 04:42:11 +0000 | [diff] [blame] | 886 | BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 887 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 888 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 889 | } else { |
| 890 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 891 | } |
| 892 | return Result; |
| 893 | } |
| 894 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 895 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 896 | case ISD::EXTLOAD: |
| 897 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 898 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 899 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 900 | Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 901 | bool sext = (ISD::SEXTLOAD == opcode); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 902 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 903 | // Make sure we generate both values. |
| 904 | if (Result != 1) |
| 905 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 906 | else |
| 907 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 908 | |
| 909 | SDOperand Chain = N.getOperand(0); |
| 910 | SDOperand Address = N.getOperand(1); |
| 911 | Select(Chain); |
| 912 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 913 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 914 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 915 | case MVT::i1: Opc = PPC::LBZ; break; |
| 916 | case MVT::i8: Opc = PPC::LBZ; break; |
| 917 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 918 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 919 | case MVT::f32: Opc = PPC::LFS; break; |
| 920 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 921 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 922 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 923 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 924 | Tmp1 = MakeIntReg(); |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 925 | unsigned CPI = BB->getParent()->getConstantPool()-> |
| 926 | getConstantPoolIndex(CP->get()); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 927 | if (PICEnabled) |
| 928 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 929 | .addConstantPoolIndex(CPI); |
| 930 | else |
| 931 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 932 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 933 | } else if (Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 934 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 935 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 936 | } else { |
| 937 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 938 | switch(SelectAddr(Address, Tmp1, offset)) { |
| 939 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 940 | case 0: // imm offset, no frame, no index |
| 941 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 942 | break; |
| 943 | case 1: // imm offset + frame index |
| 944 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset); |
| 945 | break; |
| 946 | case 2: // base+index addressing |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 947 | Opc = IndexedOpForOp(Opc); |
| 948 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 949 | break; |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 950 | case 3: { |
| 951 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address); |
| 952 | GlobalValue *GV = GN->getGlobal(); |
| 953 | BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 954 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 955 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 956 | } |
| 957 | return Result; |
| 958 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 959 | |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 960 | case ISD::TAILCALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 961 | case ISD::CALL: { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 962 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 963 | static const unsigned GPR[] = { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 964 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 965 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 966 | }; |
| 967 | static const unsigned FPR[] = { |
| 968 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 969 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 970 | }; |
| 971 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 972 | // Lower the chain for this call. |
| 973 | Select(N.getOperand(0)); |
| 974 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 975 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 976 | MachineInstr *CallMI; |
| 977 | // Emit the correct call instruction based on the type of symbol called. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 978 | if (GlobalAddressSDNode *GASD = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 979 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 980 | CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 981 | true); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 982 | } else if (ExternalSymbolSDNode *ESSDN = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 983 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 984 | CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 985 | true); |
| 986 | } else { |
| 987 | Tmp1 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 86fac6b | 2005-08-24 22:21:47 +0000 | [diff] [blame] | 988 | BuildMI(BB, PPC::MTCTR, 1).addReg(Tmp1); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 989 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 990 | CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0) |
| 991 | .addReg(PPC::R12); |
| 992 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 993 | |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 994 | // Load the register args to virtual regs |
| 995 | std::vector<unsigned> ArgVR; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 996 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 997 | ArgVR.push_back(SelectExpr(N.getOperand(i))); |
| 998 | |
| 999 | // Copy the virtual registers into the appropriate argument register |
| 1000 | for(int i = 0, e = ArgVR.size(); i < e; ++i) { |
| 1001 | switch(N.getOperand(i+2).getValueType()) { |
| 1002 | default: Node->dump(); assert(0 && "Unknown value type for call"); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1003 | case MVT::i32: |
| 1004 | assert(GPR_idx < 8 && "Too many int args"); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1005 | if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1006 | BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1007 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1008 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1009 | ++GPR_idx; |
| 1010 | break; |
| 1011 | case MVT::f64: |
| 1012 | case MVT::f32: |
| 1013 | assert(FPR_idx < 13 && "Too many fp args"); |
| 1014 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1015 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1016 | ++FPR_idx; |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1020 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1021 | // Put the call instruction in the correct place in the MachineBasicBlock |
| 1022 | BB->push_back(CallMI); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1023 | |
| 1024 | switch (Node->getValueType(0)) { |
| 1025 | default: assert(0 && "Unknown value type for call result!"); |
| 1026 | case MVT::Other: return 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1027 | case MVT::i32: |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 1028 | if (Node->getValueType(1) == MVT::i32) { |
| 1029 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3); |
| 1030 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4); |
| 1031 | } else { |
| 1032 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
| 1033 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | case MVT::f32: |
| 1036 | case MVT::f64: |
| 1037 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 1038 | break; |
| 1039 | } |
| 1040 | return Result+N.ResNo; |
| 1041 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1042 | |
| 1043 | case ISD::SIGN_EXTEND: |
| 1044 | case ISD::SIGN_EXTEND_INREG: |
| 1045 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1046 | switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1047 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1048 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1049 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1050 | break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1051 | case MVT::i8: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1052 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1055 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1056 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1057 | case ISD::CopyFromReg: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1058 | DestType = N.getValue(0).getValueType(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1059 | if (Result == 1) |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1060 | Result = ExprMap[N.getValue(0)] = MakeReg(DestType); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 1061 | Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1062 | if (MVT::isInteger(DestType)) |
| 1063 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1064 | else |
| 1065 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1066 | return Result; |
| 1067 | |
| 1068 | case ISD::SHL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1069 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1070 | unsigned SH, MB, ME; |
| 1071 | if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) && |
| 1072 | isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) { |
| 1073 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1074 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH) |
| 1075 | .addImm(MB).addImm(ME); |
| 1076 | return Result; |
| 1077 | } |
| 1078 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1079 | Tmp2 &= 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1080 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1081 | .addImm(31-Tmp2); |
| 1082 | } else { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1083 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1084 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1085 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1086 | } |
| 1087 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1088 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1089 | case ISD::SRL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1090 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1091 | unsigned SH, MB, ME; |
| 1092 | if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) && |
| 1093 | isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) { |
| 1094 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 1095 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH & 0x1F) |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1096 | .addImm(MB).addImm(ME); |
| 1097 | return Result; |
| 1098 | } |
| 1099 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1100 | Tmp2 &= 0x1F; |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 1101 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm((32-Tmp2) & 0x1F) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1102 | .addImm(Tmp2).addImm(31); |
| 1103 | } else { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1104 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1105 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1106 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1107 | } |
| 1108 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1109 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1110 | case ISD::SRA: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1111 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1112 | unsigned SH, MB, ME; |
| 1113 | if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) && |
| 1114 | isRotateAndMask(ISD::SRA, Tmp2, Tmp3, true, SH, MB, ME)) { |
| 1115 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1116 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH) |
| 1117 | .addImm(MB).addImm(ME); |
| 1118 | return Result; |
| 1119 | } |
| 1120 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1121 | Tmp2 &= 0x1F; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1122 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1123 | } else { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1124 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1125 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1126 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1127 | } |
| 1128 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1129 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1130 | case ISD::CTLZ: |
| 1131 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1132 | BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1); |
| 1133 | return Result; |
| 1134 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1135 | case ISD::ADD: |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1136 | if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true)) |
| 1137 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1138 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 39c6896 | 2005-08-08 21:21:03 +0000 | [diff] [blame] | 1139 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1140 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1141 | return Result; |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1142 | |
| 1143 | case ISD::FADD: |
| 1144 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::FMUL && |
| 1145 | N.getOperand(0).Val->hasOneUse()) { |
| 1146 | ++FusedFP; // Statistic |
| 1147 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1148 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1149 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1150 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1151 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1152 | return Result; |
| 1153 | } |
| 1154 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::FMUL && |
| 1155 | N.getOperand(1).Val->hasOneUse()) { |
| 1156 | ++FusedFP; // Statistic |
| 1157 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1158 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1159 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1160 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1161 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1162 | return Result; |
| 1163 | } |
| 1164 | Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; |
| 1165 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1166 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1167 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1168 | return Result; |
| 1169 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1170 | case ISD::AND: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1171 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1172 | if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) { |
| 1173 | unsigned SH, MB, ME; |
| 1174 | Opc = Recording ? PPC::RLWINMo : PPC::RLWINM; |
| 1175 | unsigned OprOpc; |
| 1176 | if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) && |
| 1177 | isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) { |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1178 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1179 | } else { |
| 1180 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1181 | isRunOfOnes(Tmp2, MB, ME); |
| 1182 | SH = 0; |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1183 | } |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1184 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH) |
| 1185 | .addImm(MB).addImm(ME); |
| 1186 | RecordSuccess = true; |
| 1187 | return Result; |
| 1188 | } else if (isUInt16(Tmp2)) { |
| 1189 | Tmp2 = Lo16(Tmp2); |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1190 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1191 | BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1192 | RecordSuccess = true; |
| 1193 | return Result; |
| 1194 | } else if (isUInt16(Tmp2)) { |
| 1195 | Tmp2 = Hi16(Tmp2); |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1196 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1197 | BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1198 | RecordSuccess = true; |
| 1199 | return Result; |
| 1200 | } |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1201 | } |
Jim Laskey | 847c3a9 | 2005-08-12 23:38:02 +0000 | [diff] [blame] | 1202 | if (isOprNot(N.getOperand(1))) { |
| 1203 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1204 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1205 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1206 | RecordSuccess = false; |
| 1207 | return Result; |
| 1208 | } |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1209 | if (isOprNot(N.getOperand(0))) { |
Jim Laskey | 847c3a9 | 2005-08-12 23:38:02 +0000 | [diff] [blame] | 1210 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1211 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1212 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1213 | RecordSuccess = false; |
| 1214 | return Result; |
| 1215 | } |
| 1216 | // emit a regular and |
| 1217 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1218 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1219 | Opc = Recording ? PPC::ANDo : PPC::AND; |
| 1220 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1221 | RecordSuccess = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1222 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1223 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1224 | case ISD::OR: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1225 | if (SelectBitfieldInsert(N, Result)) |
| 1226 | return Result; |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1227 | if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI)) |
| 1228 | return Result; |
Jim Laskey | 847c3a9 | 2005-08-12 23:38:02 +0000 | [diff] [blame] | 1229 | if (isOprNot(N.getOperand(1))) { |
| 1230 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1231 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1232 | BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1233 | RecordSuccess = false; |
| 1234 | return Result; |
| 1235 | } |
| 1236 | if (isOprNot(N.getOperand(0))) { |
| 1237 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1238 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1239 | BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1240 | RecordSuccess = false; |
| 1241 | return Result; |
| 1242 | } |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1243 | // emit regular or |
| 1244 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1245 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1246 | Opc = Recording ? PPC::ORo : PPC::OR; |
| 1247 | RecordSuccess = true; |
| 1248 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1249 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1250 | |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1251 | case ISD::XOR: { |
| 1252 | // Check for EQV: xor, (xor a, -1), b |
Chris Lattner | df706e3 | 2005-08-10 16:35:46 +0000 | [diff] [blame] | 1253 | if (isOprNot(N.getOperand(0))) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1254 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1255 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1256 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1257 | return Result; |
| 1258 | } |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1259 | // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1 |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1260 | if (isOprNot(N)) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1261 | switch(N.getOperand(0).getOpcode()) { |
| 1262 | case ISD::OR: |
| 1263 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1264 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1265 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1266 | break; |
| 1267 | case ISD::AND: |
| 1268 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1269 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1270 | BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1271 | break; |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1272 | case ISD::XOR: |
| 1273 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1274 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1275 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1276 | break; |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1277 | default: |
| 1278 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1279 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1280 | break; |
| 1281 | } |
| 1282 | return Result; |
| 1283 | } |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1284 | if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI)) |
| 1285 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1286 | // emit regular xor |
| 1287 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1288 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1289 | BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1290 | return Result; |
| 1291 | } |
| 1292 | |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1293 | case ISD::FSUB: |
| 1294 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::FMUL && |
| 1295 | N.getOperand(0).Val->hasOneUse()) { |
| 1296 | ++FusedFP; // Statistic |
| 1297 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1298 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1299 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1300 | Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS; |
| 1301 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1302 | return Result; |
| 1303 | } |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1304 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::FMUL && |
| 1305 | N.getOperand(1).Val->hasOneUse()) { |
| 1306 | ++FusedFP; // Statistic |
| 1307 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1308 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1309 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1310 | Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS; |
| 1311 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1312 | return Result; |
| 1313 | } |
| 1314 | Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; |
| 1315 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1316 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1317 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1318 | return Result; |
| 1319 | case ISD::SUB: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1320 | if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) { |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1321 | Tmp1 = Lo16(Tmp1); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1322 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1323 | if (0 == Tmp1) |
| 1324 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp2); |
| 1325 | else |
| 1326 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1); |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1327 | return Result; |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1328 | } |
| 1329 | if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true)) |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1330 | return Result; |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1331 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1332 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1333 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1334 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1336 | case ISD::FMUL: |
| 1337 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1338 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1339 | BuildMI(BB, DestType == MVT::f32 ? PPC::FMULS : PPC::FMUL, 2, |
| 1340 | Result).addReg(Tmp1).addReg(Tmp2); |
| 1341 | return Result; |
| 1342 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1343 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1344 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1345 | if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1346 | Tmp2 = Lo16(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1347 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1348 | } else { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1349 | Tmp2 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1350 | BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1351 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1352 | return Result; |
| 1353 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1354 | case ISD::MULHS: |
| 1355 | case ISD::MULHU: |
| 1356 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1357 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1358 | Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW; |
| 1359 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1360 | return Result; |
| 1361 | |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1362 | case ISD::FDIV: |
| 1363 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1364 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1365 | switch (DestType) { |
| 1366 | default: assert(0 && "Unknown type to ISD::FDIV"); break; |
| 1367 | case MVT::f32: Opc = PPC::FDIVS; break; |
| 1368 | case MVT::f64: Opc = PPC::FDIV; break; |
| 1369 | } |
| 1370 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1371 | return Result; |
| 1372 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1373 | case ISD::SDIV: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1374 | if (isIntImmediate(N.getOperand(1), Tmp3)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1375 | if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) { |
| 1376 | Tmp3 = Log2_32(Tmp3); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1377 | Tmp1 = MakeIntReg(); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1378 | Tmp2 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1379 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1380 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1381 | return Result; |
| 1382 | } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) { |
| 1383 | Tmp3 = Log2_32(-Tmp3); |
Chris Lattner | 2f46055 | 2005-08-09 18:08:41 +0000 | [diff] [blame] | 1384 | Tmp2 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1385 | Tmp1 = MakeIntReg(); |
| 1386 | unsigned Tmp4 = MakeIntReg(); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1387 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1388 | BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1); |
| 1389 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4); |
| 1390 | return Result; |
Chris Lattner | c70b4af | 2005-08-25 22:03:50 +0000 | [diff] [blame] | 1391 | } else if (Tmp3) { |
| 1392 | ExprMap.erase(N); |
| 1393 | return SelectExpr(BuildSDIVSequence(N)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1394 | } |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1395 | } |
| 1396 | // fall thru |
| 1397 | case ISD::UDIV: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1398 | // If this is a divide by constant, we can emit code using some magic |
| 1399 | // constants to implement it as a multiply instead. |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1400 | if (isIntImmediate(N.getOperand(1), Tmp3) && Tmp3) { |
Chris Lattner | c70b4af | 2005-08-25 22:03:50 +0000 | [diff] [blame] | 1401 | ExprMap.erase(N); |
| 1402 | return SelectExpr(BuildUDIVSequence(N)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1403 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1404 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1405 | Tmp2 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1406 | Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break; |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1407 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1408 | return Result; |
| 1409 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1410 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1411 | case ISD::SUB_PARTS: { |
| 1412 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1413 | "Not an i64 add/sub!"); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1414 | unsigned Tmp4 = 0; |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1415 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1416 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1417 | |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1418 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Chris Lattner | 95e0682 | 2005-08-26 16:38:51 +0000 | [diff] [blame] | 1419 | bool ME = false, ZE = false; |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1420 | if (isIntImmediate(N.getOperand(3), Tmp3)) { |
| 1421 | ME = (signed)Tmp3 == -1; |
| 1422 | ZE = Tmp3 == 0; |
| 1423 | } |
| 1424 | |
| 1425 | if (!ZE && !ME) |
| 1426 | Tmp4 = SelectExpr(N.getOperand(3)); |
| 1427 | |
| 1428 | if (isIntImmediate(N.getOperand(2), Tmp3) && |
| 1429 | ((signed)Tmp3 >= -32768 || (signed)Tmp3 < 32768)) { |
| 1430 | // Codegen the low 32 bits of the add. Interestingly, there is no |
| 1431 | // shifted form of add immediate carrying. |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1432 | BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1433 | } else { |
| 1434 | Tmp3 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1435 | BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1438 | // Codegen the high 32 bits, adding zero, minus one, or the full value |
| 1439 | // along with the carry flag produced by addc/addic to tmp2. |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1440 | if (ZE) { |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1441 | BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1442 | } else if (ME) { |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1443 | BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1444 | } else { |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1445 | BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1446 | } |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1447 | } else { |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1448 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 1449 | Tmp4 = SelectExpr(N.getOperand(3)); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1450 | BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1); |
| 1451 | BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1452 | } |
| 1453 | return Result+N.ResNo; |
| 1454 | } |
| 1455 | |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1456 | case ISD::SETCC: { |
| 1457 | ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get(); |
| 1458 | if (isIntImmediate(Node->getOperand(1), Tmp3)) { |
| 1459 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 1460 | // Check for those cases here. |
| 1461 | // setcc op, 0 |
| 1462 | if (Tmp3 == 0) { |
| 1463 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 1464 | switch (CC) { |
Chris Lattner | ee84f11 | 2005-08-25 17:49:31 +0000 | [diff] [blame] | 1465 | default: Node->dump(); assert(0 && "Unhandled SetCC condition");abort(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1466 | case ISD::SETEQ: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1467 | Tmp2 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1468 | BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1); |
| 1469 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27) |
| 1470 | .addImm(5).addImm(31); |
| 1471 | break; |
| 1472 | case ISD::SETNE: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1473 | Tmp2 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1474 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 1475 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1476 | break; |
| 1477 | case ISD::SETLT: |
| 1478 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1) |
| 1479 | .addImm(31).addImm(31); |
| 1480 | break; |
| 1481 | case ISD::SETGT: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1482 | Tmp2 = MakeIntReg(); |
| 1483 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1484 | BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1); |
| 1485 | BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 1486 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 1487 | .addImm(31).addImm(31); |
| 1488 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1489 | } |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1490 | return Result; |
| 1491 | } else if (Tmp3 == ~0U) { // setcc op, -1 |
| 1492 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 1493 | switch (CC) { |
| 1494 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 1495 | case ISD::SETEQ: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1496 | Tmp2 = MakeIntReg(); |
| 1497 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1498 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 1499 | BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0); |
| 1500 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3); |
| 1501 | break; |
| 1502 | case ISD::SETNE: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1503 | Tmp2 = MakeIntReg(); |
| 1504 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1505 | BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1506 | BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1); |
| 1507 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2); |
| 1508 | break; |
| 1509 | case ISD::SETLT: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1510 | Tmp2 = MakeIntReg(); |
| 1511 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1512 | BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 1513 | BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 1514 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 1515 | .addImm(31).addImm(31); |
| 1516 | break; |
| 1517 | case ISD::SETGT: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1518 | Tmp2 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1519 | BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1) |
| 1520 | .addImm(31).addImm(31); |
| 1521 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1); |
| 1522 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 1523 | } |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1524 | return Result; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 1525 | } |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1526 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1527 | |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 1528 | unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC); |
| 1529 | MoveCRtoGPR(CCReg, CC, Result); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1530 | return Result; |
| 1531 | } |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 1532 | |
| 1533 | case ISD::SELECT_CC: { |
| 1534 | ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1535 | |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1536 | // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
| 1537 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 1538 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N.getOperand(2)); |
| 1539 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N.getOperand(3)); |
| 1540 | if (N1C && N2C && N3C && N1C->isNullValue() && N3C->isNullValue() && |
Nate Begeman | 6ef4949 | 2005-08-24 05:06:48 +0000 | [diff] [blame] | 1541 | N2C->getValue() == 1ULL && CC == ISD::SETNE) { |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1542 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 1543 | Tmp2 = MakeIntReg(); |
Nate Begeman | 6ef4949 | 2005-08-24 05:06:48 +0000 | [diff] [blame] | 1544 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 1545 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1546 | return Result; |
| 1547 | } |
| 1548 | |
Nate Begeman | 5a01481 | 2005-08-14 01:17:16 +0000 | [diff] [blame] | 1549 | // If the False value only has one use, we can generate better code by |
| 1550 | // selecting it in the fallthrough basic block rather than here, which |
| 1551 | // increases register pressure. |
Nate Begeman | 5a01481 | 2005-08-14 01:17:16 +0000 | [diff] [blame] | 1552 | unsigned TrueValue = SelectExpr(N.getOperand(2)); |
Chris Lattner | 4dd4a2d | 2005-08-21 17:41:11 +0000 | [diff] [blame] | 1553 | unsigned FalseValue; |
| 1554 | |
| 1555 | // If the false value is simple enough, evaluate it inline in the false |
| 1556 | // block. |
Chris Lattner | b30ee6a | 2005-08-22 00:47:28 +0000 | [diff] [blame] | 1557 | if (N.getOperand(3).Val->hasOneUse() && |
| 1558 | (isa<ConstantSDNode>(N.getOperand(3)) || |
Chris Lattner | b30ee6a | 2005-08-22 00:47:28 +0000 | [diff] [blame] | 1559 | isa<GlobalAddressSDNode>(N.getOperand(3)))) |
Chris Lattner | 4dd4a2d | 2005-08-21 17:41:11 +0000 | [diff] [blame] | 1560 | FalseValue = 0; |
| 1561 | else |
| 1562 | FalseValue = SelectExpr(N.getOperand(3)); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 1563 | unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC); |
| 1564 | Opc = getBCCForSetCC(CC); |
| 1565 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1566 | // Create an iterator with which to insert the MBB for copying the false |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1567 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1568 | MachineBasicBlock *thisMBB = BB; |
| 1569 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1570 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1571 | ++It; |
| 1572 | |
| 1573 | // thisMBB: |
| 1574 | // ... |
| 1575 | // TrueVal = ... |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1576 | // cmpTY ccX, r1, r2 |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1577 | // bCC copy1MBB |
| 1578 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1579 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1580 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1581 | BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1582 | MachineFunction *F = BB->getParent(); |
| 1583 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1584 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1585 | // Update machine-CFG edges |
| 1586 | BB->addSuccessor(copy0MBB); |
| 1587 | BB->addSuccessor(sinkMBB); |
| 1588 | |
| 1589 | // copy0MBB: |
| 1590 | // %FalseValue = ... |
| 1591 | // # fallthrough to sinkMBB |
| 1592 | BB = copy0MBB; |
Chris Lattner | 4dd4a2d | 2005-08-21 17:41:11 +0000 | [diff] [blame] | 1593 | |
| 1594 | // If the false value is simple enough, evaluate it here, to avoid it being |
| 1595 | // evaluated on the true edge. |
| 1596 | if (FalseValue == 0) |
| 1597 | FalseValue = SelectExpr(N.getOperand(3)); |
| 1598 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1599 | // Update machine-CFG edges |
| 1600 | BB->addSuccessor(sinkMBB); |
| 1601 | |
| 1602 | // sinkMBB: |
| 1603 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1604 | // ... |
| 1605 | BB = sinkMBB; |
| 1606 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1607 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1608 | return Result; |
| 1609 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1610 | |
Chris Lattner | 0c09a41 | 2005-08-18 17:16:52 +0000 | [diff] [blame] | 1611 | case ISD::Constant: { |
| 1612 | assert(N.getValueType() == MVT::i32 && |
| 1613 | "Only i32 constants are legal on this target!"); |
Nate Begeman | 58dfb08 | 2005-08-18 18:14:49 +0000 | [diff] [blame] | 1614 | unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); |
Jim Laskey | 5b5f0b7 | 2005-08-18 18:58:23 +0000 | [diff] [blame] | 1615 | if (isInt16(v)) { |
| 1616 | BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo16(v)); |
Chris Lattner | 0c09a41 | 2005-08-18 17:16:52 +0000 | [diff] [blame] | 1617 | } else { |
Jim Laskey | 5b5f0b7 | 2005-08-18 18:58:23 +0000 | [diff] [blame] | 1618 | unsigned Hi = Hi16(v); |
| 1619 | unsigned Lo = Lo16(v); |
| 1620 | if (Lo) { |
| 1621 | Tmp1 = MakeIntReg(); |
| 1622 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi); |
| 1623 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo); |
| 1624 | } else { |
| 1625 | BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi); |
| 1626 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1627 | } |
| 1628 | return Result; |
Chris Lattner | 0c09a41 | 2005-08-18 17:16:52 +0000 | [diff] [blame] | 1629 | } |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1630 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1631 | case ISD::FNEG: |
| 1632 | if (!NoExcessFPPrecision && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1633 | ISD::FADD == N.getOperand(0).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1634 | N.getOperand(0).Val->hasOneUse() && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1635 | ISD::FMUL == N.getOperand(0).getOperand(0).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1636 | N.getOperand(0).getOperand(0).Val->hasOneUse()) { |
| 1637 | ++FusedFP; // Statistic |
| 1638 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0)); |
| 1639 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1)); |
| 1640 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1641 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 1642 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1643 | } else if (!NoExcessFPPrecision && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1644 | ISD::FADD == N.getOperand(0).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1645 | N.getOperand(0).Val->hasOneUse() && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame^] | 1646 | ISD::FMUL == N.getOperand(0).getOperand(1).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1647 | N.getOperand(0).getOperand(1).Val->hasOneUse()) { |
| 1648 | ++FusedFP; // Statistic |
| 1649 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0)); |
| 1650 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1)); |
| 1651 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1652 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 1653 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1654 | } else if (ISD::FABS == N.getOperand(0).getOpcode()) { |
| 1655 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1656 | BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1); |
| 1657 | } else { |
| 1658 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1659 | BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1); |
| 1660 | } |
| 1661 | return Result; |
| 1662 | |
| 1663 | case ISD::FABS: |
| 1664 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1665 | BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1); |
| 1666 | return Result; |
| 1667 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 1668 | case ISD::FSQRT: |
| 1669 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1670 | Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS; |
| 1671 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 1672 | return Result; |
| 1673 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1674 | case ISD::FP_ROUND: |
| 1675 | assert (DestType == MVT::f32 && |
| 1676 | N.getOperand(0).getValueType() == MVT::f64 && |
| 1677 | "only f64 to f32 conversion supported here"); |
| 1678 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1679 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 1680 | return Result; |
| 1681 | |
| 1682 | case ISD::FP_EXTEND: |
| 1683 | assert (DestType == MVT::f64 && |
| 1684 | N.getOperand(0).getValueType() == MVT::f32 && |
| 1685 | "only f32 to f64 conversion supported here"); |
| 1686 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1687 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 1688 | return Result; |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1689 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | void ISel::Select(SDOperand N) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1694 | unsigned Tmp1, Tmp2, Tmp3, Opc; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1695 | unsigned opcode = N.getOpcode(); |
| 1696 | |
| 1697 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 1698 | return; // Already selected. |
| 1699 | |
| 1700 | SDNode *Node = N.Val; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1701 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1702 | switch (Node->getOpcode()) { |
| 1703 | default: |
| 1704 | Node->dump(); std::cerr << "\n"; |
| 1705 | assert(0 && "Node not handled yet!"); |
| 1706 | case ISD::EntryToken: return; // Noop |
| 1707 | case ISD::TokenFactor: |
| 1708 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1709 | Select(Node->getOperand(i)); |
| 1710 | return; |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1711 | case ISD::CALLSEQ_START: |
| 1712 | case ISD::CALLSEQ_END: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1713 | Select(N.getOperand(0)); |
| 1714 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1715 | Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN : |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1716 | PPC::ADJCALLSTACKUP; |
| 1717 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 1718 | return; |
| 1719 | case ISD::BR: { |
| 1720 | MachineBasicBlock *Dest = |
| 1721 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1722 | Select(N.getOperand(0)); |
| 1723 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 1724 | return; |
| 1725 | } |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1726 | case ISD::BR_CC: |
| 1727 | case ISD::BRTWOWAY_CC: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1728 | SelectBranchCC(N); |
| 1729 | return; |
| 1730 | case ISD::CopyToReg: |
| 1731 | Select(N.getOperand(0)); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 1732 | Tmp1 = SelectExpr(N.getOperand(2)); |
| 1733 | Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1734 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1735 | if (Tmp1 != Tmp2) { |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 1736 | if (N.getOperand(2).getValueType() == MVT::f64 || |
| 1737 | N.getOperand(2).getValueType() == MVT::f32) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1738 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 1739 | else |
| 1740 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1741 | } |
| 1742 | return; |
| 1743 | case ISD::ImplicitDef: |
| 1744 | Select(N.getOperand(0)); |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 1745 | Tmp1 = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
| 1746 | if (N.getOperand(1).getValueType() == MVT::i32) |
| 1747 | BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Tmp1); |
| 1748 | else |
| 1749 | BuildMI(BB, PPC::IMPLICIT_DEF_FP, 0, Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1750 | return; |
| 1751 | case ISD::RET: |
| 1752 | switch (N.getNumOperands()) { |
| 1753 | default: |
| 1754 | assert(0 && "Unknown return instruction!"); |
| 1755 | case 3: |
| 1756 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 1757 | N.getOperand(2).getValueType() == MVT::i32 && |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 1758 | "Unknown two-register value!"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1759 | Select(N.getOperand(0)); |
| 1760 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1761 | Tmp2 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1762 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2); |
| 1763 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1764 | break; |
| 1765 | case 2: |
| 1766 | Select(N.getOperand(0)); |
| 1767 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1768 | switch (N.getOperand(1).getValueType()) { |
| 1769 | default: |
| 1770 | assert(0 && "Unknown return type!"); |
| 1771 | case MVT::f64: |
| 1772 | case MVT::f32: |
| 1773 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 1774 | break; |
| 1775 | case MVT::i32: |
| 1776 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1777 | break; |
| 1778 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1779 | case 1: |
| 1780 | Select(N.getOperand(0)); |
| 1781 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1782 | } |
| 1783 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 1784 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1785 | case ISD::TRUNCSTORE: |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1786 | case ISD::STORE: { |
| 1787 | SDOperand Chain = N.getOperand(0); |
| 1788 | SDOperand Value = N.getOperand(1); |
| 1789 | SDOperand Address = N.getOperand(2); |
| 1790 | Select(Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1791 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1792 | Tmp1 = SelectExpr(Value); //value |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1793 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1794 | if (opcode == ISD::STORE) { |
| 1795 | switch(Value.getValueType()) { |
| 1796 | default: assert(0 && "unknown Type in store"); |
| 1797 | case MVT::i32: Opc = PPC::STW; break; |
| 1798 | case MVT::f64: Opc = PPC::STFD; break; |
| 1799 | case MVT::f32: Opc = PPC::STFS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1800 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1801 | } else { //ISD::TRUNCSTORE |
| 1802 | switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) { |
| 1803 | default: assert(0 && "unknown Type in store"); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1804 | case MVT::i8: Opc = PPC::STB; break; |
| 1805 | case MVT::i16: Opc = PPC::STH; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1806 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1807 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1808 | |
| 1809 | if(Address.getOpcode() == ISD::FrameIndex) { |
| 1810 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1811 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1812 | } else { |
| 1813 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1814 | switch(SelectAddr(Address, Tmp2, offset)) { |
| 1815 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 1816 | case 0: // imm offset, no frame, no index |
| 1817 | BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2); |
| 1818 | break; |
| 1819 | case 1: // imm offset + frame index |
| 1820 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset); |
| 1821 | break; |
| 1822 | case 2: // base+index addressing |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1823 | Opc = IndexedOpForOp(Opc); |
| 1824 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1825 | break; |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 1826 | case 3: { |
| 1827 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address); |
| 1828 | GlobalValue *GV = GN->getGlobal(); |
| 1829 | BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2); |
| 1830 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | return; |
| 1834 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1835 | case ISD::EXTLOAD: |
| 1836 | case ISD::SEXTLOAD: |
| 1837 | case ISD::ZEXTLOAD: |
| 1838 | case ISD::LOAD: |
| 1839 | case ISD::CopyFromReg: |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1840 | case ISD::TAILCALL: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1841 | case ISD::CALL: |
| 1842 | case ISD::DYNAMIC_STACKALLOC: |
| 1843 | ExprMap.erase(N); |
| 1844 | SelectExpr(N); |
| 1845 | return; |
| 1846 | } |
| 1847 | assert(0 && "Should not be reached!"); |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 1852 | /// into a machine code representation using pattern matching and a machine |
| 1853 | /// description file. |
| 1854 | /// |
| 1855 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1856 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |