R600: Expand MUL for v4i32/v2i32
[oota-llvm.git] / lib / Target / R600 / R600ISelLowering.cpp
1 //===-- R600ISelLowering.cpp - R600 DAG Lowering Implementation -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief Custom DAG lowering for R600
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "R600ISelLowering.h"
16 #include "R600Defines.h"
17 #include "R600InstrInfo.h"
18 #include "R600MachineFunctionInfo.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/IR/Argument.h"
24 #include "llvm/IR/Function.h"
25
26 using namespace llvm;
27
28 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
29     AMDGPUTargetLowering(TM),
30     TII(static_cast<const R600InstrInfo*>(TM.getInstrInfo())) {
31   addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
32   addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
33   addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
34   addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
35   computeRegisterProperties();
36
37   setOperationAction(ISD::FADD, MVT::v4f32, Expand);
38   setOperationAction(ISD::FMUL, MVT::v4f32, Expand);
39   setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
40   setOperationAction(ISD::FSUB, MVT::v4f32, Expand);
41
42   setOperationAction(ISD::ADD,  MVT::v4i32, Expand);
43   setOperationAction(ISD::AND,  MVT::v4i32, Expand);
44   setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Expand);
45   setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Expand);
46   setOperationAction(ISD::MUL,  MVT::v2i32, Expand);
47   setOperationAction(ISD::MUL,  MVT::v4i32, Expand);
48   setOperationAction(ISD::OR, MVT::v4i32, Expand);
49   setOperationAction(ISD::OR, MVT::v2i32, Expand);
50   setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Expand);
51   setOperationAction(ISD::SHL, MVT::v4i32, Expand);
52   setOperationAction(ISD::SHL, MVT::v2i32, Expand);
53   setOperationAction(ISD::SRL, MVT::v4i32, Expand);
54   setOperationAction(ISD::SRL, MVT::v2i32, Expand);
55   setOperationAction(ISD::SRA, MVT::v4i32, Expand);
56   setOperationAction(ISD::SRA, MVT::v2i32, Expand);
57   setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Expand);
58   setOperationAction(ISD::UDIV, MVT::v4i32, Expand);
59   setOperationAction(ISD::UREM, MVT::v4i32, Expand);
60   setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
61   setOperationAction(ISD::XOR, MVT::v4i32, Expand);
62   setOperationAction(ISD::XOR, MVT::v2i32, Expand);
63
64   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
65   setOperationAction(ISD::BR_CC, MVT::f32, Expand);
66
67   setOperationAction(ISD::FSUB, MVT::f32, Expand);
68
69   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
70   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
71   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
72
73   setOperationAction(ISD::ROTL, MVT::i32, Custom);
74
75   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
76   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
77
78   setOperationAction(ISD::SETCC, MVT::i32, Expand);
79   setOperationAction(ISD::SETCC, MVT::f32, Expand);
80   setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
81
82   setOperationAction(ISD::SELECT, MVT::i32, Custom);
83   setOperationAction(ISD::SELECT, MVT::f32, Custom);
84
85   setOperationAction(ISD::VSELECT, MVT::v4i32, Expand);
86   setOperationAction(ISD::VSELECT, MVT::v2i32, Expand);
87
88   // Legalize loads and stores to the private address space.
89   setOperationAction(ISD::LOAD, MVT::i32, Custom);
90   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
91   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
92   setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Custom);
93   setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
94   setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
95   setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Custom);
96   setOperationAction(ISD::STORE, MVT::i8, Custom);
97   setOperationAction(ISD::STORE, MVT::i32, Custom);
98   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
99   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
100
101   setOperationAction(ISD::LOAD, MVT::i32, Custom);
102   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
103   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
104
105   setTargetDAGCombine(ISD::FP_ROUND);
106   setTargetDAGCombine(ISD::FP_TO_SINT);
107   setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
108   setTargetDAGCombine(ISD::SELECT_CC);
109
110   setBooleanContents(ZeroOrNegativeOneBooleanContent);
111   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
112   setSchedulingPreference(Sched::VLIW);
113 }
114
115 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
116     MachineInstr * MI, MachineBasicBlock * BB) const {
117   MachineFunction * MF = BB->getParent();
118   MachineRegisterInfo &MRI = MF->getRegInfo();
119   MachineBasicBlock::iterator I = *MI;
120
121   switch (MI->getOpcode()) {
122   default: return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
123   case AMDGPU::CLAMP_R600: {
124     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
125                                                    AMDGPU::MOV,
126                                                    MI->getOperand(0).getReg(),
127                                                    MI->getOperand(1).getReg());
128     TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
129     break;
130   }
131
132   case AMDGPU::FABS_R600: {
133     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
134                                                     AMDGPU::MOV,
135                                                     MI->getOperand(0).getReg(),
136                                                     MI->getOperand(1).getReg());
137     TII->addFlag(NewMI, 0, MO_FLAG_ABS);
138     break;
139   }
140
141   case AMDGPU::FNEG_R600: {
142     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
143                                                     AMDGPU::MOV,
144                                                     MI->getOperand(0).getReg(),
145                                                     MI->getOperand(1).getReg());
146     TII->addFlag(NewMI, 0, MO_FLAG_NEG);
147     break;
148   }
149
150   case AMDGPU::MASK_WRITE: {
151     unsigned maskedRegister = MI->getOperand(0).getReg();
152     assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
153     MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
154     TII->addFlag(defInstr, 0, MO_FLAG_MASK);
155     break;
156   }
157
158   case AMDGPU::MOV_IMM_F32:
159     TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
160                      MI->getOperand(1).getFPImm()->getValueAPF()
161                          .bitcastToAPInt().getZExtValue());
162     break;
163   case AMDGPU::MOV_IMM_I32:
164     TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
165                      MI->getOperand(1).getImm());
166     break;
167   case AMDGPU::CONST_COPY: {
168     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
169         MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
170     TII->setImmOperand(NewMI, R600Operands::SRC0_SEL,
171         MI->getOperand(1).getImm());
172     break;
173   }
174
175   case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
176   case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
177     unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
178
179     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
180             .addOperand(MI->getOperand(0))
181             .addOperand(MI->getOperand(1))
182             .addImm(EOP); // Set End of program bit
183     break;
184   }
185
186   case AMDGPU::TXD: {
187     unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
188     unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
189
190     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
191             .addOperand(MI->getOperand(3))
192             .addOperand(MI->getOperand(4))
193             .addOperand(MI->getOperand(5))
194             .addOperand(MI->getOperand(6));
195     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
196             .addOperand(MI->getOperand(2))
197             .addOperand(MI->getOperand(4))
198             .addOperand(MI->getOperand(5))
199             .addOperand(MI->getOperand(6));
200     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
201             .addOperand(MI->getOperand(0))
202             .addOperand(MI->getOperand(1))
203             .addOperand(MI->getOperand(4))
204             .addOperand(MI->getOperand(5))
205             .addOperand(MI->getOperand(6))
206             .addReg(T0, RegState::Implicit)
207             .addReg(T1, RegState::Implicit);
208     break;
209   }
210
211   case AMDGPU::TXD_SHADOW: {
212     unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
213     unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
214
215     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
216             .addOperand(MI->getOperand(3))
217             .addOperand(MI->getOperand(4))
218             .addOperand(MI->getOperand(5))
219             .addOperand(MI->getOperand(6));
220     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
221             .addOperand(MI->getOperand(2))
222             .addOperand(MI->getOperand(4))
223             .addOperand(MI->getOperand(5))
224             .addOperand(MI->getOperand(6));
225     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
226             .addOperand(MI->getOperand(0))
227             .addOperand(MI->getOperand(1))
228             .addOperand(MI->getOperand(4))
229             .addOperand(MI->getOperand(5))
230             .addOperand(MI->getOperand(6))
231             .addReg(T0, RegState::Implicit)
232             .addReg(T1, RegState::Implicit);
233     break;
234   }
235
236   case AMDGPU::BRANCH:
237       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
238               .addOperand(MI->getOperand(0));
239       break;
240
241   case AMDGPU::BRANCH_COND_f32: {
242     MachineInstr *NewMI =
243       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
244               AMDGPU::PREDICATE_BIT)
245               .addOperand(MI->getOperand(1))
246               .addImm(OPCODE_IS_NOT_ZERO)
247               .addImm(0); // Flags
248     TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
249     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
250             .addOperand(MI->getOperand(0))
251             .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
252     break;
253   }
254
255   case AMDGPU::BRANCH_COND_i32: {
256     MachineInstr *NewMI =
257       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
258             AMDGPU::PREDICATE_BIT)
259             .addOperand(MI->getOperand(1))
260             .addImm(OPCODE_IS_NOT_ZERO_INT)
261             .addImm(0); // Flags
262     TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
263     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
264            .addOperand(MI->getOperand(0))
265             .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
266     break;
267   }
268
269   case AMDGPU::EG_ExportSwz:
270   case AMDGPU::R600_ExportSwz: {
271     // Instruction is left unmodified if its not the last one of its type
272     bool isLastInstructionOfItsType = true;
273     unsigned InstExportType = MI->getOperand(1).getImm();
274     for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
275          EndBlock = BB->end(); NextExportInst != EndBlock;
276          NextExportInst = llvm::next(NextExportInst)) {
277       if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
278           NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
279         unsigned CurrentInstExportType = NextExportInst->getOperand(1)
280             .getImm();
281         if (CurrentInstExportType == InstExportType) {
282           isLastInstructionOfItsType = false;
283           break;
284         }
285       }
286     }
287     bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
288     if (!EOP && !isLastInstructionOfItsType)
289       return BB;
290     unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
291     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
292             .addOperand(MI->getOperand(0))
293             .addOperand(MI->getOperand(1))
294             .addOperand(MI->getOperand(2))
295             .addOperand(MI->getOperand(3))
296             .addOperand(MI->getOperand(4))
297             .addOperand(MI->getOperand(5))
298             .addOperand(MI->getOperand(6))
299             .addImm(CfInst)
300             .addImm(EOP);
301     break;
302   }
303   case AMDGPU::RETURN: {
304     // RETURN instructions must have the live-out registers as implicit uses,
305     // otherwise they appear dead.
306     R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
307     MachineInstrBuilder MIB(*MF, MI);
308     for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
309       MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
310     return BB;
311   }
312   }
313
314   MI->eraseFromParent();
315   return BB;
316 }
317
318 //===----------------------------------------------------------------------===//
319 // Custom DAG Lowering Operations
320 //===----------------------------------------------------------------------===//
321
322 using namespace llvm::Intrinsic;
323 using namespace llvm::AMDGPUIntrinsic;
324
325 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
326   switch (Op.getOpcode()) {
327   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
328   case ISD::ROTL: return LowerROTL(Op, DAG);
329   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
330   case ISD::SELECT: return LowerSELECT(Op, DAG);
331   case ISD::STORE: return LowerSTORE(Op, DAG);
332   case ISD::LOAD: return LowerLOAD(Op, DAG);
333   case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
334   case ISD::INTRINSIC_VOID: {
335     SDValue Chain = Op.getOperand(0);
336     unsigned IntrinsicID =
337                          cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
338     switch (IntrinsicID) {
339     case AMDGPUIntrinsic::AMDGPU_store_output: {
340       MachineFunction &MF = DAG.getMachineFunction();
341       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
342       int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
343       unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
344       MFI->LiveOuts.push_back(Reg);
345       return DAG.getCopyToReg(Chain, Op.getDebugLoc(), Reg, Op.getOperand(2));
346     }
347     case AMDGPUIntrinsic::R600_store_swizzle: {
348       const SDValue Args[8] = {
349         Chain,
350         Op.getOperand(2), // Export Value
351         Op.getOperand(3), // ArrayBase
352         Op.getOperand(4), // Type
353         DAG.getConstant(0, MVT::i32), // SWZ_X
354         DAG.getConstant(1, MVT::i32), // SWZ_Y
355         DAG.getConstant(2, MVT::i32), // SWZ_Z
356         DAG.getConstant(3, MVT::i32) // SWZ_W
357       };
358       return DAG.getNode(AMDGPUISD::EXPORT, Op.getDebugLoc(), Op.getValueType(),
359           Args, 8);
360     }
361
362     // default for switch(IntrinsicID)
363     default: break;
364     }
365     // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
366     break;
367   }
368   case ISD::INTRINSIC_WO_CHAIN: {
369     unsigned IntrinsicID =
370                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
371     EVT VT = Op.getValueType();
372     DebugLoc DL = Op.getDebugLoc();
373     switch(IntrinsicID) {
374     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
375     case AMDGPUIntrinsic::R600_load_input: {
376       int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
377       unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
378       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass, Reg, VT);
379     }
380
381     case AMDGPUIntrinsic::R600_interp_input: {
382       int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
383       int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
384       MachineSDNode *interp;
385       if (ijb < 0) {
386         interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
387             MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
388         return DAG.getTargetExtractSubreg(
389             TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
390             DL, MVT::f32, SDValue(interp, 0));
391       }
392
393       if (slot % 4 < 2)
394         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
395             MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
396             CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
397                 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
398             CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
399                 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
400       else
401         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
402             MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
403             CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
404                 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
405             CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
406                 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
407
408       return SDValue(interp, slot % 2);
409     }
410
411     case r600_read_ngroups_x:
412       return LowerImplicitParameter(DAG, VT, DL, 0);
413     case r600_read_ngroups_y:
414       return LowerImplicitParameter(DAG, VT, DL, 1);
415     case r600_read_ngroups_z:
416       return LowerImplicitParameter(DAG, VT, DL, 2);
417     case r600_read_global_size_x:
418       return LowerImplicitParameter(DAG, VT, DL, 3);
419     case r600_read_global_size_y:
420       return LowerImplicitParameter(DAG, VT, DL, 4);
421     case r600_read_global_size_z:
422       return LowerImplicitParameter(DAG, VT, DL, 5);
423     case r600_read_local_size_x:
424       return LowerImplicitParameter(DAG, VT, DL, 6);
425     case r600_read_local_size_y:
426       return LowerImplicitParameter(DAG, VT, DL, 7);
427     case r600_read_local_size_z:
428       return LowerImplicitParameter(DAG, VT, DL, 8);
429
430     case r600_read_tgid_x:
431       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
432                                   AMDGPU::T1_X, VT);
433     case r600_read_tgid_y:
434       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
435                                   AMDGPU::T1_Y, VT);
436     case r600_read_tgid_z:
437       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
438                                   AMDGPU::T1_Z, VT);
439     case r600_read_tidig_x:
440       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
441                                   AMDGPU::T0_X, VT);
442     case r600_read_tidig_y:
443       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
444                                   AMDGPU::T0_Y, VT);
445     case r600_read_tidig_z:
446       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
447                                   AMDGPU::T0_Z, VT);
448     }
449     // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
450     break;
451   }
452   } // end switch(Op.getOpcode())
453   return SDValue();
454 }
455
456 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
457                                             SmallVectorImpl<SDValue> &Results,
458                                             SelectionDAG &DAG) const {
459   switch (N->getOpcode()) {
460   default: return;
461   case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
462     return;
463   case ISD::LOAD: {
464     SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
465     Results.push_back(SDValue(Node, 0));
466     Results.push_back(SDValue(Node, 1));
467     // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
468     // function
469     DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
470     return;
471   }
472   case ISD::STORE:
473     SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
474     Results.push_back(SDValue(Node, 0));
475     return;
476   }
477 }
478
479 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
480   return DAG.getNode(
481       ISD::SETCC,
482       Op.getDebugLoc(),
483       MVT::i1,
484       Op, DAG.getConstantFP(0.0f, MVT::f32),
485       DAG.getCondCode(ISD::SETNE)
486       );
487 }
488
489 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
490                                                    DebugLoc DL,
491                                                    unsigned DwordOffset) const {
492   unsigned ByteOffset = DwordOffset * 4;
493   PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
494                                       AMDGPUAS::PARAM_I_ADDRESS);
495
496   // We shouldn't be using an offset wider than 16-bits for implicit parameters.
497   assert(isInt<16>(ByteOffset));
498
499   return DAG.getLoad(VT, DL, DAG.getEntryNode(),
500                      DAG.getConstant(ByteOffset, MVT::i32), // PTR
501                      MachinePointerInfo(ConstantPointerNull::get(PtrType)),
502                      false, false, false, 0);
503 }
504
505 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
506
507   MachineFunction &MF = DAG.getMachineFunction();
508   const AMDGPUFrameLowering *TFL =
509    static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
510
511   FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
512   assert(FIN);
513
514   unsigned FrameIndex = FIN->getIndex();
515   unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
516   return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
517 }
518
519 SDValue R600TargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
520   DebugLoc DL = Op.getDebugLoc();
521   EVT VT = Op.getValueType();
522
523   return DAG.getNode(AMDGPUISD::BITALIGN, DL, VT,
524                      Op.getOperand(0),
525                      Op.getOperand(0),
526                      DAG.getNode(ISD::SUB, DL, VT,
527                                  DAG.getConstant(32, MVT::i32),
528                                  Op.getOperand(1)));
529 }
530
531 bool R600TargetLowering::isZero(SDValue Op) const {
532   if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
533     return Cst->isNullValue();
534   } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
535     return CstFP->isZero();
536   } else {
537     return false;
538   }
539 }
540
541 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
542   DebugLoc DL = Op.getDebugLoc();
543   EVT VT = Op.getValueType();
544
545   SDValue LHS = Op.getOperand(0);
546   SDValue RHS = Op.getOperand(1);
547   SDValue True = Op.getOperand(2);
548   SDValue False = Op.getOperand(3);
549   SDValue CC = Op.getOperand(4);
550   SDValue Temp;
551
552   // LHS and RHS are guaranteed to be the same value type
553   EVT CompareVT = LHS.getValueType();
554
555   // Check if we can lower this to a native operation.
556
557   // Try to lower to a SET* instruction:
558   //
559   // SET* can match the following patterns:
560   //
561   // select_cc f32, f32, -1,  0, cc_any
562   // select_cc f32, f32, 1.0f, 0.0f, cc_any
563   // select_cc i32, i32, -1,  0, cc_any
564   //
565
566   // Move hardware True/False values to the correct operand.
567   if (isHWTrueValue(False) && isHWFalseValue(True)) {
568     ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
569     std::swap(False, True);
570     CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
571   }
572
573   if (isHWTrueValue(True) && isHWFalseValue(False) &&
574       (CompareVT == VT || VT == MVT::i32)) {
575     // This can be matched by a SET* instruction.
576     return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
577   }
578
579   // Try to lower to a CND* instruction:
580   //
581   // CND* can match the following patterns:
582   //
583   // select_cc f32, 0.0, f32, f32, cc_any
584   // select_cc f32, 0.0, i32, i32, cc_any
585   // select_cc i32, 0,   f32, f32, cc_any
586   // select_cc i32, 0,   i32, i32, cc_any
587   //
588   if (isZero(LHS) || isZero(RHS)) {
589     SDValue Cond = (isZero(LHS) ? RHS : LHS);
590     SDValue Zero = (isZero(LHS) ? LHS : RHS);
591     ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
592     if (CompareVT != VT) {
593       // Bitcast True / False to the correct types.  This will end up being
594       // a nop, but it allows us to define only a single pattern in the
595       // .TD files for each CND* instruction rather than having to have
596       // one pattern for integer True/False and one for fp True/False
597       True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
598       False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
599     }
600     if (isZero(LHS)) {
601       CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
602     }
603
604     switch (CCOpcode) {
605     case ISD::SETONE:
606     case ISD::SETUNE:
607     case ISD::SETNE:
608     case ISD::SETULE:
609     case ISD::SETULT:
610     case ISD::SETOLE:
611     case ISD::SETOLT:
612     case ISD::SETLE:
613     case ISD::SETLT:
614       CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
615       Temp = True;
616       True = False;
617       False = Temp;
618       break;
619     default:
620       break;
621     }
622     SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
623         Cond, Zero,
624         True, False,
625         DAG.getCondCode(CCOpcode));
626     return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
627   }
628
629
630   // Possible Min/Max pattern
631   SDValue MinMax = LowerMinMax(Op, DAG);
632   if (MinMax.getNode()) {
633     return MinMax;
634   }
635
636   // If we make it this for it means we have no native instructions to handle
637   // this SELECT_CC, so we must lower it.
638   SDValue HWTrue, HWFalse;
639
640   if (CompareVT == MVT::f32) {
641     HWTrue = DAG.getConstantFP(1.0f, CompareVT);
642     HWFalse = DAG.getConstantFP(0.0f, CompareVT);
643   } else if (CompareVT == MVT::i32) {
644     HWTrue = DAG.getConstant(-1, CompareVT);
645     HWFalse = DAG.getConstant(0, CompareVT);
646   }
647   else {
648     assert(!"Unhandled value type in LowerSELECT_CC");
649   }
650
651   // Lower this unsupported SELECT_CC into a combination of two supported
652   // SELECT_CC operations.
653   SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
654
655   return DAG.getNode(ISD::SELECT_CC, DL, VT,
656       Cond, HWFalse,
657       True, False,
658       DAG.getCondCode(ISD::SETNE));
659 }
660
661 SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
662   return DAG.getNode(ISD::SELECT_CC,
663       Op.getDebugLoc(),
664       Op.getValueType(),
665       Op.getOperand(0),
666       DAG.getConstant(0, MVT::i32),
667       Op.getOperand(1),
668       Op.getOperand(2),
669       DAG.getCondCode(ISD::SETNE));
670 }
671
672 /// LLVM generates byte-addresed pointers.  For indirect addressing, we need to
673 /// convert these pointers to a register index.  Each register holds
674 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
675 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
676 /// for indirect addressing.
677 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
678                                                unsigned StackWidth,
679                                                SelectionDAG &DAG) const {
680   unsigned SRLPad;
681   switch(StackWidth) {
682   case 1:
683     SRLPad = 2;
684     break;
685   case 2:
686     SRLPad = 3;
687     break;
688   case 4:
689     SRLPad = 4;
690     break;
691   default: llvm_unreachable("Invalid stack width");
692   }
693
694   return DAG.getNode(ISD::SRL, Ptr.getDebugLoc(), Ptr.getValueType(), Ptr,
695                      DAG.getConstant(SRLPad, MVT::i32));
696 }
697
698 void R600TargetLowering::getStackAddress(unsigned StackWidth,
699                                          unsigned ElemIdx,
700                                          unsigned &Channel,
701                                          unsigned &PtrIncr) const {
702   switch (StackWidth) {
703   default:
704   case 1:
705     Channel = 0;
706     if (ElemIdx > 0) {
707       PtrIncr = 1;
708     } else {
709       PtrIncr = 0;
710     }
711     break;
712   case 2:
713     Channel = ElemIdx % 2;
714     if (ElemIdx == 2) {
715       PtrIncr = 1;
716     } else {
717       PtrIncr = 0;
718     }
719     break;
720   case 4:
721     Channel = ElemIdx;
722     PtrIncr = 0;
723     break;
724   }
725 }
726
727 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
728   DebugLoc DL = Op.getDebugLoc();
729   StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
730   SDValue Chain = Op.getOperand(0);
731   SDValue Value = Op.getOperand(1);
732   SDValue Ptr = Op.getOperand(2);
733
734   if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
735       Ptr->getOpcode() != AMDGPUISD::DWORDADDR) {
736     // Convert pointer from byte address to dword address.
737     Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
738                       DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
739                                   Ptr, DAG.getConstant(2, MVT::i32)));
740
741     if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
742       assert(!"Truncated and indexed stores not supported yet");
743     } else {
744       Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
745     }
746     return Chain;
747   }
748
749   EVT ValueVT = Value.getValueType();
750
751   if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
752     return SDValue();
753   }
754
755   // Lowering for indirect addressing
756
757   const MachineFunction &MF = DAG.getMachineFunction();
758   const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
759                                          getTargetMachine().getFrameLowering());
760   unsigned StackWidth = TFL->getStackWidth(MF);
761
762   Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
763
764   if (ValueVT.isVector()) {
765     unsigned NumElemVT = ValueVT.getVectorNumElements();
766     EVT ElemVT = ValueVT.getVectorElementType();
767     SDValue Stores[4];
768
769     assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
770                                       "vector width in load");
771
772     for (unsigned i = 0; i < NumElemVT; ++i) {
773       unsigned Channel, PtrIncr;
774       getStackAddress(StackWidth, i, Channel, PtrIncr);
775       Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
776                         DAG.getConstant(PtrIncr, MVT::i32));
777       SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
778                                  Value, DAG.getConstant(i, MVT::i32));
779
780       Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
781                               Chain, Elem, Ptr,
782                               DAG.getTargetConstant(Channel, MVT::i32));
783     }
784      Chain =  DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
785    } else {
786     if (ValueVT == MVT::i8) {
787       Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
788     }
789     Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
790     DAG.getTargetConstant(0, MVT::i32)); // Channel 
791   }
792
793   return Chain;
794 }
795
796 // return (512 + (kc_bank << 12)
797 static int
798 ConstantAddressBlock(unsigned AddressSpace) {
799   switch (AddressSpace) {
800   case AMDGPUAS::CONSTANT_BUFFER_0:
801     return 512;
802   case AMDGPUAS::CONSTANT_BUFFER_1:
803     return 512 + 4096;
804   case AMDGPUAS::CONSTANT_BUFFER_2:
805     return 512 + 4096 * 2;
806   case AMDGPUAS::CONSTANT_BUFFER_3:
807     return 512 + 4096 * 3;
808   case AMDGPUAS::CONSTANT_BUFFER_4:
809     return 512 + 4096 * 4;
810   case AMDGPUAS::CONSTANT_BUFFER_5:
811     return 512 + 4096 * 5;
812   case AMDGPUAS::CONSTANT_BUFFER_6:
813     return 512 + 4096 * 6;
814   case AMDGPUAS::CONSTANT_BUFFER_7:
815     return 512 + 4096 * 7;
816   case AMDGPUAS::CONSTANT_BUFFER_8:
817     return 512 + 4096 * 8;
818   case AMDGPUAS::CONSTANT_BUFFER_9:
819     return 512 + 4096 * 9;
820   case AMDGPUAS::CONSTANT_BUFFER_10:
821     return 512 + 4096 * 10;
822   case AMDGPUAS::CONSTANT_BUFFER_11:
823     return 512 + 4096 * 11;
824   case AMDGPUAS::CONSTANT_BUFFER_12:
825     return 512 + 4096 * 12;
826   case AMDGPUAS::CONSTANT_BUFFER_13:
827     return 512 + 4096 * 13;
828   case AMDGPUAS::CONSTANT_BUFFER_14:
829     return 512 + 4096 * 14;
830   case AMDGPUAS::CONSTANT_BUFFER_15:
831     return 512 + 4096 * 15;
832   default:
833     return -1;
834   }
835 }
836
837 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
838 {
839   EVT VT = Op.getValueType();
840   DebugLoc DL = Op.getDebugLoc();
841   LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
842   SDValue Chain = Op.getOperand(0);
843   SDValue Ptr = Op.getOperand(1);
844   SDValue LoweredLoad;
845
846   int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
847   if (ConstantBlock > -1) {
848     SDValue Result;
849     if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
850         dyn_cast<Constant>(LoadNode->getSrcValue()) ||
851         dyn_cast<ConstantSDNode>(Ptr)) {
852       SDValue Slots[4];
853       for (unsigned i = 0; i < 4; i++) {
854         // We want Const position encoded with the following formula :
855         // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
856         // const_index is Ptr computed by llvm using an alignment of 16.
857         // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
858         // then div by 4 at the ISel step
859         SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
860             DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
861         Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
862       }
863       Result = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Slots, 4);
864     } else {
865       // non constant ptr cant be folded, keeps it as a v4f32 load
866       Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
867           DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
868           DAG.getConstant(LoadNode->getAddressSpace() -
869                           AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
870           );
871     }
872
873     if (!VT.isVector()) {
874       Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
875           DAG.getConstant(0, MVT::i32));
876     }
877
878     SDValue MergedValues[2] = {
879         Result,
880         Chain
881     };
882     return DAG.getMergeValues(MergedValues, 2, DL);
883   }
884
885   if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
886     return SDValue();
887   }
888
889   // Lowering for indirect addressing
890   const MachineFunction &MF = DAG.getMachineFunction();
891   const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
892                                          getTargetMachine().getFrameLowering());
893   unsigned StackWidth = TFL->getStackWidth(MF);
894
895   Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
896
897   if (VT.isVector()) {
898     unsigned NumElemVT = VT.getVectorNumElements();
899     EVT ElemVT = VT.getVectorElementType();
900     SDValue Loads[4];
901
902     assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
903                                       "vector width in load");
904
905     for (unsigned i = 0; i < NumElemVT; ++i) {
906       unsigned Channel, PtrIncr;
907       getStackAddress(StackWidth, i, Channel, PtrIncr);
908       Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
909                         DAG.getConstant(PtrIncr, MVT::i32));
910       Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
911                              Chain, Ptr,
912                              DAG.getTargetConstant(Channel, MVT::i32),
913                              Op.getOperand(2));
914     }
915     for (unsigned i = NumElemVT; i < 4; ++i) {
916       Loads[i] = DAG.getUNDEF(ElemVT);
917     }
918     EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
919     LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
920   } else {
921     LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
922                               Chain, Ptr,
923                               DAG.getTargetConstant(0, MVT::i32), // Channel
924                               Op.getOperand(2));
925   }
926
927   SDValue Ops[2];
928   Ops[0] = LoweredLoad;
929   Ops[1] = Chain;
930
931   return DAG.getMergeValues(Ops, 2, DL);
932 }
933
934 /// XXX Only kernel functions are supported, so we can assume for now that
935 /// every function is a kernel function, but in the future we should use
936 /// separate calling conventions for kernel and non-kernel functions.
937 SDValue R600TargetLowering::LowerFormalArguments(
938                                       SDValue Chain,
939                                       CallingConv::ID CallConv,
940                                       bool isVarArg,
941                                       const SmallVectorImpl<ISD::InputArg> &Ins,
942                                       DebugLoc DL, SelectionDAG &DAG,
943                                       SmallVectorImpl<SDValue> &InVals) const {
944   unsigned ParamOffsetBytes = 36;
945   Function::const_arg_iterator FuncArg =
946                             DAG.getMachineFunction().getFunction()->arg_begin();
947   for (unsigned i = 0, e = Ins.size(); i < e; ++i, ++FuncArg) {
948     EVT VT = Ins[i].VT;
949     Type *ArgType = FuncArg->getType();
950     unsigned ArgSizeInBits = ArgType->isPointerTy() ?
951                              32 : ArgType->getPrimitiveSizeInBits();
952     unsigned ArgBytes = ArgSizeInBits >> 3;
953     EVT ArgVT;
954     if (ArgSizeInBits < VT.getSizeInBits()) {
955       assert(!ArgType->isFloatTy() &&
956              "Extending floating point arguments not supported yet");
957       ArgVT = MVT::getIntegerVT(ArgSizeInBits);
958     } else {
959       ArgVT = VT;
960     }
961     PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
962                                                     AMDGPUAS::PARAM_I_ADDRESS);
963     SDValue Arg = DAG.getExtLoad(ISD::ZEXTLOAD, DL, VT, DAG.getRoot(),
964                                 DAG.getConstant(ParamOffsetBytes, MVT::i32),
965                                        MachinePointerInfo(UndefValue::get(PtrTy)),
966                                        ArgVT, false, false, ArgBytes);
967     InVals.push_back(Arg);
968     ParamOffsetBytes += ArgBytes;
969   }
970   return Chain;
971 }
972
973 EVT R600TargetLowering::getSetCCResultType(EVT VT) const {
974    if (!VT.isVector()) return MVT::i32;
975    return VT.changeVectorElementTypeToInteger();
976 }
977
978 //===----------------------------------------------------------------------===//
979 // Custom DAG Optimizations
980 //===----------------------------------------------------------------------===//
981
982 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
983                                               DAGCombinerInfo &DCI) const {
984   SelectionDAG &DAG = DCI.DAG;
985
986   switch (N->getOpcode()) {
987   // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
988   case ISD::FP_ROUND: {
989       SDValue Arg = N->getOperand(0);
990       if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
991         return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), N->getValueType(0),
992                            Arg.getOperand(0));
993       }
994       break;
995     }
996
997   // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
998   // (i32 select_cc f32, f32, -1, 0 cc)
999   //
1000   // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1001   // this to one of the SET*_DX10 instructions.
1002   case ISD::FP_TO_SINT: {
1003     SDValue FNeg = N->getOperand(0);
1004     if (FNeg.getOpcode() != ISD::FNEG) {
1005       return SDValue();
1006     }
1007     SDValue SelectCC = FNeg.getOperand(0);
1008     if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1009         SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1010         SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1011         !isHWTrueValue(SelectCC.getOperand(2)) ||
1012         !isHWFalseValue(SelectCC.getOperand(3))) {
1013       return SDValue();
1014     }
1015
1016     return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N->getValueType(0),
1017                            SelectCC.getOperand(0), // LHS
1018                            SelectCC.getOperand(1), // RHS
1019                            DAG.getConstant(-1, MVT::i32), // True
1020                            DAG.getConstant(0, MVT::i32),  // Flase
1021                            SelectCC.getOperand(4)); // CC
1022
1023     break;
1024   }
1025   // Extract_vec (Build_vector) generated by custom lowering
1026   // also needs to be customly combined
1027   case ISD::EXTRACT_VECTOR_ELT: {
1028     SDValue Arg = N->getOperand(0);
1029     if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1030       if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1031         unsigned Element = Const->getZExtValue();
1032         return Arg->getOperand(Element);
1033       }
1034     }
1035     if (Arg.getOpcode() == ISD::BITCAST &&
1036         Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1037       if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1038         unsigned Element = Const->getZExtValue();
1039         return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getVTList(),
1040             Arg->getOperand(0).getOperand(Element));
1041       }
1042     }
1043   }
1044
1045   case ISD::SELECT_CC: {
1046     // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1047     //      selectcc x, y, a, b, inv(cc)
1048     //
1049     // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1050     //      selectcc x, y, a, b, cc
1051     SDValue LHS = N->getOperand(0);
1052     if (LHS.getOpcode() != ISD::SELECT_CC) {
1053       return SDValue();
1054     }
1055
1056     SDValue RHS = N->getOperand(1);
1057     SDValue True = N->getOperand(2);
1058     SDValue False = N->getOperand(3);
1059     ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1060
1061     if (LHS.getOperand(2).getNode() != True.getNode() ||
1062         LHS.getOperand(3).getNode() != False.getNode() ||
1063         RHS.getNode() != False.getNode()) {
1064       return SDValue();
1065     }
1066
1067     switch (NCC) {
1068     default: return SDValue();
1069     case ISD::SETNE: return LHS;
1070     case ISD::SETEQ: {
1071       ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1072       LHSCC = ISD::getSetCCInverse(LHSCC,
1073                                   LHS.getOperand(0).getValueType().isInteger());
1074       return DAG.getSelectCC(N->getDebugLoc(),
1075                              LHS.getOperand(0),
1076                              LHS.getOperand(1),
1077                              LHS.getOperand(2),
1078                              LHS.getOperand(3),
1079                              LHSCC);
1080     }
1081     }
1082   }
1083   case AMDGPUISD::EXPORT: {
1084     SDValue Arg = N->getOperand(1);
1085     if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1086       break;
1087     SDValue NewBldVec[4] = {
1088         DAG.getUNDEF(MVT::f32),
1089         DAG.getUNDEF(MVT::f32),
1090         DAG.getUNDEF(MVT::f32),
1091         DAG.getUNDEF(MVT::f32)
1092       };
1093     SDValue NewArgs[8] = {
1094       N->getOperand(0), // Chain
1095       SDValue(),
1096       N->getOperand(2), // ArrayBase
1097       N->getOperand(3), // Type
1098       N->getOperand(4), // SWZ_X
1099       N->getOperand(5), // SWZ_Y
1100       N->getOperand(6), // SWZ_Z
1101       N->getOperand(7) // SWZ_W
1102     };
1103     for (unsigned i = 0; i < Arg.getNumOperands(); i++) {
1104       if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg.getOperand(i))) {
1105         if (C->isZero()) {
1106           NewArgs[4 + i] = DAG.getConstant(4, MVT::i32); // SEL_0
1107         } else if (C->isExactlyValue(1.0)) {
1108           NewArgs[4 + i] = DAG.getConstant(5, MVT::i32); // SEL_0
1109         } else {
1110           NewBldVec[i] = Arg.getOperand(i);
1111         }
1112       } else {
1113         NewBldVec[i] = Arg.getOperand(i);
1114       }
1115     }
1116     DebugLoc DL = N->getDebugLoc();
1117     NewArgs[1] = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4f32, NewBldVec, 4);
1118     return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
1119   }
1120   }
1121   return SDValue();
1122 }