SelectionDAGISel.cpp revision 20a4921791eafc0cce00fb01dcacfcfc15a0d0fc
11c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
2edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman//
31c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//                     The LLVM Compiler Infrastructure
41c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//
51c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner// This file was developed by the LLVM research group and is distributed under
61c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details.
7edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman//
81c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===----------------------------------------------------------------------===//
91c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//
101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner// This implements the SelectionDAGISel class.
111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//
121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===----------------------------------------------------------------------===//
131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#define DEBUG_TYPE "isel"
151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/SelectionDAGISel.h"
16a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng#include "llvm/CodeGen/ScheduleDAG.h"
17adf6a965a321372c640845407195594835921eb4Chris Lattner#include "llvm/CallingConv.h"
181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Constants.h"
191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/DerivedTypes.h"
201c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Function.h"
2136ce69195ed488034d0bb11180cc2ebd923679c8Chris Lattner#include "llvm/GlobalVariable.h"
22ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner#include "llvm/InlineAsm.h"
231c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Instructions.h"
241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Intrinsics.h"
25b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner#include "llvm/CodeGen/IntrinsicLowering.h"
26b2efb853f00d45b1c8d57f92acd0028fbdeffda6Jim Laskey#include "llvm/CodeGen/MachineDebugInfo.h"
271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/MachineFunction.h"
281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/MachineFrameInfo.h"
291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/MachineInstrBuilder.h"
301c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/SelectionDAG.h"
311c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/CodeGen/SSARegMap.h"
32fa57702388f139e964befecb4b98c7dfe836945fChris Lattner#include "llvm/Target/MRegisterInfo.h"
331c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetData.h"
341c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetFrameInfo.h"
351c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetInstrInfo.h"
361c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetLowering.h"
371c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Target/TargetMachine.h"
38495a0b51915eb763576874f29192820b731edc22Chris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h"
397944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner#include "llvm/Support/CommandLine.h"
407c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner#include "llvm/Support/MathExtras.h"
411c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Support/Debug.h"
421c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include <map>
434e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner#include <set>
441c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include <iostream>
457e88103cdea8c36b2229dae8c60def14e3816512Jeff Cohen#include <algorithm>
461c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerusing namespace llvm;
471c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
48da8abb02397d7cd62a1e16e7c534edd7096ac873Chris Lattner#ifndef NDEBUG
497944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattnerstatic cl::opt<bool>
50a9c2091cd38e401c846391c9951ff416e709b65eEvan ChengViewISelDAGs("view-isel-dags", cl::Hidden,
51a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng          cl::desc("Pop up a window to show isel dags as they are selected"));
52a9c2091cd38e401c846391c9951ff416e709b65eEvan Chengstatic cl::opt<bool>
53a9c2091cd38e401c846391c9951ff416e709b65eEvan ChengViewSchedDAGs("view-sched-dags", cl::Hidden,
54a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng          cl::desc("Pop up a window to show sched dags as they are processed"));
557944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner#else
56a9c2091cd38e401c846391c9951ff416e709b65eEvan Chengstatic const bool ViewISelDAGs = 0;
57a9c2091cd38e401c846391c9951ff416e709b65eEvan Chengstatic const bool ViewSchedDAGs = 0;
587944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner#endif
597944d9d9957db1efe085d9df3ea89826f50029b7Chris Lattner
6020a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner// Scheduling heuristics
6120a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattnerenum SchedHeuristics {
6220a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner  defaultScheduling,      // Let the target specify its preference.
6320a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner  noScheduling,           // No scheduling, emit breadth first sequence.
6420a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner  simpleScheduling,       // Two pass, min. critical path, max. utilization.
6520a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner  simpleNoItinScheduling, // Same as above exact using generic latency.
6620a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner  listSchedulingBURR,     // Bottom up reg reduction list scheduling.
6720a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner  listSchedulingTD        // Top-down list scheduler.
6820a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner};
6920a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner
704ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Chengnamespace {
714ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  cl::opt<SchedHeuristics>
724ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  ISHeuristic(
734ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng    "sched",
744ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng    cl::desc("Choose scheduling style"),
753f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng    cl::init(defaultScheduling),
764ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng    cl::values(
773f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng      clEnumValN(defaultScheduling, "default",
783f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng                 "Target preferred scheduling style"),
794ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng      clEnumValN(noScheduling, "none",
8017d52f723421ce28d1b9fe2fc058366ed43ec094Jim Laskey                 "No scheduling: breadth first sequencing"),
814ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng      clEnumValN(simpleScheduling, "simple",
824ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng                 "Simple two pass scheduling: minimize critical path "
834ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng                 "and maximize processor utilization"),
844ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng      clEnumValN(simpleNoItinScheduling, "simple-noitin",
854ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng                 "Simple two pass scheduling: Same as simple "
864ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng                 "except using generic latency"),
873f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng      clEnumValN(listSchedulingBURR, "list-burr",
88f0f9c90204c650b9f3c3feb02ccfcb1e40c6acddEvan Cheng                 "Bottom up register reduction list scheduling"),
8903fc53c174e654adae4e42a0c352c7937de2cd87Chris Lattner      clEnumValN(listSchedulingTD, "list-td",
9003fc53c174e654adae4e42a0c352c7937de2cd87Chris Lattner                 "Top-down list scheduler"),
914ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng      clEnumValEnd));
924ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng} // namespace
934ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng
94864635ad7b3046d3042311423071152c613961deChris Lattnernamespace {
95864635ad7b3046d3042311423071152c613961deChris Lattner  /// RegsForValue - This struct represents the physical registers that a
96864635ad7b3046d3042311423071152c613961deChris Lattner  /// particular value is assigned and the type information about the value.
97864635ad7b3046d3042311423071152c613961deChris Lattner  /// This is needed because values can be promoted into larger registers and
98864635ad7b3046d3042311423071152c613961deChris Lattner  /// expanded into multiple smaller registers than the value.
99864635ad7b3046d3042311423071152c613961deChris Lattner  struct RegsForValue {
100864635ad7b3046d3042311423071152c613961deChris Lattner    /// Regs - This list hold the register (for legal and promoted values)
101864635ad7b3046d3042311423071152c613961deChris Lattner    /// or register set (for expanded values) that the value should be assigned
102864635ad7b3046d3042311423071152c613961deChris Lattner    /// to.
103864635ad7b3046d3042311423071152c613961deChris Lattner    std::vector<unsigned> Regs;
104864635ad7b3046d3042311423071152c613961deChris Lattner
105864635ad7b3046d3042311423071152c613961deChris Lattner    /// RegVT - The value type of each register.
106864635ad7b3046d3042311423071152c613961deChris Lattner    ///
107864635ad7b3046d3042311423071152c613961deChris Lattner    MVT::ValueType RegVT;
108864635ad7b3046d3042311423071152c613961deChris Lattner
109864635ad7b3046d3042311423071152c613961deChris Lattner    /// ValueVT - The value type of the LLVM value, which may be promoted from
110864635ad7b3046d3042311423071152c613961deChris Lattner    /// RegVT or made from merging the two expanded parts.
111864635ad7b3046d3042311423071152c613961deChris Lattner    MVT::ValueType ValueVT;
112864635ad7b3046d3042311423071152c613961deChris Lattner
113864635ad7b3046d3042311423071152c613961deChris Lattner    RegsForValue() : RegVT(MVT::Other), ValueVT(MVT::Other) {}
114864635ad7b3046d3042311423071152c613961deChris Lattner
115864635ad7b3046d3042311423071152c613961deChris Lattner    RegsForValue(unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt)
116864635ad7b3046d3042311423071152c613961deChris Lattner      : RegVT(regvt), ValueVT(valuevt) {
117864635ad7b3046d3042311423071152c613961deChris Lattner        Regs.push_back(Reg);
118864635ad7b3046d3042311423071152c613961deChris Lattner    }
119864635ad7b3046d3042311423071152c613961deChris Lattner    RegsForValue(const std::vector<unsigned> &regs,
120864635ad7b3046d3042311423071152c613961deChris Lattner                 MVT::ValueType regvt, MVT::ValueType valuevt)
121864635ad7b3046d3042311423071152c613961deChris Lattner      : Regs(regs), RegVT(regvt), ValueVT(valuevt) {
122864635ad7b3046d3042311423071152c613961deChris Lattner    }
123864635ad7b3046d3042311423071152c613961deChris Lattner
124864635ad7b3046d3042311423071152c613961deChris Lattner    /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
125864635ad7b3046d3042311423071152c613961deChris Lattner    /// this value and returns the result as a ValueVT value.  This uses
126864635ad7b3046d3042311423071152c613961deChris Lattner    /// Chain/Flag as the input and updates them for the output Chain/Flag.
127864635ad7b3046d3042311423071152c613961deChris Lattner    SDOperand getCopyFromRegs(SelectionDAG &DAG,
1289f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner                              SDOperand &Chain, SDOperand &Flag) const;
129c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
130c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
131c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    /// specified value into the registers specified by this object.  This uses
132c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    /// Chain/Flag as the input and updates them for the output Chain/Flag.
133c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    void getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
1349f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner                       SDOperand &Chain, SDOperand &Flag) const;
135c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
136c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    /// AddInlineAsmOperands - Add this value to the specified inlineasm node
137c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    /// operand list.  This adds the code marker and includes the number of
138c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    /// values added into it.
139c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
1409f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner                              std::vector<SDOperand> &Ops) const;
141864635ad7b3046d3042311423071152c613961deChris Lattner  };
142864635ad7b3046d3042311423071152c613961deChris Lattner}
1434ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng
1441c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnernamespace llvm {
1451c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  //===--------------------------------------------------------------------===//
1461c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  /// FunctionLoweringInfo - This contains information that is global to a
1471c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  /// function that is used when lowering a region of the function.
148f26bc8ef4827cf0023a7052b62b920b41813d473Chris Lattner  class FunctionLoweringInfo {
149f26bc8ef4827cf0023a7052b62b920b41813d473Chris Lattner  public:
1501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    TargetLowering &TLI;
1511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    Function &Fn;
1521c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MachineFunction &MF;
1531c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SSARegMap *RegMap;
1541c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1551c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
1561c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1571c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
1581c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    std::map<const BasicBlock*, MachineBasicBlock *> MBBMap;
1591c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// ValueMap - Since we emit code for the function a basic block at a time,
1611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// we must remember which virtual registers hold the values for
1621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// cross-basic-block values.
1631c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    std::map<const Value*, unsigned> ValueMap;
1641c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
1661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// the entry block.  This allows the allocas to be efficiently referenced
1671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    /// anywhere in the function.
1681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    std::map<const AllocaInst*, int> StaticAllocaMap;
1691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    unsigned MakeReg(MVT::ValueType VT) {
1711c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
1721c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
173edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
1741c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    unsigned CreateRegForValue(const Value *V) {
1751c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      MVT::ValueType VT = TLI.getValueType(V->getType());
1761c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // The common case is that we will only create one register for this
1771c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // value.  If we have that case, create and return the virtual register.
1781c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      unsigned NV = TLI.getNumElements(VT);
179fb849800ea2040c188365c265421ad54fbdcf219Chris Lattner      if (NV == 1) {
180fb849800ea2040c188365c265421ad54fbdcf219Chris Lattner        // If we are promoting this value, pick the next largest supported type.
18198e5c0e5e4c5be1b531d287d0a1373a62fe562e2Chris Lattner        return MakeReg(TLI.getTypeToTransformTo(VT));
182fb849800ea2040c188365c265421ad54fbdcf219Chris Lattner      }
183edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
1841c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // If this value is represented with multiple target registers, make sure
185864635ad7b3046d3042311423071152c613961deChris Lattner      // to create enough consecutive registers of the right (smaller) type.
1861c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      unsigned NT = VT-1;  // Find the type to use.
1871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      while (TLI.getNumElements((MVT::ValueType)NT) != 1)
1881c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        --NT;
189edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
1901c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      unsigned R = MakeReg((MVT::ValueType)NT);
1911c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      for (unsigned i = 1; i != NV; ++i)
1921c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        MakeReg((MVT::ValueType)NT);
1931c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      return R;
1941c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
195edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
1961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    unsigned InitializeRegForValue(const Value *V) {
1971c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      unsigned &R = ValueMap[V];
1981c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      assert(R == 0 && "Already initialized this value register!");
1991c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      return R = CreateRegForValue(V);
2001c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
2011c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  };
2021c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
2031c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2041c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
2051c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner/// PHI nodes or outside of the basic block that defines it.
2061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerstatic bool isUsedOutsideOfDefiningBlock(Instruction *I) {
2071c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (isa<PHINode>(I)) return true;
2081c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  BasicBlock *BB = I->getParent();
2091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
2101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
2111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      return true;
2121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  return false;
2131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
2141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
215bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
216bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner/// entry block, return true.
217bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattnerstatic bool isOnlyUsedInEntryBlock(Argument *A) {
218bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  BasicBlock *Entry = A->getParent()->begin();
219bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
220bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    if (cast<Instruction>(*UI)->getParent() != Entry)
221bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      return false;  // Use not in entry block.
222bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  return true;
223bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner}
224bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner
2251c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris LattnerFunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
226edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman                                           Function &fn, MachineFunction &mf)
2271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
2281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
229bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // Create a vreg for each argument register that is not dead and is used
230bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // outside of the entry block for the function.
23116ce0df92717cd1474029d87efe596d000dc2caaChris Lattner  for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end();
23216ce0df92717cd1474029d87efe596d000dc2caaChris Lattner       AI != E; ++AI)
233bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    if (!isOnlyUsedInEntryBlock(AI))
234bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      InitializeRegForValue(AI);
2351c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
236bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // Initialize the mapping of values to registers.  This is only set up for
237bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // instruction values that are used outside of the block that defines
238bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // them.
2392aeaf4e839438d51766996006fc22310d05ab2a7Jeff Cohen  Function::iterator BB = Fn.begin(), EB = Fn.end();
2401c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
2411c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
2421c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) {
2431c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        const Type *Ty = AI->getAllocatedType();
2441c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
245ae232e7a1055033436370c0b3aecf054fa44d5e7Nate Begeman        unsigned Align =
246ae232e7a1055033436370c0b3aecf054fa44d5e7Nate Begeman          std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
247ae232e7a1055033436370c0b3aecf054fa44d5e7Nate Begeman                   AI->getAlignment());
248a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner
249a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        // If the alignment of the value is smaller than the size of the value,
250a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        // and if the size of the value is particularly small (<= 8 bytes),
251a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        // round up to the size of the value for potentially better performance.
252a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        //
253a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        // FIXME: This could be made better with a preferred alignment hook in
254a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        // TargetData.  It serves primarily to 8-byte align doubles for X86.
255a8217e3000b5b01c4a95316aef078a9d02a9a119Chris Lattner        if (Align < TySize && TySize <= 8) Align = TySize;
2562dfa8192abaad8a3c29132dba50e8b85218e918cChris Lattner        TySize *= CUI->getValue();   // Get total allocated size.
257d222f6ab67472fa2b2e211172a11b43905aa9445Chris Lattner        if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
2581c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        StaticAllocaMap[AI] =
259f26bc8ef4827cf0023a7052b62b920b41813d473Chris Lattner          MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      }
2611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2622aeaf4e839438d51766996006fc22310d05ab2a7Jeff Cohen  for (; BB != EB; ++BB)
2632aeaf4e839438d51766996006fc22310d05ab2a7Jeff Cohen    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
2641c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
2651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        if (!isa<AllocaInst>(I) ||
2661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner            !StaticAllocaMap.count(cast<AllocaInst>(I)))
2671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner          InitializeRegForValue(I);
2681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Create an initial MachineBasicBlock for each LLVM BasicBlock in F.  This
2701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // also creates the initial PHI MachineInstrs, though none of the input
2711c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // operands are populated.
2722aeaf4e839438d51766996006fc22310d05ab2a7Jeff Cohen  for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) {
2731c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MachineBasicBlock *MBB = new MachineBasicBlock(BB);
2741c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MBBMap[BB] = MBB;
2751c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MF.getBasicBlockList().push_back(MBB);
2761c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2771c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
2781c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // appropriate.
2791c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    PHINode *PN;
2801c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    for (BasicBlock::iterator I = BB->begin();
281f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner         (PN = dyn_cast<PHINode>(I)); ++I)
282f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner      if (!PN->use_empty()) {
283f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        unsigned NumElements =
284f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          TLI.getNumElements(TLI.getValueType(PN->getType()));
285f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        unsigned PHIReg = ValueMap[PN];
286f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        assert(PHIReg &&"PHI node does not have an assigned virtual register!");
287f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        for (unsigned i = 0; i != NumElements; ++i)
288f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i);
289f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner      }
2901c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
2911c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
2921c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2931c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2941c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner//===----------------------------------------------------------------------===//
2961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner/// SelectionDAGLowering - This is the common target-independent lowering
2971c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner/// implementation that is parameterized by a TargetLowering object.
2981c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner/// Also, targets can overload any lowering method.
2991c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner///
3001c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnernamespace llvm {
3011c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerclass SelectionDAGLowering {
3021c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MachineBasicBlock *CurMBB;
3031c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3041c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  std::map<const Value*, SDOperand> NodeMap;
3051c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
306d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  /// PendingLoads - Loads are not emitted to the program immediately.  We bunch
307d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  /// them up and then emit token factor nodes when possible.  This allows us to
308d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  /// get simple disambiguation between loads without worrying about alias
309d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  /// analysis.
310d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  std::vector<SDOperand> PendingLoads;
311d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner
3121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerpublic:
3131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // TLI - This is information that describes the available target features we
3141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // need for lowering.  This indicates when operations are unavailable,
3151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // implemented with a libcall, etc.
3161c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  TargetLowering &TLI;
3171c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SelectionDAG &DAG;
3181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  const TargetData &TD;
3191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3201c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  /// FuncInfo - Information about the function as a whole.
3211c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  ///
3221c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  FunctionLoweringInfo &FuncInfo;
3231c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
325edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman                       FunctionLoweringInfo &funcinfo)
3261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
3271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      FuncInfo(funcinfo) {
3281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
3291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
330a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  /// getRoot - Return the current virtual root of the Selection DAG.
331a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  ///
332a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  SDOperand getRoot() {
333d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    if (PendingLoads.empty())
334d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner      return DAG.getRoot();
335edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
336d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    if (PendingLoads.size() == 1) {
337d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner      SDOperand Root = PendingLoads[0];
338d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner      DAG.setRoot(Root);
339d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner      PendingLoads.clear();
340d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner      return Root;
341d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    }
342d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner
343d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    // Otherwise, we have to make a token factor node.
344d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads);
345d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    PendingLoads.clear();
346d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    DAG.setRoot(Root);
347d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    return Root;
348a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  }
349a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner
3501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visit(Instruction &I) { visit(I.getOpcode(), I); }
3511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3521c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visit(unsigned Opcode, User &I) {
3531c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    switch (Opcode) {
3541c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    default: assert(0 && "Unknown instruction type encountered!");
3551c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner             abort();
3561c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // Build the switch statement using the Instruction.def file.
3571c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#define HANDLE_INST(NUM, OPCODE, CLASS) \
3581c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
3591c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner#include "llvm/Instruction.def"
3601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
3611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
3621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3631c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
3641c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand getIntPtrConstant(uint64_t Val) {
3671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    return DAG.getConstant(Val, TLI.getPointerTy());
3681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
3691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand getValue(const Value *V) {
3711c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SDOperand &N = NodeMap[V];
3721c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (N.Val) return N;
3731c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
3748cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman    const Type *VTy = V->getType();
3758cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman    MVT::ValueType VT = TLI.getValueType(VTy);
3761c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)))
3771c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
3781c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        visit(CE->getOpcode(), *CE);
3791c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        assert(N.Val && "visit didn't populate the ValueMap!");
3801c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        return N;
3811c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
3821c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        return N = DAG.getGlobalAddress(GV, VT);
3831c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      } else if (isa<ConstantPointerNull>(C)) {
3841c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        return N = DAG.getConstant(0, TLI.getPointerTy());
3851c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      } else if (isa<UndefValue>(C)) {
386b882752bd04602249d391699dc7183de007f8964Nate Begeman        return N = DAG.getNode(ISD::UNDEF, VT);
3871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3881c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        return N = DAG.getConstantFP(CFP->getValue(), VT);
3898cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman      } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) {
3908cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        unsigned NumElements = PTy->getNumElements();
3918cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
3928cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements);
3938cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman
3948cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        // Now that we know the number and type of the elements, push a
3958cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        // Constant or ConstantFP node onto the ops list for each element of
3968cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        // the packed constant.
3978cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        std::vector<SDOperand> Ops;
3983b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner        if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
3993b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner          if (MVT::isFloatingPoint(PVT)) {
4003b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner            for (unsigned i = 0; i != NumElements; ++i) {
4013b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner              const ConstantFP *El = cast<ConstantFP>(CP->getOperand(i));
4023b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner              Ops.push_back(DAG.getConstantFP(El->getValue(), PVT));
4033b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner            }
4043b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner          } else {
4053b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner            for (unsigned i = 0; i != NumElements; ++i) {
4063b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner              const ConstantIntegral *El =
4073b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner                cast<ConstantIntegral>(CP->getOperand(i));
4083b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner              Ops.push_back(DAG.getConstant(El->getRawValue(), PVT));
4093b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner            }
4103b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner          }
4113b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner        } else {
4123b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner          assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!");
4133b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner          SDOperand Op;
4148cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman          if (MVT::isFloatingPoint(PVT))
4153b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner            Op = DAG.getConstantFP(0, PVT);
4168cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman          else
4173b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner            Op = DAG.getConstant(0, PVT);
4183b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner          Ops.assign(NumElements, Op);
4198cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        }
4203b841e9f52611c1f92c60f979c775adf2c1fe22dChris Lattner
4218cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        // Handle the case where we have a 1-element vector, in which
4228cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman        // case we want to immediately turn it into a scalar constant.
423cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman        if (Ops.size() == 1) {
4248cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman          return N = Ops[0];
425cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman        } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) {
426cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman          return N = DAG.getNode(ISD::ConstantVec, TVT, Ops);
427cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman        } else {
428cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman          // If the packed type isn't legal, then create a ConstantVec node with
429cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman          // generic Vector type instead.
430860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng          SDOperand Num = DAG.getConstant(NumElements, MVT::i32);
431860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng          SDOperand Typ = DAG.getValueType(PVT);
432860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng          Ops.insert(Ops.begin(), Typ);
433860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng          Ops.insert(Ops.begin(), Num);
434860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng          return N = DAG.getNode(ISD::VConstant, MVT::Vector, Ops);
435cc827e60b67b2cbcf08a37b119e68081e4171b8aNate Begeman        }
4361c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      } else {
4371c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        // Canonicalize all constant ints to be unsigned.
4381c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT);
4391c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      }
4401c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
4411c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
4421c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      std::map<const AllocaInst*, int>::iterator SI =
4431c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        FuncInfo.StaticAllocaMap.find(AI);
4441c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      if (SI != FuncInfo.StaticAllocaMap.end())
4451c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
4461c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
4471c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
4481c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    std::map<const Value*, unsigned>::const_iterator VMI =
4491c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      FuncInfo.ValueMap.find(V);
4501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!");
451c8ea3c47103656a0924909f41651bf5d396c26cdChris Lattner
452d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    unsigned InReg = VMI->second;
453d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner
454d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    // If this type is not legal, make it so now.
455d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT);
456d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner
457d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT);
458d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    if (DestVT < VT) {
459d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner      // Source must be expanded.  This input value is actually coming from the
460d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner      // register pair VMI->second and VMI->second+1.
461d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner      N = DAG.getNode(ISD::BUILD_PAIR, VT, N,
462d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner                      DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT));
463d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    } else {
464d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner      if (DestVT > VT) { // Promotion case
465d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner        if (MVT::isFloatingPoint(VT))
466d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner          N = DAG.getNode(ISD::FP_ROUND, VT, N);
467d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner        else
468d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner          N = DAG.getNode(ISD::TRUNCATE, VT, N);
469d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner      }
470d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    }
471d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner
472d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    return N;
4731c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
4741c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
4751c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  const SDOperand &setValue(const Value *V, SDOperand NewN) {
4761c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SDOperand &N = NodeMap[V];
4771c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    assert(N.Val == 0 && "Already set a value for this node!");
4781c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    return N = NewN;
4791c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
4804e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner
481864635ad7b3046d3042311423071152c613961deChris Lattner  RegsForValue GetRegistersForValue(const std::string &ConstrCode,
482864635ad7b3046d3042311423071152c613961deChris Lattner                                    MVT::ValueType VT,
483864635ad7b3046d3042311423071152c613961deChris Lattner                                    bool OutReg, bool InReg,
484864635ad7b3046d3042311423071152c613961deChris Lattner                                    std::set<unsigned> &OutputRegs,
485864635ad7b3046d3042311423071152c613961deChris Lattner                                    std::set<unsigned> &InputRegs);
486864635ad7b3046d3042311423071152c613961deChris Lattner
4871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Terminator instructions.
4881c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitRet(ReturnInst &I);
4891c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitBr(BranchInst &I);
4901c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitUnreachable(UnreachableInst &I) { /* noop */ }
4911c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
4921c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // These all get lowered before this pass.
493c0f4cd99316b0b9ae10b5774d1036d74d838f0afRobert Bocchino  void visitExtractElement(ExtractElementInst &I) { assert(0 && "TODO"); }
4944eb2e3a6f45e6f0a4a8f0002918f8d14c34169c1Robert Bocchino  void visitInsertElement(InsertElementInst &I) { assert(0 && "TODO"); }
4951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); }
4961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); }
4971c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); }
4981c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
4991c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  //
5005fbb5d2459a5410590f285250faa604576308a93Nate Begeman  void visitBinary(User &I, unsigned IntOp, unsigned FPOp, unsigned VecOp);
501e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman  void visitShift(User &I, unsigned Opcode);
5025fbb5d2459a5410590f285250faa604576308a93Nate Begeman  void visitAdd(User &I) {
5035fbb5d2459a5410590f285250faa604576308a93Nate Begeman    visitBinary(I, ISD::ADD, ISD::FADD, ISD::VADD);
50401b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner  }
505b9fccc41933648647e3f7669612c683eb5de0d58Chris Lattner  void visitSub(User &I);
5065fbb5d2459a5410590f285250faa604576308a93Nate Begeman  void visitMul(User &I) {
5075fbb5d2459a5410590f285250faa604576308a93Nate Begeman    visitBinary(I, ISD::MUL, ISD::FMUL, ISD::VMUL);
50801b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner  }
5091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitDiv(User &I) {
51001b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner    const Type *Ty = I.getType();
5113e1ce5a44d3d59b2b9ca68a21261f0f487d69269Evan Cheng    visitBinary(I,
5123e1ce5a44d3d59b2b9ca68a21261f0f487d69269Evan Cheng                Ty->isSigned() ? ISD::SDIV : ISD::UDIV, ISD::FDIV,
5133e1ce5a44d3d59b2b9ca68a21261f0f487d69269Evan Cheng                Ty->isSigned() ? ISD::VSDIV : ISD::VUDIV);
5141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
5151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitRem(User &I) {
51601b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner    const Type *Ty = I.getType();
5175fbb5d2459a5410590f285250faa604576308a93Nate Begeman    visitBinary(I, Ty->isSigned() ? ISD::SREM : ISD::UREM, ISD::FREM, 0);
5181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
5193e1ce5a44d3d59b2b9ca68a21261f0f487d69269Evan Cheng  void visitAnd(User &I) { visitBinary(I, ISD::AND, 0, ISD::VAND); }
5203e1ce5a44d3d59b2b9ca68a21261f0f487d69269Evan Cheng  void visitOr (User &I) { visitBinary(I, ISD::OR,  0, ISD::VOR); }
5213e1ce5a44d3d59b2b9ca68a21261f0f487d69269Evan Cheng  void visitXor(User &I) { visitBinary(I, ISD::XOR, 0, ISD::VXOR); }
522e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman  void visitShl(User &I) { visitShift(I, ISD::SHL); }
523e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman  void visitShr(User &I) {
524e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman    visitShift(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA);
5251c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
5261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc);
5281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); }
5291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); }
5301c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); }
5311c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); }
5321c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); }
5331c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); }
5341c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5351c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitGetElementPtr(User &I);
5361c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitCast(User &I);
5371c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitSelect(User &I);
5381c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  //
5391c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5401c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitMalloc(MallocInst &I);
5411c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitFree(FreeInst &I);
5421c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitAlloca(AllocaInst &I);
5431c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitLoad(LoadInst &I);
5441c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitStore(StoreInst &I);
5451c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
5461c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitCall(CallInst &I);
547ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  void visitInlineAsm(CallInst &I);
548c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic);
5491c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitVAStart(CallInst &I);
5511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitVAArg(VAArgInst &I);
5521c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitVAEnd(CallInst &I);
5531c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitVACopy(CallInst &I);
55439ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner  void visitFrameReturnAddress(CallInst &I, bool isFrameAddress);
5551c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5567041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner  void visitMemIntrinsic(CallInst &I, unsigned Op);
5571c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5581c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitUserOp1(Instruction &I) {
5591c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    assert(0 && "UserOp1 should not exist at instruction selection time!");
5601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    abort();
5611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
5621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  void visitUserOp2(Instruction &I) {
5631c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    assert(0 && "UserOp2 should not exist at instruction selection time!");
5641c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    abort();
5651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
5661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner};
5671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner} // end namespace llvm
5681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
5691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitRet(ReturnInst &I) {
5701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (I.getNumOperands() == 0) {
571a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner    DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot()));
5721c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    return;
5731c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
574ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman  std::vector<SDOperand> NewValues;
575ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman  NewValues.push_back(getRoot());
576ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
577ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    SDOperand RetOp = getValue(I.getOperand(i));
578ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman
579ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    // If this is an integer return value, we need to promote it ourselves to
580ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    // the full width of a register, since LegalizeOp will use ANY_EXTEND rather
581ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    // than sign/zero.
582ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    if (MVT::isInteger(RetOp.getValueType()) &&
583ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman        RetOp.getValueType() < MVT::i64) {
584ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman      MVT::ValueType TmpVT;
585ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman      if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote)
586ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman        TmpVT = TLI.getTypeToTransformTo(MVT::i32);
587ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman      else
588ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman        TmpVT = MVT::i32;
5891c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
590ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman      if (I.getOperand(i)->getType()->isSigned())
591ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman        RetOp = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, RetOp);
592ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman      else
593ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman        RetOp = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, RetOp);
594ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    }
595ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman    NewValues.push_back(RetOp);
5961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
597ee625573b5b39b91441fc6ea23f3ba415abdc71fNate Begeman  DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, NewValues));
5981c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
5991c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
6001c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitBr(BranchInst &I) {
6011c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Update machine-CFG edges.
6021c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
6031c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
6041c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Figure out which block is immediately after the current one.
6051c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MachineBasicBlock *NextBlock = 0;
6061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MachineFunction::iterator BBI = CurMBB;
6071c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (++BBI != CurMBB->getParent()->end())
6081c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    NextBlock = BBI;
6091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
6101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (I.isUnconditional()) {
6111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // If this is not a fall-through branch, emit the branch.
6121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (Succ0MBB != NextBlock)
613a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner      DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(),
614dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman                              DAG.getBasicBlock(Succ0MBB)));
6151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  } else {
6161c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
6171c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
6181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SDOperand Cond = getValue(I.getCondition());
6191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    if (Succ1MBB == NextBlock) {
6201c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // If the condition is false, fall through.  This means we should branch
6211c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // if the condition is true to Succ #0.
622a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner      DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
623dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman                              Cond, DAG.getBasicBlock(Succ0MBB)));
6241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    } else if (Succ0MBB == NextBlock) {
6251c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // If the condition is true, fall through.  This means we should branch if
6261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      // the condition is false to Succ #1.  Invert the condition first.
6271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      SDOperand True = DAG.getConstant(1, Cond.getValueType());
6281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
629a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner      DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
630dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman                              Cond, DAG.getBasicBlock(Succ1MBB)));
6311c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    } else {
632e7ccd4acd2ee9753e6d212efd522be5c7de154d7Chris Lattner      std::vector<SDOperand> Ops;
633e7ccd4acd2ee9753e6d212efd522be5c7de154d7Chris Lattner      Ops.push_back(getRoot());
634298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng      // If the false case is the current basic block, then this is a self
635298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng      // loop. We do not want to emit "Loop: ... brcond Out; br Loop", as it
636298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng      // adds an extra instruction in the loop.  Instead, invert the
637298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng      // condition and emit "Loop: ... br!cond Loop; br Out.
638298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng      if (CurMBB == Succ1MBB) {
639298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng        std::swap(Succ0MBB, Succ1MBB);
640298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng        SDOperand True = DAG.getConstant(1, Cond.getValueType());
641298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng        Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
642298ebf2bd80ca415e58bbcbd9866ee58f167b620Evan Cheng      }
643e7ccd4acd2ee9753e6d212efd522be5c7de154d7Chris Lattner      Ops.push_back(Cond);
644e7ccd4acd2ee9753e6d212efd522be5c7de154d7Chris Lattner      Ops.push_back(DAG.getBasicBlock(Succ0MBB));
645e7ccd4acd2ee9753e6d212efd522be5c7de154d7Chris Lattner      Ops.push_back(DAG.getBasicBlock(Succ1MBB));
646e7ccd4acd2ee9753e6d212efd522be5c7de154d7Chris Lattner      DAG.setRoot(DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops));
6471c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
6481c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
6491c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
6501c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
651b9fccc41933648647e3f7669612c683eb5de0d58Chris Lattnervoid SelectionDAGLowering::visitSub(User &I) {
652b9fccc41933648647e3f7669612c683eb5de0d58Chris Lattner  // -0.0 - X --> fneg
65301b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner  if (I.getType()->isFloatingPoint()) {
65401b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner    if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
65501b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner      if (CFP->isExactlyValue(-0.0)) {
65601b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner        SDOperand Op2 = getValue(I.getOperand(1));
65701b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner        setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
65801b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner        return;
65901b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner      }
66001b3d73c20f5afb8265ae943a8ba23c2238c5eeaChris Lattner  }
6615fbb5d2459a5410590f285250faa604576308a93Nate Begeman  visitBinary(I, ISD::SUB, ISD::FSUB, ISD::VSUB);
662b9fccc41933648647e3f7669612c683eb5de0d58Chris Lattner}
663b9fccc41933648647e3f7669612c683eb5de0d58Chris Lattner
6645fbb5d2459a5410590f285250faa604576308a93Nate Begemanvoid SelectionDAGLowering::visitBinary(User &I, unsigned IntOp, unsigned FPOp,
6655fbb5d2459a5410590f285250faa604576308a93Nate Begeman                                       unsigned VecOp) {
6665fbb5d2459a5410590f285250faa604576308a93Nate Begeman  const Type *Ty = I.getType();
6671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Op1 = getValue(I.getOperand(0));
6681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Op2 = getValue(I.getOperand(1));
6692c49f2795514a7c56b680ba0310d7eb0a8a43289Chris Lattner
670b67eb9131c5c04d2e55c6b587fde954619feceafChris Lattner  if (Ty->isIntegral()) {
6715fbb5d2459a5410590f285250faa604576308a93Nate Begeman    setValue(&I, DAG.getNode(IntOp, Op1.getValueType(), Op1, Op2));
6725fbb5d2459a5410590f285250faa604576308a93Nate Begeman  } else if (Ty->isFloatingPoint()) {
6735fbb5d2459a5410590f285250faa604576308a93Nate Begeman    setValue(&I, DAG.getNode(FPOp, Op1.getValueType(), Op1, Op2));
6745fbb5d2459a5410590f285250faa604576308a93Nate Begeman  } else {
6755fbb5d2459a5410590f285250faa604576308a93Nate Begeman    const PackedType *PTy = cast<PackedType>(Ty);
6764ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    unsigned NumElements = PTy->getNumElements();
6774ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
678f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements);
6794ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman
6804ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    // Immediately scalarize packed types containing only one element, so that
681f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    // the Legalize pass does not have to deal with them.  Similarly, if the
682f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    // abstract vector is going to turn into one that the target natively
683f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    // supports, generate that type now so that Legalize doesn't have to deal
684f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    // with that either.  These steps ensure that Legalize only has to handle
685f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    // vector types in its Expand case.
686f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    unsigned Opc = MVT::isFloatingPoint(PVT) ? FPOp : IntOp;
6874ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    if (NumElements == 1) {
6884ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman      setValue(&I, DAG.getNode(Opc, PVT, Op1, Op2));
689860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng    } else if (TVT != MVT::Other &&
690860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng               TLI.isTypeLegal(TVT) && TLI.isOperationLegal(Opc, TVT)) {
691f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman      setValue(&I, DAG.getNode(Opc, TVT, Op1, Op2));
6924ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    } else {
6934ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman      SDOperand Num = DAG.getConstant(NumElements, MVT::i32);
6944ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman      SDOperand Typ = DAG.getValueType(PVT);
695860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng      setValue(&I, DAG.getNode(VecOp, MVT::Vector, Num, Typ, Op1, Op2));
6964ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    }
6975fbb5d2459a5410590f285250faa604576308a93Nate Begeman  }
698e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman}
6992c49f2795514a7c56b680ba0310d7eb0a8a43289Chris Lattner
700e21ea61588996609f827213a2701a204f2f13fb3Nate Begemanvoid SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
701e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman  SDOperand Op1 = getValue(I.getOperand(0));
702e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman  SDOperand Op2 = getValue(I.getOperand(1));
703e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman
704e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman  Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
705e21ea61588996609f827213a2701a204f2f13fb3Nate Begeman
7061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
7071c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
7081c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
7091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode,
7101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                                      ISD::CondCode UnsignedOpcode) {
7111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Op1 = getValue(I.getOperand(0));
7121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Op2 = getValue(I.getOperand(1));
7131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  ISD::CondCode Opcode = SignedOpcode;
7141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (I.getOperand(0)->getType()->isUnsigned())
7151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    Opcode = UnsignedOpcode;
7167cf7e3f33f25544d08492d47cc8a1cbba25dc8d7Chris Lattner  setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
7171c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
7181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
7191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitSelect(User &I) {
7201c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Cond     = getValue(I.getOperand(0));
7211c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand TrueVal  = getValue(I.getOperand(1));
7221c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand FalseVal = getValue(I.getOperand(2));
7231c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
7241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                           TrueVal, FalseVal));
7251c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
7261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
7271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitCast(User &I) {
7281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand N = getValue(I.getOperand(0));
7291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType());
7301c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MVT::ValueType DestTy = TLI.getValueType(I.getType());
7311c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
7321c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (N.getValueType() == DestTy) {
7331c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    setValue(&I, N);  // noop cast.
734ef311aa7cf26ae0cbb6e784d767801b9058dd24bChris Lattner  } else if (DestTy == MVT::i1) {
735ef311aa7cf26ae0cbb6e784d767801b9058dd24bChris Lattner    // Cast to bool is a comparison against zero, not truncation to zero.
736ef311aa7cf26ae0cbb6e784d767801b9058dd24bChris Lattner    SDOperand Zero = isInteger(SrcTy) ? DAG.getConstant(0, N.getValueType()) :
737ef311aa7cf26ae0cbb6e784d767801b9058dd24bChris Lattner                                       DAG.getConstantFP(0.0, N.getValueType());
7387cf7e3f33f25544d08492d47cc8a1cbba25dc8d7Chris Lattner    setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE));
739ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner  } else if (isInteger(SrcTy)) {
740ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    if (isInteger(DestTy)) {        // Int -> Int cast
741ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      if (DestTy < SrcTy)   // Truncating cast?
742ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N));
743ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      else if (I.getOperand(0)->getType()->isSigned())
744ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N));
745ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      else
746ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N));
747ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    } else {                        // Int -> FP cast
748ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      if (I.getOperand(0)->getType()->isSigned())
749ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N));
750ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      else
751ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N));
752ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    }
7531c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  } else {
754ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    assert(isFloatingPoint(SrcTy) && "Unknown value type!");
755ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    if (isFloatingPoint(DestTy)) {  // FP -> FP cast
756ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      if (DestTy < SrcTy)   // Rounding cast?
757ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N));
758ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      else
759ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N));
760ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    } else {                        // FP -> Int cast.
761ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      if (I.getType()->isSigned())
762ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N));
763ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner      else
764ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner        setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N));
765ae0aacb8331e1227abea6601e531a10d0e65fdcaChris Lattner    }
7661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
7671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
7681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
7691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitGetElementPtr(User &I) {
7701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand N = getValue(I.getOperand(0));
7711c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  const Type *Ty = I.getOperand(0)->getType();
7721c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  const Type *UIntPtrTy = TD.getIntPtrType();
7731c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
7741c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
7751c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner       OI != E; ++OI) {
7761c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    Value *Idx = *OI;
777c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
7781c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      unsigned Field = cast<ConstantUInt>(Idx)->getValue();
7791c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      if (Field) {
7801c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        // N = N + Offset
7811c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field];
7821c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        N = DAG.getNode(ISD::ADD, N.getValueType(), N,
783dedf2bd5a34dac25e4245f58bb902ced6b64edd9Misha Brukman                        getIntPtrConstant(Offset));
7841c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      }
7851c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      Ty = StTy->getElementType(Field);
7861c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    } else {
7871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      Ty = cast<SequentialType>(Ty)->getElementType();
7887c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner
7897c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      // If this is a constant subscript, handle it quickly.
7907c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
7917c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        if (CI->getRawValue() == 0) continue;
7927c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner
7937c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        uint64_t Offs;
7947c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
7957c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner          Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
7967c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        else
7977c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner          Offs = TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue();
7987c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs));
7997c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        continue;
8007c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      }
8017c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner
8027c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      // N = N + Idx * ElementSize;
8037c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      uint64_t ElementSize = TD.getTypeSize(Ty);
8047c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      SDOperand IdxN = getValue(Idx);
8057c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner
8067c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      // If the index is smaller or larger than intptr_t, truncate or extend
8077c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      // it.
8087c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      if (IdxN.getValueType() < N.getValueType()) {
8097c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        if (Idx->getType()->isSigned())
8107c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner          IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
8117c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        else
8127c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner          IdxN = DAG.getNode(ISD::ZERO_EXTEND, N.getValueType(), IdxN);
8137c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      } else if (IdxN.getValueType() > N.getValueType())
8147c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
8157c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner
8167c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      // If this is a multiply by a power of two, turn it into a shl
8177c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      // immediately.  This is a very common case.
8187c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      if (isPowerOf2_64(ElementSize)) {
8197c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        unsigned Amt = Log2_64(ElementSize);
8207c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
8216b2d69655ace2788b244c8a4ebcfb6f2a926ad92Chris Lattner                           DAG.getConstant(Amt, TLI.getShiftAmountTy()));
8221c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
8237c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner        continue;
8241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      }
8257c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner
8267c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      SDOperand Scale = getIntPtrConstant(ElementSize);
8277c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
8287c0104b525a4ba8b5268ee5455f92b011f7cc263Chris Lattner      N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
8291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
8301c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
8311c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  setValue(&I, N);
8321c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
8331c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
8341c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitAlloca(AllocaInst &I) {
8351c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // If this is a fixed sized alloca in the entry block of the function,
8361c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // allocate it statically on the stack.
8371c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (FuncInfo.StaticAllocaMap.count(&I))
8381c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    return;   // getValue will auto-populate this.
8391c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
8401c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  const Type *Ty = I.getAllocatedType();
8411c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
842ae232e7a1055033436370c0b3aecf054fa44d5e7Nate Begeman  unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
843ae232e7a1055033436370c0b3aecf054fa44d5e7Nate Begeman                            I.getAlignment());
8441c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
8451c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand AllocSize = getValue(I.getArraySize());
84668cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner  MVT::ValueType IntPtr = TLI.getPointerTy();
84768cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner  if (IntPtr < AllocSize.getValueType())
84868cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner    AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
84968cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner  else if (IntPtr > AllocSize.getValueType())
85068cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner    AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
8511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
85268cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner  AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
8531c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                          getIntPtrConstant(TySize));
8541c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
8551c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Handle alignment.  If the requested alignment is less than or equal to the
8561c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // stack alignment, ignore it and round the size of the allocation up to the
8571c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // stack alignment size.  If the size is greater than the stack alignment, we
8581c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // note this in the DYNAMIC_STACKALLOC node.
8591c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  unsigned StackAlign =
8601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
8611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (Align <= StackAlign) {
8621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    Align = 0;
8631c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // Add SA-1 to the size.
8641c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
8651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                            getIntPtrConstant(StackAlign-1));
8661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // Mask out the low bits for alignment purposes.
8671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
8681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                            getIntPtrConstant(~(uint64_t)(StackAlign-1)));
8691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
8701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
871adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  std::vector<MVT::ValueType> VTs;
872adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  VTs.push_back(AllocSize.getValueType());
873adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  VTs.push_back(MVT::Other);
874adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  std::vector<SDOperand> Ops;
875adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  Ops.push_back(getRoot());
876adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  Ops.push_back(AllocSize);
877adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  Ops.push_back(getIntPtrConstant(Align));
878adf6c2a0cb638e8b211200b57b927d16f6e1cfc4Chris Lattner  SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
8791c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DAG.setRoot(setValue(&I, DSA).getValue(1));
8801c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
8811c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Inform the Frame Information that we have just allocated a variable-sized
8821c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // object.
8831c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
8841c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
8851c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
8861c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitLoad(LoadInst &I) {
8871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Ptr = getValue(I.getOperand(0));
888edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
889d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  SDOperand Root;
890d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  if (I.isVolatile())
891d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    Root = getRoot();
892d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  else {
893d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    // Do not serialize non-volatile loads against each other.
894d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    Root = DAG.getRoot();
895d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  }
8965fbb5d2459a5410590f285250faa604576308a93Nate Begeman
8975fbb5d2459a5410590f285250faa604576308a93Nate Begeman  const Type *Ty = I.getType();
8985fbb5d2459a5410590f285250faa604576308a93Nate Begeman  SDOperand L;
8995fbb5d2459a5410590f285250faa604576308a93Nate Begeman
9008cfa57b1b4eade4e0101195b2f94ab288cd03563Nate Begeman  if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
9014ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    unsigned NumElements = PTy->getNumElements();
9024ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
903f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman    MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements);
9044ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman
9054ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    // Immediately scalarize packed types containing only one element, so that
9064ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    // the Legalize pass does not have to deal with them.
9074ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    if (NumElements == 1) {
9084ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman      L = DAG.getLoad(PVT, Root, Ptr, DAG.getSrcValue(I.getOperand(0)));
909860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng    } else if (TVT != MVT::Other &&
910860771d2d86243b65ec16fac6cc57b285078f138Evan Cheng               TLI.isTypeLegal(TVT) && TLI.isOperationLegal(ISD::LOAD, TVT)) {
911f43a3ca26d7bf431be5cdfb5963350a158e840afNate Begeman      L = DAG.getLoad(TVT, Root, Ptr, DAG.getSrcValue(I.getOperand(0)));
9124ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    } else {
9134ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman      L = DAG.getVecLoad(NumElements, PVT, Root, Ptr,
9144ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman                         DAG.getSrcValue(I.getOperand(0)));
9154ef3b817fee7ea5be7219e00ab8e15976bfe279fNate Begeman    }
9165fbb5d2459a5410590f285250faa604576308a93Nate Begeman  } else {
9175fbb5d2459a5410590f285250faa604576308a93Nate Begeman    L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr,
9185fbb5d2459a5410590f285250faa604576308a93Nate Begeman                    DAG.getSrcValue(I.getOperand(0)));
9195fbb5d2459a5410590f285250faa604576308a93Nate Begeman  }
920d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  setValue(&I, L);
921d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner
922d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  if (I.isVolatile())
923d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    DAG.setRoot(L.getValue(1));
924d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner  else
925d3948116b81b11e82246c11389a9b4ce7e619fbbChris Lattner    PendingLoads.push_back(L.getValue(1));
9261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
9271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
9281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
9291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitStore(StoreInst &I) {
9301c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  Value *SrcV = I.getOperand(0);
9311c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Src = getValue(SrcV);
9321c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Ptr = getValue(I.getOperand(1));
933369e6db9b6033bb60519b0ad84afeacc9ec9b416Chris Lattner  DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr,
93406ef88472f7bac3512e224974e87a0a456471b82Andrew Lenharth                          DAG.getSrcValue(I.getOperand(1))));
9351c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
9361c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
937c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner/// visitIntrinsicCall - Lower the call to the specified intrinsic function.  If
938c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner/// we want to emit this as a call to a named external function, return the name
939c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner/// otherwise lower it and return null.
940c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattnerconst char *
941c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris LattnerSelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
942c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  switch (Intrinsic) {
943c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::vastart:  visitVAStart(I); return 0;
944c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::vaend:    visitVAEnd(I); return 0;
945c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::vacopy:   visitVACopy(I); return 0;
946c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return 0;
947c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::frameaddress:  visitFrameReturnAddress(I, true); return 0;
948c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::setjmp:
949c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return "_setjmp"+!TLI.usesUnderscoreSetJmpLongJmp();
950c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    break;
951c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::longjmp:
952c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return "_longjmp"+!TLI.usesUnderscoreSetJmpLongJmp();
953c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    break;
95403dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner  case Intrinsic::memcpy_i32:
95503dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner  case Intrinsic::memcpy_i64:
95603dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner    visitMemIntrinsic(I, ISD::MEMCPY);
95703dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner    return 0;
95803dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner  case Intrinsic::memset_i32:
95903dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner  case Intrinsic::memset_i64:
96003dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner    visitMemIntrinsic(I, ISD::MEMSET);
96103dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner    return 0;
96203dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner  case Intrinsic::memmove_i32:
96303dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner  case Intrinsic::memmove_i64:
96403dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner    visitMemIntrinsic(I, ISD::MEMMOVE);
96503dd4652158f8a4c1db65f066195342d4a3695a7Chris Lattner    return 0;
966c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner
96786cb643801be1308ba1da7db774b64852a119e94Chris Lattner  case Intrinsic::dbg_stoppoint: {
968b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions())
969b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      return "llvm_debugger_stop";
97036ce69195ed488034d0bb11180cc2ebd923679c8Chris Lattner
971ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey    MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
972ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey    if (DebugInfo &&  DebugInfo->Verify(I.getOperand(4))) {
973ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      std::vector<SDOperand> Ops;
97436ce69195ed488034d0bb11180cc2ebd923679c8Chris Lattner
975ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      // Input Chain
976ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      Ops.push_back(getRoot());
977ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey
978ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      // line number
979ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      Ops.push_back(getValue(I.getOperand(2)));
980ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey
981ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      // column
982ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      Ops.push_back(getValue(I.getOperand(3)));
983ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey
984d96185aa62dacf5c75d889231a8ab2e0f1ceb073Jim Laskey      DebugInfoDesc *DD = DebugInfo->getDescFor(I.getOperand(4));
985ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      assert(DD && "Not a debug information descriptor");
986ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      CompileUnitDesc *CompileUnit = dyn_cast<CompileUnitDesc>(DD);
987ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      assert(CompileUnit && "Not a compile unit");
988ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      Ops.push_back(DAG.getString(CompileUnit->getFileName()));
989ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      Ops.push_back(DAG.getString(CompileUnit->getDirectory()));
990ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey
991ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey      if (Ops.size() == 5)  // Found filename/workingdir.
992ce72b1755f5993a42c2e04e32a93fa5228a285d4Jim Laskey        DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops));
99386cb643801be1308ba1da7db774b64852a119e94Chris Lattner    }
99486cb643801be1308ba1da7db774b64852a119e94Chris Lattner
995d67b3a8bf75d4d3c50263960a9375e6856588f2cChris Lattner    setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType())));
996b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    return 0;
99736ce69195ed488034d0bb11180cc2ebd923679c8Chris Lattner  }
998c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::dbg_region_start:
999b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions())
1000b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      return "llvm_dbg_region_start";
1001b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (I.getType() != Type::VoidTy)
1002b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType())));
1003b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    return 0;
1004c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::dbg_region_end:
1005b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions())
1006b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      return "llvm_dbg_region_end";
1007b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (I.getType() != Type::VoidTy)
1008b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType())));
1009b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    return 0;
1010c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::dbg_func_start:
1011b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions())
1012b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      return "llvm_dbg_subprogram";
1013b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    if (I.getType() != Type::VoidTy)
1014b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner      setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType())));
1015b1a5a5c4c0182205b91b962def7b008228a1f7e6Chris Lattner    return 0;
1016c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner
10170b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::isunordered_f32:
10180b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::isunordered_f64:
1019c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)),
1020c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                              getValue(I.getOperand(2)), ISD::SETUO));
1021c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
1022c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner
10230b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::sqrt_f32:
10240b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::sqrt_f64:
1025c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    setValue(&I, DAG.getNode(ISD::FSQRT,
1026c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1)).getValueType(),
1027c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1))));
1028c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
1029c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  case Intrinsic::pcmarker: {
1030c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    SDOperand Tmp = getValue(I.getOperand(1));
1031c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
1032c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
1033c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  }
10348b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth  case Intrinsic::readcyclecounter: {
10358b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    std::vector<MVT::ValueType> VTs;
10368b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    VTs.push_back(MVT::i64);
10378b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    VTs.push_back(MVT::Other);
10388b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    std::vector<SDOperand> Ops;
10398b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    Ops.push_back(getRoot());
10408b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, VTs, Ops);
10418b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    setValue(&I, Tmp);
10428b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth    DAG.setRoot(Tmp.getValue(1));
104351b8d54922350b7e1c2cd5a5183ef2c5f5d1b1d5Andrew Lenharth    return 0;
10448b91c77385a055474d271aa8c10f0382fdeaafebAndrew Lenharth  }
1045d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman  case Intrinsic::bswap_i16:
1046d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman  case Intrinsic::bswap_i32:
1047d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman  case Intrinsic::bswap_i64:
1048d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman    setValue(&I, DAG.getNode(ISD::BSWAP,
1049d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman                             getValue(I.getOperand(1)).getValueType(),
1050d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman                             getValue(I.getOperand(1))));
1051d88fc03602947b5baa35c8b09fe8bcfa2b4a03c1Nate Begeman    return 0;
10520b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::cttz_i8:
10530b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::cttz_i16:
10540b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::cttz_i32:
10550b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::cttz_i64:
1056c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    setValue(&I, DAG.getNode(ISD::CTTZ,
1057c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1)).getValueType(),
1058c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1))));
1059c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
10600b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctlz_i8:
10610b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctlz_i16:
10620b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctlz_i32:
10630b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctlz_i64:
1064c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    setValue(&I, DAG.getNode(ISD::CTLZ,
1065c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1)).getValueType(),
1066c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1))));
1067c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
10680b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctpop_i8:
10690b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctpop_i16:
10700b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctpop_i32:
10710b118206bf3411722707f2e5cab8fd2eedcd50d6Reid Spencer  case Intrinsic::ctpop_i64:
1072c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    setValue(&I, DAG.getNode(ISD::CTPOP,
1073c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1)).getValueType(),
1074c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner                             getValue(I.getOperand(1))));
1075c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
1076140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner  case Intrinsic::stacksave: {
1077140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    std::vector<MVT::ValueType> VTs;
1078140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    VTs.push_back(TLI.getPointerTy());
1079140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    VTs.push_back(MVT::Other);
1080140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    std::vector<SDOperand> Ops;
1081140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    Ops.push_back(getRoot());
1082140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1083140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    setValue(&I, Tmp);
1084140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    DAG.setRoot(Tmp.getValue(1));
1085140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    return 0;
1086140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner  }
108739a17dd31ddc4af6067940cb31e2c7d380773478Chris Lattner  case Intrinsic::stackrestore: {
108839a17dd31ddc4af6067940cb31e2c7d380773478Chris Lattner    SDOperand Tmp = getValue(I.getOperand(1));
108939a17dd31ddc4af6067940cb31e2c7d380773478Chris Lattner    DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
1090140d53c99c3a70b9d3858a3c87f8ecb098994748Chris Lattner    return 0;
109139a17dd31ddc4af6067940cb31e2c7d380773478Chris Lattner  }
1092ac22c83e6853c759a10eb7310b019b22e1d42d16Chris Lattner  case Intrinsic::prefetch:
1093ac22c83e6853c759a10eb7310b019b22e1d42d16Chris Lattner    // FIXME: Currently discarding prefetches.
1094ac22c83e6853c759a10eb7310b019b22e1d42d16Chris Lattner    return 0;
1095c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  default:
1096c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    std::cerr << I;
1097c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    assert(0 && "This intrinsic is not implemented yet!");
1098c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner    return 0;
1099c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  }
1100c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner}
1101c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner
1102c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner
11031c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitCall(CallInst &I) {
110464e14b1679fa3649b286402ea254d663ac43ef91Chris Lattner  const char *RenameFn = 0;
1105c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  if (Function *F = I.getCalledFunction()) {
1106c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner    if (F->isExternal())
1107c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner      if (unsigned IID = F->getIntrinsicID()) {
1108c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner        RenameFn = visitIntrinsicCall(I, IID);
1109c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner        if (!RenameFn)
1110c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner          return;
1111c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner      } else {    // Not an LLVM intrinsic.
1112c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner        const std::string &Name = F->getName();
1113a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner        if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) {
1114a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner          if (I.getNumOperands() == 3 &&   // Basic sanity checks.
1115a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner              I.getOperand(1)->getType()->isFloatingPoint() &&
1116a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner              I.getType() == I.getOperand(1)->getType() &&
1117a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner              I.getType() == I.getOperand(2)->getType()) {
1118a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner            SDOperand LHS = getValue(I.getOperand(1));
1119a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner            SDOperand RHS = getValue(I.getOperand(2));
1120a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner            setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
1121a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner                                     LHS, RHS));
1122a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner            return;
1123a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner          }
1124a09f848c11c9db3c2614e0275a3256310ac26653Chris Lattner        } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) {
1125c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner          if (I.getNumOperands() == 2 &&   // Basic sanity checks.
1126c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner              I.getOperand(1)->getType()->isFloatingPoint() &&
1127c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner              I.getType() == I.getOperand(1)->getType()) {
1128c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner            SDOperand Tmp = getValue(I.getOperand(1));
1129c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner            setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
1130c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner            return;
1131c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner          }
1132c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner        } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) {
1133f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner          if (I.getNumOperands() == 2 &&   // Basic sanity checks.
1134f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner              I.getOperand(1)->getType()->isFloatingPoint() &&
113506a248c9b398049d41cf6dd1a3f9eecc75603401Chris Lattner              I.getType() == I.getOperand(1)->getType()) {
1136c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner            SDOperand Tmp = getValue(I.getOperand(1));
1137f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner            setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
1138f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner            return;
1139f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner          }
1140c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner        } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) {
1141f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner          if (I.getNumOperands() == 2 &&   // Basic sanity checks.
1142f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner              I.getOperand(1)->getType()->isFloatingPoint() &&
114306a248c9b398049d41cf6dd1a3f9eecc75603401Chris Lattner              I.getType() == I.getOperand(1)->getType()) {
1144c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner            SDOperand Tmp = getValue(I.getOperand(1));
1145f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner            setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
1146f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner            return;
1147f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner          }
1148f76e7dc8d8ac1855ef59698e82c757548ef4ca65Chris Lattner        }
1149c0f18152d94bf65061fab4b80869998cfb0439e1Chris Lattner      }
1150ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  } else if (isa<InlineAsm>(I.getOperand(0))) {
1151ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner    visitInlineAsm(I);
1152ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner    return;
1153c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  }
1154edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
115564e14b1679fa3649b286402ea254d663ac43ef91Chris Lattner  SDOperand Callee;
115664e14b1679fa3649b286402ea254d663ac43ef91Chris Lattner  if (!RenameFn)
115764e14b1679fa3649b286402ea254d663ac43ef91Chris Lattner    Callee = getValue(I.getOperand(0));
115864e14b1679fa3649b286402ea254d663ac43ef91Chris Lattner  else
115964e14b1679fa3649b286402ea254d663ac43ef91Chris Lattner    Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
11601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  std::vector<std::pair<SDOperand, const Type*> > Args;
1161c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner  Args.reserve(I.getNumOperands());
11621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
11631c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    Value *Arg = I.getOperand(i);
11641c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SDOperand ArgNode = getValue(Arg);
11651c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    Args.push_back(std::make_pair(ArgNode, Arg->getType()));
11661c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
1167edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
11688e21e71b248365c69c0f666518c378b5819ce6fbNate Begeman  const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType());
11698e21e71b248365c69c0f666518c378b5819ce6fbNate Begeman  const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1170edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
1171cf5734dddd66af9388a171b44996505ede47feedChris Lattner  std::pair<SDOperand,SDOperand> Result =
11729092fa310c8b2b1645b0d448c4c34b1e8ddc131dChris Lattner    TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(),
1173adf6a965a321372c640845407195594835921eb4Chris Lattner                    I.isTailCall(), Callee, Args, DAG);
11741c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  if (I.getType() != Type::VoidTy)
1175cf5734dddd66af9388a171b44996505ede47feedChris Lattner    setValue(&I, Result.first);
1176cf5734dddd66af9388a171b44996505ede47feedChris Lattner  DAG.setRoot(Result.second);
11771c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
11781c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1179864635ad7b3046d3042311423071152c613961deChris LattnerSDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
11809f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner                                        SDOperand &Chain, SDOperand &Flag)const{
1181864635ad7b3046d3042311423071152c613961deChris Lattner  SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag);
1182864635ad7b3046d3042311423071152c613961deChris Lattner  Chain = Val.getValue(1);
1183864635ad7b3046d3042311423071152c613961deChris Lattner  Flag  = Val.getValue(2);
1184864635ad7b3046d3042311423071152c613961deChris Lattner
1185864635ad7b3046d3042311423071152c613961deChris Lattner  // If the result was expanded, copy from the top part.
1186864635ad7b3046d3042311423071152c613961deChris Lattner  if (Regs.size() > 1) {
1187864635ad7b3046d3042311423071152c613961deChris Lattner    assert(Regs.size() == 2 &&
1188864635ad7b3046d3042311423071152c613961deChris Lattner           "Cannot expand to more than 2 elts yet!");
1189864635ad7b3046d3042311423071152c613961deChris Lattner    SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag);
1190864635ad7b3046d3042311423071152c613961deChris Lattner    Chain = Val.getValue(1);
1191864635ad7b3046d3042311423071152c613961deChris Lattner    Flag  = Val.getValue(2);
11929f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner    if (DAG.getTargetLoweringInfo().isLittleEndian())
11939f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner      return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi);
11949f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner    else
11959f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner      return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val);
1196864635ad7b3046d3042311423071152c613961deChris Lattner  }
1197864635ad7b3046d3042311423071152c613961deChris Lattner
1198864635ad7b3046d3042311423071152c613961deChris Lattner  // Otherwise, if the return value was promoted, truncate it to the
1199864635ad7b3046d3042311423071152c613961deChris Lattner  // appropriate type.
1200864635ad7b3046d3042311423071152c613961deChris Lattner  if (RegVT == ValueVT)
1201864635ad7b3046d3042311423071152c613961deChris Lattner    return Val;
1202864635ad7b3046d3042311423071152c613961deChris Lattner
1203864635ad7b3046d3042311423071152c613961deChris Lattner  if (MVT::isInteger(RegVT))
1204864635ad7b3046d3042311423071152c613961deChris Lattner    return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
1205864635ad7b3046d3042311423071152c613961deChris Lattner  else
1206864635ad7b3046d3042311423071152c613961deChris Lattner    return DAG.getNode(ISD::FP_ROUND, ValueVT, Val);
1207864635ad7b3046d3042311423071152c613961deChris Lattner}
1208864635ad7b3046d3042311423071152c613961deChris Lattner
1209c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
1210c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner/// specified value into the registers specified by this object.  This uses
1211c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner/// Chain/Flag as the input and updates them for the output Chain/Flag.
1212c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattnervoid RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
12139f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner                                 SDOperand &Chain, SDOperand &Flag) const {
1214c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner  if (Regs.size() == 1) {
1215c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    // If there is a single register and the types differ, this must be
1216c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    // a promotion.
1217c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    if (RegVT != ValueVT) {
1218c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      if (MVT::isInteger(RegVT))
1219c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val);
1220c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      else
1221c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val);
1222c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    }
1223c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag);
1224c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    Flag = Chain.getValue(1);
1225c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner  } else {
12269f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner    std::vector<unsigned> R(Regs);
12279f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner    if (!DAG.getTargetLoweringInfo().isLittleEndian())
12289f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner      std::reverse(R.begin(), R.end());
12299f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner
12309f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner    for (unsigned i = 0, e = R.size(); i != e; ++i) {
1231c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val,
1232c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner                                   DAG.getConstant(i, MVT::i32));
12339f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner      Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag);
1234c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      Flag = Chain.getValue(1);
1235c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    }
1236c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner  }
1237c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner}
1238864635ad7b3046d3042311423071152c613961deChris Lattner
1239c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner/// AddInlineAsmOperands - Add this value to the specified inlineasm node
1240c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner/// operand list.  This adds the code marker and includes the number of
1241c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner/// values added into it.
1242c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattnervoid RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
12439f6637db10642fae51fa3628b7833c6999f7fdb3Chris Lattner                                        std::vector<SDOperand> &Ops) const {
1244c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner  Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32));
1245c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner  for (unsigned i = 0, e = Regs.size(); i != e; ++i)
1246c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    Ops.push_back(DAG.getRegister(Regs[i], RegVT));
1247c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner}
1248864635ad7b3046d3042311423071152c613961deChris Lattner
1249864635ad7b3046d3042311423071152c613961deChris Lattner/// isAllocatableRegister - If the specified register is safe to allocate,
1250864635ad7b3046d3042311423071152c613961deChris Lattner/// i.e. it isn't a stack pointer or some other special register, return the
1251864635ad7b3046d3042311423071152c613961deChris Lattner/// register class for the register.  Otherwise, return null.
1252864635ad7b3046d3042311423071152c613961deChris Lattnerstatic const TargetRegisterClass *
12539b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris LattnerisAllocatableRegister(unsigned Reg, MachineFunction &MF,
12549b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner                      const TargetLowering &TLI, const MRegisterInfo *MRI) {
12559b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner  for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(),
12569b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner       E = MRI->regclass_end(); RCI != E; ++RCI) {
12579b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    const TargetRegisterClass *RC = *RCI;
12589b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    // If none of the the value types for this register class are valid, we
12599b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    // can't use it.  For example, 64-bit reg classes on 32-bit targets.
12609b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    bool isLegal = false;
12619b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
12629b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner         I != E; ++I) {
12639b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner      if (TLI.isTypeLegal(*I)) {
12649b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner        isLegal = true;
12659b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner        break;
12669b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner      }
12679b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    }
12689b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner
12699b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    if (!isLegal) continue;
12709b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner
1271864635ad7b3046d3042311423071152c613961deChris Lattner    // NOTE: This isn't ideal.  In particular, this might allocate the
1272864635ad7b3046d3042311423071152c613961deChris Lattner    // frame pointer in functions that need it (due to them not being taken
1273864635ad7b3046d3042311423071152c613961deChris Lattner    // out of allocation, because a variable sized allocation hasn't been seen
1274864635ad7b3046d3042311423071152c613961deChris Lattner    // yet).  This is a slight code pessimization, but should still work.
12759b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
12769b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner         E = RC->allocation_order_end(MF); I != E; ++I)
1277864635ad7b3046d3042311423071152c613961deChris Lattner      if (*I == Reg)
12789b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner        return RC;
1279864635ad7b3046d3042311423071152c613961deChris Lattner  }
1280864635ad7b3046d3042311423071152c613961deChris Lattner  return 0;
1281864635ad7b3046d3042311423071152c613961deChris Lattner}
1282864635ad7b3046d3042311423071152c613961deChris Lattner
1283864635ad7b3046d3042311423071152c613961deChris LattnerRegsForValue SelectionDAGLowering::
1284864635ad7b3046d3042311423071152c613961deChris LattnerGetRegistersForValue(const std::string &ConstrCode,
1285864635ad7b3046d3042311423071152c613961deChris Lattner                     MVT::ValueType VT, bool isOutReg, bool isInReg,
1286864635ad7b3046d3042311423071152c613961deChris Lattner                     std::set<unsigned> &OutputRegs,
12874e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner                     std::set<unsigned> &InputRegs) {
1288864635ad7b3046d3042311423071152c613961deChris Lattner  std::pair<unsigned, const TargetRegisterClass*> PhysReg =
1289864635ad7b3046d3042311423071152c613961deChris Lattner    TLI.getRegForInlineAsmConstraint(ConstrCode, VT);
1290864635ad7b3046d3042311423071152c613961deChris Lattner  std::vector<unsigned> Regs;
1291864635ad7b3046d3042311423071152c613961deChris Lattner
1292864635ad7b3046d3042311423071152c613961deChris Lattner  unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1;
1293864635ad7b3046d3042311423071152c613961deChris Lattner  MVT::ValueType RegVT;
1294864635ad7b3046d3042311423071152c613961deChris Lattner  MVT::ValueType ValueVT = VT;
1295864635ad7b3046d3042311423071152c613961deChris Lattner
1296864635ad7b3046d3042311423071152c613961deChris Lattner  if (PhysReg.first) {
1297864635ad7b3046d3042311423071152c613961deChris Lattner    if (VT == MVT::Other)
1298864635ad7b3046d3042311423071152c613961deChris Lattner      ValueVT = *PhysReg.second->vt_begin();
1299864635ad7b3046d3042311423071152c613961deChris Lattner    RegVT = VT;
1300864635ad7b3046d3042311423071152c613961deChris Lattner
1301864635ad7b3046d3042311423071152c613961deChris Lattner    // This is a explicit reference to a physical register.
1302864635ad7b3046d3042311423071152c613961deChris Lattner    Regs.push_back(PhysReg.first);
1303864635ad7b3046d3042311423071152c613961deChris Lattner
1304864635ad7b3046d3042311423071152c613961deChris Lattner    // If this is an expanded reference, add the rest of the regs to Regs.
1305864635ad7b3046d3042311423071152c613961deChris Lattner    if (NumRegs != 1) {
1306864635ad7b3046d3042311423071152c613961deChris Lattner      RegVT = *PhysReg.second->vt_begin();
1307864635ad7b3046d3042311423071152c613961deChris Lattner      TargetRegisterClass::iterator I = PhysReg.second->begin();
1308864635ad7b3046d3042311423071152c613961deChris Lattner      TargetRegisterClass::iterator E = PhysReg.second->end();
1309864635ad7b3046d3042311423071152c613961deChris Lattner      for (; *I != PhysReg.first; ++I)
1310864635ad7b3046d3042311423071152c613961deChris Lattner        assert(I != E && "Didn't find reg!");
1311864635ad7b3046d3042311423071152c613961deChris Lattner
1312864635ad7b3046d3042311423071152c613961deChris Lattner      // Already added the first reg.
1313864635ad7b3046d3042311423071152c613961deChris Lattner      --NumRegs; ++I;
1314864635ad7b3046d3042311423071152c613961deChris Lattner      for (; NumRegs; --NumRegs, ++I) {
1315864635ad7b3046d3042311423071152c613961deChris Lattner        assert(I != E && "Ran out of registers to allocate!");
1316864635ad7b3046d3042311423071152c613961deChris Lattner        Regs.push_back(*I);
1317864635ad7b3046d3042311423071152c613961deChris Lattner      }
1318864635ad7b3046d3042311423071152c613961deChris Lattner    }
1319864635ad7b3046d3042311423071152c613961deChris Lattner    return RegsForValue(Regs, RegVT, ValueVT);
1320864635ad7b3046d3042311423071152c613961deChris Lattner  }
1321864635ad7b3046d3042311423071152c613961deChris Lattner
1322864635ad7b3046d3042311423071152c613961deChris Lattner  // This is a reference to a register class.  Allocate NumRegs consecutive,
1323864635ad7b3046d3042311423071152c613961deChris Lattner  // available, registers from the class.
1324864635ad7b3046d3042311423071152c613961deChris Lattner  std::vector<unsigned> RegClassRegs =
1325864635ad7b3046d3042311423071152c613961deChris Lattner    TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT);
1326864635ad7b3046d3042311423071152c613961deChris Lattner
13274e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo();
13284e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  MachineFunction &MF = *CurMBB->getParent();
1329864635ad7b3046d3042311423071152c613961deChris Lattner  unsigned NumAllocated = 0;
1330864635ad7b3046d3042311423071152c613961deChris Lattner  for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
1331864635ad7b3046d3042311423071152c613961deChris Lattner    unsigned Reg = RegClassRegs[i];
13324e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    // See if this register is available.
1333864635ad7b3046d3042311423071152c613961deChris Lattner    if ((isOutReg && OutputRegs.count(Reg)) ||   // Already used.
1334864635ad7b3046d3042311423071152c613961deChris Lattner        (isInReg  && InputRegs.count(Reg))) {    // Already used.
1335864635ad7b3046d3042311423071152c613961deChris Lattner      // Make sure we find consecutive registers.
1336864635ad7b3046d3042311423071152c613961deChris Lattner      NumAllocated = 0;
1337864635ad7b3046d3042311423071152c613961deChris Lattner      continue;
1338864635ad7b3046d3042311423071152c613961deChris Lattner    }
1339864635ad7b3046d3042311423071152c613961deChris Lattner
13404e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    // Check to see if this register is allocatable (i.e. don't give out the
13414e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    // stack pointer).
13429b6fb5de49f30d03b3e3f2fcb99e777b3149b783Chris Lattner    const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI);
1343864635ad7b3046d3042311423071152c613961deChris Lattner    if (!RC) {
1344864635ad7b3046d3042311423071152c613961deChris Lattner      // Make sure we find consecutive registers.
1345864635ad7b3046d3042311423071152c613961deChris Lattner      NumAllocated = 0;
1346864635ad7b3046d3042311423071152c613961deChris Lattner      continue;
13474e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    }
13484e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner
1349864635ad7b3046d3042311423071152c613961deChris Lattner    // Okay, this register is good, we can use it.
1350864635ad7b3046d3042311423071152c613961deChris Lattner    ++NumAllocated;
1351864635ad7b3046d3042311423071152c613961deChris Lattner
1352864635ad7b3046d3042311423071152c613961deChris Lattner    // If we allocated enough consecutive
1353864635ad7b3046d3042311423071152c613961deChris Lattner    if (NumAllocated == NumRegs) {
1354864635ad7b3046d3042311423071152c613961deChris Lattner      unsigned RegStart = (i-NumAllocated)+1;
1355864635ad7b3046d3042311423071152c613961deChris Lattner      unsigned RegEnd   = i+1;
1356864635ad7b3046d3042311423071152c613961deChris Lattner      // Mark all of the allocated registers used.
1357864635ad7b3046d3042311423071152c613961deChris Lattner      for (unsigned i = RegStart; i != RegEnd; ++i) {
1358864635ad7b3046d3042311423071152c613961deChris Lattner        unsigned Reg = RegClassRegs[i];
1359864635ad7b3046d3042311423071152c613961deChris Lattner        Regs.push_back(Reg);
1360864635ad7b3046d3042311423071152c613961deChris Lattner        if (isOutReg) OutputRegs.insert(Reg);    // Mark reg used.
1361864635ad7b3046d3042311423071152c613961deChris Lattner        if (isInReg)  InputRegs.insert(Reg);     // Mark reg used.
1362864635ad7b3046d3042311423071152c613961deChris Lattner      }
1363864635ad7b3046d3042311423071152c613961deChris Lattner
1364864635ad7b3046d3042311423071152c613961deChris Lattner      return RegsForValue(Regs, *RC->vt_begin(), VT);
1365864635ad7b3046d3042311423071152c613961deChris Lattner    }
13664e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  }
1367864635ad7b3046d3042311423071152c613961deChris Lattner
1368864635ad7b3046d3042311423071152c613961deChris Lattner  // Otherwise, we couldn't allocate enough registers for this.
1369864635ad7b3046d3042311423071152c613961deChris Lattner  return RegsForValue();
13704e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner}
13714e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner
1372864635ad7b3046d3042311423071152c613961deChris Lattner
1373ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner/// visitInlineAsm - Handle a call to an InlineAsm object.
1374ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner///
1375ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattnervoid SelectionDAGLowering::visitInlineAsm(CallInst &I) {
1376ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  InlineAsm *IA = cast<InlineAsm>(I.getOperand(0));
1377ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1378ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
1379ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner                                                 MVT::Other);
1380ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1381ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  // Note, we treat inline asms both with and without side-effects as the same.
1382ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  // If an inline asm doesn't have side effects and doesn't access memory, we
1383ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  // could not choose to not chain it.
1384ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  bool hasSideEffects = IA->hasSideEffects();
1385ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
13862cc2f66c25d9576743026688fdae5ed402726532Chris Lattner  std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
13871efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  std::vector<MVT::ValueType> ConstraintVTs;
1388ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1389ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  /// AsmNodeOperands - A list of pairs.  The first element is a register, the
1390ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set
1391ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  /// if it is a def of that register.
1392ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  std::vector<SDOperand> AsmNodeOperands;
1393ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  AsmNodeOperands.push_back(SDOperand());  // reserve space for input chain
1394ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  AsmNodeOperands.push_back(AsmStr);
1395ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1396ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  SDOperand Chain = getRoot();
1397ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  SDOperand Flag;
1398ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
13994e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  // We fully assign registers here at isel time.  This is not optimal, but
14004e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  // should work.  For register classes that correspond to LLVM classes, we
14014e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  // could let the LLVM RA do its thing, but we currently don't.  Do a prepass
14024e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  // over the constraints, collecting fixed registers that we know we can't use.
14034e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  std::set<unsigned> OutputRegs, InputRegs;
14041efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  unsigned OpNum = 1;
14054e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
14064e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!");
14074e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    std::string &ConstraintCode = Constraints[i].Codes[0];
14082223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner
14091efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    MVT::ValueType OpVT;
14101efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
14111efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    // Compute the value type for each operand and add it to ConstraintVTs.
14121efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    switch (Constraints[i].Type) {
14131efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    case InlineAsm::isOutput:
14141efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      if (!Constraints[i].isIndirectOutput) {
14151efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        assert(I.getType() != Type::VoidTy && "Bad inline asm!");
14161efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        OpVT = TLI.getValueType(I.getType());
14171efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      } else {
141822873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        const Type *OpTy = I.getOperand(OpNum)->getType();
14191efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType());
14201efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner        OpNum++;  // Consumes a call operand.
14211efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      }
14221efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      break;
14231efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    case InlineAsm::isInput:
14241efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      OpVT = TLI.getValueType(I.getOperand(OpNum)->getType());
14251efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      OpNum++;  // Consumes a call operand.
14261efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      break;
14271efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    case InlineAsm::isClobber:
14281efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      OpVT = MVT::Other;
14291efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      break;
14301efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    }
14311efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
14321efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    ConstraintVTs.push_back(OpVT);
14331efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
1434864635ad7b3046d3042311423071152c613961deChris Lattner    if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0)
1435864635ad7b3046d3042311423071152c613961deChris Lattner      continue;  // Not assigned a fixed reg.
14361efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
1437864635ad7b3046d3042311423071152c613961deChris Lattner    // Build a list of regs that this operand uses.  This always has a single
1438864635ad7b3046d3042311423071152c613961deChris Lattner    // element for promoted/expanded operands.
1439864635ad7b3046d3042311423071152c613961deChris Lattner    RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT,
1440864635ad7b3046d3042311423071152c613961deChris Lattner                                             false, false,
1441864635ad7b3046d3042311423071152c613961deChris Lattner                                             OutputRegs, InputRegs);
14424e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner
14434e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    switch (Constraints[i].Type) {
14444e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    case InlineAsm::isOutput:
14454e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      // We can't assign any other output to this register.
1446864635ad7b3046d3042311423071152c613961deChris Lattner      OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
14474e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      // If this is an early-clobber output, it cannot be assigned to the same
14484e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      // value as the input reg.
14492223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner      if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
1450864635ad7b3046d3042311423071152c613961deChris Lattner        InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
14514e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      break;
14521efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner    case InlineAsm::isInput:
14531efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      // We can't assign any other input to this register.
1454864635ad7b3046d3042311423071152c613961deChris Lattner      InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
14551efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner      break;
14564e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    case InlineAsm::isClobber:
14574e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      // Clobbered regs cannot be used as inputs or outputs.
1458864635ad7b3046d3042311423071152c613961deChris Lattner      InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
1459864635ad7b3046d3042311423071152c613961deChris Lattner      OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
14604e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      break;
14614e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner    }
14624e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner  }
14632cc2f66c25d9576743026688fdae5ed402726532Chris Lattner
14640f0b7d4927e56e622cf43da0db56f3e0d40b8aafChris Lattner  // Loop over all of the inputs, copying the operand values into the
14650f0b7d4927e56e622cf43da0db56f3e0d40b8aafChris Lattner  // appropriate registers and processing the output regs.
1466864635ad7b3046d3042311423071152c613961deChris Lattner  RegsForValue RetValRegs;
1467864635ad7b3046d3042311423071152c613961deChris Lattner  std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
14681efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner  OpNum = 1;
14690f0b7d4927e56e622cf43da0db56f3e0d40b8aafChris Lattner
14706656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
14712cc2f66c25d9576743026688fdae5ed402726532Chris Lattner    assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!");
14722cc2f66c25d9576743026688fdae5ed402726532Chris Lattner    std::string &ConstraintCode = Constraints[i].Codes[0];
14731efa40f6a4b561cf8f80fe018684236010645cd0Chris Lattner
14742cc2f66c25d9576743026688fdae5ed402726532Chris Lattner    switch (Constraints[i].Type) {
14752cc2f66c25d9576743026688fdae5ed402726532Chris Lattner    case InlineAsm::isOutput: {
147622873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
147722873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      if (ConstraintCode.size() == 1)   // not a physreg name.
147822873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        CTy = TLI.getConstraintType(ConstraintCode[0]);
147922873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
148022873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      if (CTy == TargetLowering::C_Memory) {
148122873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        // Memory output.
148222873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        SDOperand InOperandVal = getValue(I.getOperand(OpNum));
148322873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
148422873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        // Check that the operand (the address to store to) isn't a float.
148522873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        if (!MVT::isInteger(InOperandVal.getValueType()))
148622873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner          assert(0 && "MATCH FAIL!");
148722873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
148822873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        if (!Constraints[i].isIndirectOutput)
148922873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner          assert(0 && "MATCH FAIL!");
149022873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
149122873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        OpNum++;  // Consumes a call operand.
149222873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
149322873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        // Extend/truncate to the right pointer type if needed.
149422873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        MVT::ValueType PtrType = TLI.getPointerTy();
149522873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        if (InOperandVal.getValueType() < PtrType)
149622873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner          InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
149722873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        else if (InOperandVal.getValueType() > PtrType)
149822873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner          InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
149922873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
150022873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        // Add information to the INLINEASM node to know about this output.
150122873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        unsigned ResOpType = 4/*MEM*/ | (1 << 3);
150222873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
150322873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        AsmNodeOperands.push_back(InOperandVal);
150422873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        break;
150522873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      }
150622873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
150722873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      // Otherwise, this is a register output.
150822873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
150922873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner
1510864635ad7b3046d3042311423071152c613961deChris Lattner      // If this is an early-clobber output, or if there is an input
1511864635ad7b3046d3042311423071152c613961deChris Lattner      // constraint that matches this, we need to reserve the input register
1512864635ad7b3046d3042311423071152c613961deChris Lattner      // so no other inputs allocate to it.
1513864635ad7b3046d3042311423071152c613961deChris Lattner      bool UsesInputRegister = false;
1514864635ad7b3046d3042311423071152c613961deChris Lattner      if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
1515864635ad7b3046d3042311423071152c613961deChris Lattner        UsesInputRegister = true;
15162223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner
1517864635ad7b3046d3042311423071152c613961deChris Lattner      // Copy the output from the appropriate register.  Find a register that
1518864635ad7b3046d3042311423071152c613961deChris Lattner      // we can use.
1519864635ad7b3046d3042311423071152c613961deChris Lattner      RegsForValue Regs =
1520864635ad7b3046d3042311423071152c613961deChris Lattner        GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
1521864635ad7b3046d3042311423071152c613961deChris Lattner                             true, UsesInputRegister,
1522864635ad7b3046d3042311423071152c613961deChris Lattner                             OutputRegs, InputRegs);
1523864635ad7b3046d3042311423071152c613961deChris Lattner      assert(!Regs.Regs.empty() && "Couldn't allocate output reg!");
15242cc2f66c25d9576743026688fdae5ed402726532Chris Lattner
15252cc2f66c25d9576743026688fdae5ed402726532Chris Lattner      if (!Constraints[i].isIndirectOutput) {
1526864635ad7b3046d3042311423071152c613961deChris Lattner        assert(RetValRegs.Regs.empty() &&
15272cc2f66c25d9576743026688fdae5ed402726532Chris Lattner               "Cannot have multiple output constraints yet!");
15282cc2f66c25d9576743026688fdae5ed402726532Chris Lattner        assert(I.getType() != Type::VoidTy && "Bad inline asm!");
1529864635ad7b3046d3042311423071152c613961deChris Lattner        RetValRegs = Regs;
15302cc2f66c25d9576743026688fdae5ed402726532Chris Lattner      } else {
153122873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner        IndirectStoresToEmit.push_back(std::make_pair(Regs,
153222873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner                                                      I.getOperand(OpNum)));
15332cc2f66c25d9576743026688fdae5ed402726532Chris Lattner        OpNum++;  // Consumes a call operand.
15342cc2f66c25d9576743026688fdae5ed402726532Chris Lattner      }
15356656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner
15366656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner      // Add information to the INLINEASM node to know that this register is
15376656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner      // set.
1538c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands);
15396656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner      break;
15406656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    }
15416656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    case InlineAsm::isInput: {
154222873462c965a57664c4e375aa6e8bf02c9c6ad8Chris Lattner      SDOperand InOperandVal = getValue(I.getOperand(OpNum));
15434e4b576e2edfc7d40a3d7177c639acbe91cfd45fChris Lattner      OpNum++;  // Consumes a call operand.
15443d81fee8511536962543cbf420fd70ef15ae9c3aChris Lattner
15452223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner      if (isdigit(ConstraintCode[0])) {    // Matching constraint?
15462223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner        // If this is required to match an output register we have already set,
15472223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner        // just use its register.
15482223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner        unsigned OperandNo = atoi(ConstraintCode.c_str());
15493d81fee8511536962543cbf420fd70ef15ae9c3aChris Lattner
1550c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        // Scan until we find the definition we already emitted of this operand.
1551c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        // When we find it, create a RegsForValue operand.
1552c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        unsigned CurOp = 2;  // The first operand.
1553c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        for (; OperandNo; --OperandNo) {
1554c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          // Advance to the next operand.
1555c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          unsigned NumOps =
1556c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner            cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
1557c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          assert((NumOps & 7) == 2 /*REGDEF*/ &&
1558c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner                 "Skipped past definitions?");
1559c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          CurOp += (NumOps>>3)+1;
1560c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        }
1561c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
1562c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        unsigned NumOps =
1563c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
1564c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        assert((NumOps & 7) == 2 /*REGDEF*/ &&
1565c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner               "Skipped past definitions?");
1566c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
1567c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        // Add NumOps>>3 registers to MatchedRegs.
1568c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        RegsForValue MatchedRegs;
1569c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        MatchedRegs.ValueVT = InOperandVal.getValueType();
1570c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        MatchedRegs.RegVT   = AsmNodeOperands[CurOp+1].getValueType();
1571c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
1572c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          unsigned Reg=cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
1573c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner          MatchedRegs.Regs.push_back(Reg);
1574c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        }
1575c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
1576c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        // Use the produced MatchedRegs object to
1577c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag);
1578c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
157987bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        break;
158087bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      }
158187bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
158287bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
158387bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      if (ConstraintCode.size() == 1)   // not a physreg name.
158487bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        CTy = TLI.getConstraintType(ConstraintCode[0]);
15853d81fee8511536962543cbf420fd70ef15ae9c3aChris Lattner
158687bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      if (CTy == TargetLowering::C_Other) {
158787bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        if (!TLI.isOperandValidForConstraint(InOperandVal, ConstraintCode[0]))
158887bc3bd1213ced06eade93e3267178198d41a381Chris Lattner          assert(0 && "MATCH FAIL!");
1589c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
159087bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        // Add information to the INLINEASM node to know about this input.
159187bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        unsigned ResOpType = 3 /*IMM*/ | (1 << 3);
159287bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
159387bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        AsmNodeOperands.push_back(InOperandVal);
159487bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        break;
159587bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      } else if (CTy == TargetLowering::C_Memory) {
159687bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        // Memory input.
159787bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
159887bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        // Check that the operand isn't a float.
159987bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        if (!MVT::isInteger(InOperandVal.getValueType()))
160087bc3bd1213ced06eade93e3267178198d41a381Chris Lattner          assert(0 && "MATCH FAIL!");
1601c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner
160287bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        // Extend/truncate to the right pointer type if needed.
160387bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        MVT::ValueType PtrType = TLI.getPointerTy();
160487bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        if (InOperandVal.getValueType() < PtrType)
160587bc3bd1213ced06eade93e3267178198d41a381Chris Lattner          InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
160687bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        else if (InOperandVal.getValueType() > PtrType)
160787bc3bd1213ced06eade93e3267178198d41a381Chris Lattner          InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
160887bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
160987bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        // Add information to the INLINEASM node to know about this input.
161087bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        unsigned ResOpType = 4/*MEM*/ | (1 << 3);
161187bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
161287bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        AsmNodeOperands.push_back(InOperandVal);
1613c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        break;
16142223aea6ed33e4261d506afdcfbf30ccd8f52bfbChris Lattner      }
161587bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
161687bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
161787bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
161887bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      // Copy the input into the appropriate registers.
161987bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      RegsForValue InRegs =
162087bc3bd1213ced06eade93e3267178198d41a381Chris Lattner        GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
162187bc3bd1213ced06eade93e3267178198d41a381Chris Lattner                             false, true, OutputRegs, InputRegs);
162287bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      // FIXME: should be match fail.
162387bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!");
162487bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
162587bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag);
162687bc3bd1213ced06eade93e3267178198d41a381Chris Lattner
162787bc3bd1213ced06eade93e3267178198d41a381Chris Lattner      InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands);
16286656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner      break;
16296656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    }
1630c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    case InlineAsm::isClobber: {
1631c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      RegsForValue ClobberedRegs =
1632c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        GetRegistersForValue(ConstraintCode, MVT::Other, false, false,
1633c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner                             OutputRegs, InputRegs);
1634c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      // Add the clobbered value to the operand list, so that the register
1635c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      // allocator is aware that the physreg got clobbered.
1636c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner      if (!ClobberedRegs.Regs.empty())
1637c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner        ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands);
16386656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner      break;
16396656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    }
1640c3a9f8d31ce93ba384bd2bbdd55c757b06600a15Chris Lattner    }
16416656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  }
1642ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1643ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  // Finish up input operands.
1644ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  AsmNodeOperands[0] = Chain;
1645ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  if (Flag.Val) AsmNodeOperands.push_back(Flag);
1646ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1647ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  std::vector<MVT::ValueType> VTs;
1648ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  VTs.push_back(MVT::Other);
1649ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  VTs.push_back(MVT::Flag);
1650ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  Chain = DAG.getNode(ISD::INLINEASM, VTs, AsmNodeOperands);
1651ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  Flag = Chain.getValue(1);
1652ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
16536656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  // If this asm returns a register value, copy the result from that register
16546656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  // and set it as the value of the call.
1655864635ad7b3046d3042311423071152c613961deChris Lattner  if (!RetValRegs.Regs.empty())
1656864635ad7b3046d3042311423071152c613961deChris Lattner    setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag));
16576656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner
16586656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  std::vector<std::pair<SDOperand, Value*> > StoresToEmit;
16596656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner
16606656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  // Process indirect outputs, first output all of the flagged copies out of
16616656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  // physregs.
16626656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
1663864635ad7b3046d3042311423071152c613961deChris Lattner    RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
16646656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    Value *Ptr = IndirectStoresToEmit[i].second;
1665864635ad7b3046d3042311423071152c613961deChris Lattner    SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag);
1666864635ad7b3046d3042311423071152c613961deChris Lattner    StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
16676656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  }
1668ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
16696656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  // Emit the non-flagged stores from the physregs.
16706656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  std::vector<SDOperand> OutChains;
16716656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
16726656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
16736656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner                                    StoresToEmit[i].first,
16746656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner                                    getValue(StoresToEmit[i].second),
16756656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner                                    DAG.getSrcValue(StoresToEmit[i].second)));
16766656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner  if (!OutChains.empty())
16776656dd1a7888e6dabc82ebce734734127b1df6a7Chris Lattner    Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains);
1678ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner  DAG.setRoot(Chain);
1679ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner}
1680ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
1681ce7518ce92f45568dc7d4cbb863284afb962fec5Chris Lattner
16821c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitMalloc(MallocInst &I) {
16831c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDOperand Src = getValue(I.getOperand(0));
16841c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
16851c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MVT::ValueType IntPtr = TLI.getPointerTy();
168668cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner
168768cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner  if (IntPtr < Src.getValueType())
168868cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner    Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
168968cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner  else if (IntPtr > Src.getValueType())
169068cd65ea689907fb8a4aa80d72d182921e94607fChris Lattner    Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
16911c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
16921c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Scale the source by the type size.
16931c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType());
16941c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  Src = DAG.getNode(ISD::MUL, Src.getValueType(),
16951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                    Src, getIntPtrConstant(ElementSize));
16961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
16971c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  std::vector<std::pair<SDOperand, const Type*> > Args;
16981c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType()));
1699cf5734dddd66af9388a171b44996505ede47feedChris Lattner
1700cf5734dddd66af9388a171b44996505ede47feedChris Lattner  std::pair<SDOperand,SDOperand> Result =
1701adf6a965a321372c640845407195594835921eb4Chris Lattner    TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true,
1702cf5734dddd66af9388a171b44996505ede47feedChris Lattner                    DAG.getExternalSymbol("malloc", IntPtr),
1703cf5734dddd66af9388a171b44996505ede47feedChris Lattner                    Args, DAG);
1704cf5734dddd66af9388a171b44996505ede47feedChris Lattner  setValue(&I, Result.first);  // Pointers always fit in registers
1705cf5734dddd66af9388a171b44996505ede47feedChris Lattner  DAG.setRoot(Result.second);
17061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
17071c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
17081c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitFree(FreeInst &I) {
17091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  std::vector<std::pair<SDOperand, const Type*> > Args;
17101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  Args.push_back(std::make_pair(getValue(I.getOperand(0)),
17111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                                TLI.getTargetData().getIntPtrType()));
17121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MVT::ValueType IntPtr = TLI.getPointerTy();
1713cf5734dddd66af9388a171b44996505ede47feedChris Lattner  std::pair<SDOperand,SDOperand> Result =
1714adf6a965a321372c640845407195594835921eb4Chris Lattner    TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true,
1715cf5734dddd66af9388a171b44996505ede47feedChris Lattner                    DAG.getExternalSymbol("free", IntPtr), Args, DAG);
1716cf5734dddd66af9388a171b44996505ede47feedChris Lattner  DAG.setRoot(Result.second);
17171c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
17181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
1719025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// InsertAtEndOfBasicBlock - This method should be implemented by targets that
1720025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// mark instructions with the 'usesCustomDAGSchedInserter' flag.  These
1721025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// instructions are special in various ways, which require special support to
1722025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// insert.  The specified MachineInstr is created but not inserted into any
1723025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner// basic blocks, and the scheduler passes ownership of it to this method.
1724025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris LattnerMachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1725025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner                                                       MachineBasicBlock *MBB) {
1726025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner  std::cerr << "If a target marks an instruction with "
1727025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner               "'usesCustomDAGSchedInserter', it must implement "
1728025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner               "TargetLowering::InsertAtEndOfBasicBlock!\n";
1729025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner  abort();
1730025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner  return 0;
1731025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner}
1732025c39bf36b6bc2f35a5544338de6cf96fa511a9Chris Lattner
173339ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattnervoid SelectionDAGLowering::visitVAStart(CallInst &I) {
1734acc398c195a697795bff3245943d104eb19192b9Nate Begeman  DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(),
1735acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          getValue(I.getOperand(1)),
1736acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          DAG.getSrcValue(I.getOperand(1))));
173739ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner}
173839ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner
17391c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitVAArg(VAArgInst &I) {
1740acc398c195a697795bff3245943d104eb19192b9Nate Begeman  SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
1741acc398c195a697795bff3245943d104eb19192b9Nate Begeman                             getValue(I.getOperand(0)),
1742acc398c195a697795bff3245943d104eb19192b9Nate Begeman                             DAG.getSrcValue(I.getOperand(0)));
1743acc398c195a697795bff3245943d104eb19192b9Nate Begeman  setValue(&I, V);
1744acc398c195a697795bff3245943d104eb19192b9Nate Begeman  DAG.setRoot(V.getValue(1));
17451c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
17461c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
17471c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitVAEnd(CallInst &I) {
1748acc398c195a697795bff3245943d104eb19192b9Nate Begeman  DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
1749acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          getValue(I.getOperand(1)),
1750acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          DAG.getSrcValue(I.getOperand(1))));
17511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
17521c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
17531c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGLowering::visitVACopy(CallInst &I) {
1754acc398c195a697795bff3245943d104eb19192b9Nate Begeman  DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(),
1755acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          getValue(I.getOperand(1)),
1756acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          getValue(I.getOperand(2)),
1757acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          DAG.getSrcValue(I.getOperand(1)),
1758acc398c195a697795bff3245943d104eb19192b9Nate Begeman                          DAG.getSrcValue(I.getOperand(2))));
17591c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
17601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
176139ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner// It is always conservatively correct for llvm.returnaddress and
176239ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner// llvm.frameaddress to return 0.
176339ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattnerstd::pair<SDOperand, SDOperand>
176439ae3622791986a0232f7e4797b633f8fa9e54d2Chris LattnerTargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain,
176539ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner                                        unsigned Depth, SelectionDAG &DAG) {
176639ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner  return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain);
17671c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
17681c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
176950381b6c4180e9a2b983d4623da2e485cd768632Chris LattnerSDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1770171453a284b097f1ee89fb87ff495c3a6c7b6297Chris Lattner  assert(0 && "LowerOperation not implemented for this target!");
1771171453a284b097f1ee89fb87ff495c3a6c7b6297Chris Lattner  abort();
1772d3f03e4b50feb6abfa9fec8b0aa705d45134c59eMisha Brukman  return SDOperand();
1773171453a284b097f1ee89fb87ff495c3a6c7b6297Chris Lattner}
1774171453a284b097f1ee89fb87ff495c3a6c7b6297Chris Lattner
17750aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate BegemanSDOperand TargetLowering::CustomPromoteOperation(SDOperand Op,
17760aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate Begeman                                                 SelectionDAG &DAG) {
17770aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate Begeman  assert(0 && "CustomPromoteOperation not implemented for this target!");
17780aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate Begeman  abort();
17790aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate Begeman  return SDOperand();
17800aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate Begeman}
17810aed7840ec8cc85f91b4aa6e69318bba0cbd1f03Nate Begeman
178239ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattnervoid SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) {
178339ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner  unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue();
178439ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner  std::pair<SDOperand,SDOperand> Result =
1785a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner    TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG);
178639ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner  setValue(&I, Result.first);
178739ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner  DAG.setRoot(Result.second);
178839ae3622791986a0232f7e4797b633f8fa9e54d2Chris Lattner}
17891c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
179074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng/// getMemsetValue - Vectorized representation of the memset value
17911db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng/// operand.
17921db92f947cc600dee5edb9305a4e0f1c5c872965Evan Chengstatic SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
1793a47876d87a84fa94bf9f09cfef6756223575611dEvan Cheng                                SelectionDAG &DAG) {
17941db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  MVT::ValueType CurVT = VT;
17951db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
17961db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    uint64_t Val   = C->getValue() & 255;
17971db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    unsigned Shift = 8;
17981db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    while (CurVT != MVT::i8) {
17991db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      Val = (Val << Shift) | Val;
18001db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      Shift <<= 1;
18011db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
18021db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    }
18031db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    return DAG.getConstant(Val, VT);
18041db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  } else {
18051db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
18061db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    unsigned Shift = 8;
18071db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    while (CurVT != MVT::i8) {
18081db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      Value =
18091db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng        DAG.getNode(ISD::OR, VT,
18101db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng                    DAG.getNode(ISD::SHL, VT, Value,
18111db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng                                DAG.getConstant(Shift, MVT::i8)), Value);
18121db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      Shift <<= 1;
18131db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
18141db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    }
18151db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
18161db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    return Value;
18171db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  }
18181db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng}
18191db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
182074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
182174d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng/// used when a memcpy is turned into a memset when the source is a constant
182274d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng/// string ptr.
182374d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Chengstatic SDOperand getMemsetStringVal(MVT::ValueType VT,
182474d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                                    SelectionDAG &DAG, TargetLowering &TLI,
182574d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                                    std::string &Str, unsigned Offset) {
182674d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  MVT::ValueType CurVT = VT;
182774d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  uint64_t Val = 0;
182874d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  unsigned MSB = getSizeInBits(VT) / 8;
182974d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  if (TLI.isLittleEndian())
183074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng    Offset = Offset + MSB - 1;
183174d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  for (unsigned i = 0; i != MSB; ++i) {
183274d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng    Val = (Val << 8) | Str[Offset];
183374d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng    Offset += TLI.isLittleEndian() ? -1 : 1;
183474d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  }
183574d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng  return DAG.getConstant(Val, VT);
183674d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng}
183774d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng
18381db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng/// getMemBasePlusOffset - Returns base and offset node for the
18391db92f947cc600dee5edb9305a4e0f1c5c872965Evan Chengstatic SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
18401db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng                                      SelectionDAG &DAG, TargetLowering &TLI) {
18411db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  MVT::ValueType VT = Base.getValueType();
18421db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
18431db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng}
18441db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
1845c4f8eee05447a1d7ead3deabbeb087e3e037f3f8Evan Cheng/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
184680e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng/// to replace the memset / memcpy is below the threshold. It also returns the
184780e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng/// types of the sequence of  memory ops to perform memset / memcpy.
1848c4f8eee05447a1d7ead3deabbeb087e3e037f3f8Evan Chengstatic bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
1849c4f8eee05447a1d7ead3deabbeb087e3e037f3f8Evan Cheng                                     unsigned Limit, uint64_t Size,
1850c4f8eee05447a1d7ead3deabbeb087e3e037f3f8Evan Cheng                                     unsigned Align, TargetLowering &TLI) {
18511db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  MVT::ValueType VT;
18521db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
18531db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  if (TLI.allowsUnalignedMemoryAccesses()) {
18541db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    VT = MVT::i64;
18551db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  } else {
18561db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    switch (Align & 7) {
18571db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    case 0:
18581db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      VT = MVT::i64;
18591db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      break;
18601db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    case 4:
18611db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      VT = MVT::i32;
18621db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      break;
18631db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    case 2:
18641db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      VT = MVT::i16;
18651db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      break;
18661db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    default:
18671db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      VT = MVT::i8;
18681db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      break;
18691db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    }
18701db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  }
18711db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
187280e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng  MVT::ValueType LVT = MVT::i64;
187380e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng  while (!TLI.isTypeLegal(LVT))
187480e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng    LVT = (MVT::ValueType)((unsigned)LVT - 1);
187580e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng  assert(MVT::isInteger(LVT));
18761db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
187780e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng  if (VT > LVT)
187880e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng    VT = LVT;
187980e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng
1880dea7245997f37972ed2f94d4ca1ec50c5af5000aEvan Cheng  unsigned NumMemOps = 0;
18811db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  while (Size != 0) {
18821db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    unsigned VTSize = getSizeInBits(VT) / 8;
18831db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    while (VTSize > Size) {
18841db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      VT = (MVT::ValueType)((unsigned)VT - 1);
18851db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      VTSize >>= 1;
18861db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    }
188780e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng    assert(MVT::isInteger(VT));
188880e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng
188980e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng    if (++NumMemOps > Limit)
189080e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng      return false;
18911db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    MemOps.push_back(VT);
18921db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    Size -= VTSize;
18931db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  }
189480e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng
189580e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng  return true;
18961db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng}
18971db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
18987041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattnervoid SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
18991db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  SDOperand Op1 = getValue(I.getOperand(1));
19001db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  SDOperand Op2 = getValue(I.getOperand(2));
19011db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  SDOperand Op3 = getValue(I.getOperand(3));
19021db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  SDOperand Op4 = getValue(I.getOperand(4));
19031db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue();
19041db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  if (Align == 0) Align = 1;
19051db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
19061db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) {
19071db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    std::vector<MVT::ValueType> MemOps;
19081db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
19091db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    // Expand memset / memcpy to a series of load / store ops
19101db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    // if the size operand falls below a certain threshold.
19111db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    std::vector<SDOperand> OutChains;
19121db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    switch (Op) {
1913ac940ab1bf21be40f74a83b202419a20ad2e279fEvan Cheng    default: break;  // Do nothing for now.
19141db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    case ISD::MEMSET: {
1915c4f8eee05447a1d7ead3deabbeb087e3e037f3f8Evan Cheng      if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
1916c4f8eee05447a1d7ead3deabbeb087e3e037f3f8Evan Cheng                                   Size->getValue(), Align, TLI)) {
191780e89d7d6c02233a92d26bd4625e4188d48cbfa1Evan Cheng        unsigned NumMemOps = MemOps.size();
19181db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng        unsigned Offset = 0;
19191db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng        for (unsigned i = 0; i < NumMemOps; i++) {
19201db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng          MVT::ValueType VT = MemOps[i];
19211db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng          unsigned VTSize = getSizeInBits(VT) / 8;
1922a47876d87a84fa94bf9f09cfef6756223575611dEvan Cheng          SDOperand Value = getMemsetValue(Op2, VT, DAG);
1923c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng          SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, getRoot(),
1924c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng                                        Value,
1925864635ad7b3046d3042311423071152c613961deChris Lattner                                    getMemBasePlusOffset(Op1, Offset, DAG, TLI),
1926864635ad7b3046d3042311423071152c613961deChris Lattner                                      DAG.getSrcValue(I.getOperand(1), Offset));
1927c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng          OutChains.push_back(Store);
1928c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng          Offset += VTSize;
1929c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng        }
1930c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng      }
1931c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng      break;
1932c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng    }
1933c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng    case ISD::MEMCPY: {
1934c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng      if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(),
1935c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng                                   Size->getValue(), Align, TLI)) {
1936c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng        unsigned NumMemOps = MemOps.size();
1937cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng        unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0;
193874d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        GlobalAddressSDNode *G = NULL;
193974d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        std::string Str;
1940cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng        bool CopyFromStr = false;
194174d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng
194274d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        if (Op2.getOpcode() == ISD::GlobalAddress)
194374d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          G = cast<GlobalAddressSDNode>(Op2);
194474d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        else if (Op2.getOpcode() == ISD::ADD &&
194574d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                 Op2.getOperand(0).getOpcode() == ISD::GlobalAddress &&
194674d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                 Op2.getOperand(1).getOpcode() == ISD::Constant) {
194774d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          G = cast<GlobalAddressSDNode>(Op2.getOperand(0));
1948cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng          SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue();
194974d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        }
195074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        if (G) {
195174d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
1952cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng          if (GV) {
195321b6c9d6477c8df3f884c3f1ebeaaa44dd53aafeJim Laskey            Str = GV->getStringValue();
1954cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng            if (!Str.empty()) {
1955cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng              CopyFromStr = true;
1956cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng              SrcOff += SrcDelta;
1957cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng            }
1958cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng          }
195974d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng        }
196074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng
1961c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng        for (unsigned i = 0; i < NumMemOps; i++) {
1962c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng          MVT::ValueType VT = MemOps[i];
1963c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng          unsigned VTSize = getSizeInBits(VT) / 8;
196474d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          SDOperand Value, Chain, Store;
196574d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng
1966cffbb5174f283d123d6bfc582292f4a9c84cb3edEvan Cheng          if (CopyFromStr) {
196774d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng            Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
196874d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng            Chain = getRoot();
196974d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng            Store =
197074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng              DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
197174d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                          getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
197274d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                          DAG.getSrcValue(I.getOperand(1), DstOff));
197374d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          } else {
197474d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng            Value = DAG.getLoad(VT, getRoot(),
197574d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                        getMemBasePlusOffset(Op2, SrcOff, DAG, TLI),
197674d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                        DAG.getSrcValue(I.getOperand(2), SrcOff));
197774d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng            Chain = Value.getValue(1);
197874d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng            Store =
197974d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng              DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
198074d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                          getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
198174d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng                          DAG.getSrcValue(I.getOperand(1), DstOff));
198274d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          }
1983c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng          OutChains.push_back(Store);
198474d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          SrcOff += VTSize;
198574d0aa9a4b1f5e021d2ce851a7af344e9b4ebb23Evan Cheng          DstOff += VTSize;
19861db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng        }
19871db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng      }
1988c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng      break;
19891db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    }
19901db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng    }
1991c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng
1992c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng    if (!OutChains.empty()) {
1993c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng      DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
1994c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng      return;
1995c080d6fb3dc7769c5a1e00c6a77cb415453b0b89Evan Cheng    }
19961db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  }
19971db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng
19987041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner  std::vector<SDOperand> Ops;
1999a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  Ops.push_back(getRoot());
20001db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  Ops.push_back(Op1);
20011db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  Ops.push_back(Op2);
20021db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  Ops.push_back(Op3);
20031db92f947cc600dee5edb9305a4e0f1c5c872965Evan Cheng  Ops.push_back(Op4);
20047041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner  DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops));
20051c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
20061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
20077041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner//===----------------------------------------------------------------------===//
20087041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner// SelectionDAGISel code
20097041ee35adecb864e3e8df490aa73b0605fbfb5aChris Lattner//===----------------------------------------------------------------------===//
20101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
20111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerunsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
20121c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
20131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
20141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2015495a0b51915eb763576874f29192820b731edc22Chris Lattnervoid SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
201636b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner  // FIXME: we only modify the CFG to split critical edges.  This
201736b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner  // updates dom and loop info.
2018495a0b51915eb763576874f29192820b731edc22Chris Lattner}
20191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2020c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2021c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset,
2022c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// casting to the type of GEPI.
2023c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattnerstatic Value *InsertGEPComputeCode(Value *&V, BasicBlock *BB, Instruction *GEPI,
2024c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner                                   Value *Ptr, Value *PtrOffset) {
2025c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  if (V) return V;   // Already computed.
2026c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2027c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  BasicBlock::iterator InsertPt;
2028c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  if (BB == GEPI->getParent()) {
2029c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    // If insert into the GEP's block, insert right after the GEP.
2030c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    InsertPt = GEPI;
2031c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    ++InsertPt;
2032c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  } else {
2033c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    // Otherwise, insert at the top of BB, after any PHI nodes
2034c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    InsertPt = BB->begin();
2035c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    while (isa<PHINode>(InsertPt)) ++InsertPt;
2036c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  }
2037c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2038c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into
2039c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // BB so that there is only one value live across basic blocks (the cast
2040c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // operand).
2041c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  if (CastInst *CI = dyn_cast<CastInst>(Ptr))
2042c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType()))
2043c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner      Ptr = new CastInst(CI->getOperand(0), CI->getType(), "", InsertPt);
2044c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner
2045c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // Add the offset, cast it to the right type.
2046c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt);
2047c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  Ptr = new CastInst(Ptr, GEPI->getType(), "", InsertPt);
2048c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  return V = Ptr;
2049c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner}
2050c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2051c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2052c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction
2053c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// selection, we want to be a bit careful about some things.  In particular, if
2054c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// we have a GEP instruction that is used in a different block than it is
2055c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// defined, the addressing expression of the GEP cannot be folded into loads or
2056c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// stores that use it.  In this case, decompose the GEP and move constant
2057c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner/// indices into blocks that use it.
2058c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattnerstatic void OptimizeGEPExpression(GetElementPtrInst *GEPI,
2059c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner                                  const TargetData &TD) {
2060c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // If this GEP is only used inside the block it is defined in, there is no
2061c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // need to rewrite it.
2062c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  bool isUsedOutsideDefBB = false;
2063c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  BasicBlock *DefBB = GEPI->getParent();
2064c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end();
2065c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner       UI != E; ++UI) {
2066c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    if (cast<Instruction>(*UI)->getParent() != DefBB) {
2067c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      isUsedOutsideDefBB = true;
2068c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      break;
2069c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    }
2070c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  }
2071c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  if (!isUsedOutsideDefBB) return;
2072c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2073c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // If this GEP has no non-zero constant indices, there is nothing we can do,
2074c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // ignore it.
2075c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  bool hasConstantIndex = false;
2076c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
2077c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner       E = GEPI->op_end(); OI != E; ++OI) {
2078c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI))
2079c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      if (CI->getRawValue()) {
2080c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        hasConstantIndex = true;
2081c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        break;
2082c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      }
2083c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  }
20843802c2552fe4475418db3c948e7601d7259f996dChris Lattner  // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses.
20853802c2552fe4475418db3c948e7601d7259f996dChris Lattner  if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) return;
2086c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2087c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // Otherwise, decompose the GEP instruction into multiplies and adds.  Sum the
2088c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // constant offset (which we now know is non-zero) and deal with it later.
2089c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  uint64_t ConstantOffset = 0;
2090c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  const Type *UIntPtrTy = TD.getIntPtrType();
2091c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI);
2092c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  const Type *Ty = GEPI->getOperand(0)->getType();
2093c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2094c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
2095c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner       E = GEPI->op_end(); OI != E; ++OI) {
2096c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    Value *Idx = *OI;
2097c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2098c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      unsigned Field = cast<ConstantUInt>(Idx)->getValue();
2099c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      if (Field)
2100c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field];
2101c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      Ty = StTy->getElementType(Field);
2102c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    } else {
2103c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      Ty = cast<SequentialType>(Ty)->getElementType();
2104c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2105c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      // Handle constant subscripts.
2106c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2107c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        if (CI->getRawValue() == 0) continue;
2108c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2109c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
2110c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner          ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
2111c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        else
2112c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner          ConstantOffset+=TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue();
2113c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        continue;
2114c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      }
2115c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2116c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      // Ptr = Ptr + Idx * ElementSize;
2117c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2118c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      // Cast Idx to UIntPtrTy if needed.
2119c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      Idx = new CastInst(Idx, UIntPtrTy, "", GEPI);
2120c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2121c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      uint64_t ElementSize = TD.getTypeSize(Ty);
2122c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      // Mask off bits that should not be set.
2123c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
2124c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize);
2125c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2126c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      // Multiply by the element size and add to the base.
2127c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI);
2128c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI);
2129c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    }
2130c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  }
2131c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2132c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // Make sure that the offset fits in uintptr_t.
2133c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
2134c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  Constant *PtrOffset = ConstantUInt::get(UIntPtrTy, ConstantOffset);
2135c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2136c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // Okay, we have now emitted all of the variable index parts to the BB that
2137c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // the GEP is defined in.  Loop over all of the using instructions, inserting
2138c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // an "add Ptr, ConstantOffset" into each block that uses it and update the
2139c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // instruction to use the newly computed value, making GEPI dead.  When the
2140c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // user is a load or store instruction address, we emit the add into the user
2141c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // block, otherwise we use a canonical version right next to the gep (these
2142c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  // won't be foldable as addresses, so we might as well share the computation).
2143c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner
2144c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  std::map<BasicBlock*,Value*> InsertedExprs;
2145c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  while (!GEPI->use_empty()) {
2146c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    Instruction *User = cast<Instruction>(GEPI->use_back());
2147c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner
2148c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    // If this use is not foldable into the addressing mode, use a version
2149c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    // emitted in the GEP block.
2150c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    Value *NewVal;
2151c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    if (!isa<LoadInst>(User) &&
2152c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner        (!isa<StoreInst>(User) || User->getOperand(0) == GEPI)) {
2153c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner      NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI,
2154c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner                                    Ptr, PtrOffset);
2155c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    } else {
2156c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner      // Otherwise, insert the code in the User's block so it can be folded into
2157c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner      // any users in that block.
2158c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner      NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()],
2159c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner                                    User->getParent(), GEPI,
2160c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner                                    Ptr, PtrOffset);
2161c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    }
2162c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner    User->replaceUsesOfWith(GEPI, NewVal);
2163c78b0b740bcf2e00a8871090709c3571fe442f07Chris Lattner  }
2164c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2165c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // Finally, the GEP is dead, remove it.
2166c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  GEPI->eraseFromParent();
2167c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner}
2168c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
21691c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnerbool SelectionDAGISel::runOnFunction(Function &Fn) {
21701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
21711c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  RegMap = MF.getSSARegMap();
21721c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n");
21731c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2174c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // First, split all critical edges for PHI nodes with incoming values that are
2175c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // constants, this way the load of the constant into a vreg will not be placed
2176c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // into MBBs that are used some other way.
2177c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  //
2178c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // In this pass we also look for GEP instructions that are used across basic
2179c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  // blocks and rewrites them to improve basic-block-at-a-time selection.
2180c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner  //
218136b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner  for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
218236b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner    PHINode *PN;
2183c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    BasicBlock::iterator BBI;
2184c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    for (BBI = BB->begin(); (PN = dyn_cast<PHINode>(BBI)); ++BBI)
218536b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner      for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
218636b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner        if (isa<Constant>(PN->getIncomingValue(i)))
218736b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner          SplitCriticalEdge(PN->getIncomingBlock(i), BB);
2188c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner
2189c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner    for (BasicBlock::iterator E = BB->end(); BBI != E; )
2190c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner      if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(BBI++))
2191c88d8e944dae71e31595b8ae264668e68db6b8edChris Lattner        OptimizeGEPExpression(GEPI, TLI.getTargetData());
219236b708f05720c407f5e5657f2fd4b0677702ad46Chris Lattner  }
2193c9ea6fde305b35ab7c9f909ac390d4b53e33d536Chris Lattner
21941c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
21951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
21961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
21971c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SelectBasicBlock(I, MF, FuncInfo);
2198edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
21991c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  return true;
22001c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
22011c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
22021c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2203ddb870b065984007a0df645ad97c6ad6a6f12de0Chris LattnerSDOperand SelectionDAGISel::
2204ddb870b065984007a0df645ad97c6ad6a6f12de0Chris LattnerCopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) {
2205f1fdacae8c1bd9e5ff4619546b3a28e93fb75ab5Chris Lattner  SDOperand Op = SDL.getValue(V);
220618c2f13e0f9d0e5d6227cf6d1881e9ee3d1b6109Chris Lattner  assert((Op.getOpcode() != ISD::CopyFromReg ||
2207d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner          cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
220818c2f13e0f9d0e5d6227cf6d1881e9ee3d1b6109Chris Lattner         "Copy from a reg to the same reg!");
2209d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner
2210d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  // If this type is not legal, we must make sure to not create an invalid
2211d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  // register use.
2212d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  MVT::ValueType SrcVT = Op.getValueType();
2213d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT);
2214d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  SelectionDAG &DAG = SDL.DAG;
2215d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  if (SrcVT == DestVT) {
2216d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
2217d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  } else if (SrcVT < DestVT) {
2218d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    // The src value is promoted to the register.
2219fae59b99b8d1942f30a4be609423282c3fd62dd8Chris Lattner    if (MVT::isFloatingPoint(SrcVT))
2220fae59b99b8d1942f30a4be609423282c3fd62dd8Chris Lattner      Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op);
2221fae59b99b8d1942f30a4be609423282c3fd62dd8Chris Lattner    else
2222fab08875b73656f373b10a59aad475615df82bafChris Lattner      Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op);
2223d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
2224d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  } else  {
2225d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    // The src value is expanded into multiple registers.
2226d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
2227d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner                               Op, DAG.getConstant(0, MVT::i32));
2228d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
2229d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner                               Op, DAG.getConstant(1, MVT::i32));
2230d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo);
2231d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner    return DAG.getCopyToReg(Op, Reg+1, Hi);
2232d5d0f9bd20d9df07d6b4d41b7e8ed6d33b6a649dChris Lattner  }
22331c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
22341c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2235068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattnervoid SelectionDAGISel::
2236068a81e9fca511b9a3b3a0f28a8988a57f994652Chris LattnerLowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
2237068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner               std::vector<SDOperand> &UnorderedChains) {
22381c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // If this is the entry block, emit arguments.
2239068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner  Function &F = *BB->getParent();
22400afa8e348eab21d3e09ae3240544886d61879266Chris Lattner  FunctionLoweringInfo &FuncInfo = SDL.FuncInfo;
2241bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  SDOperand OldRoot = SDL.DAG.getRoot();
2242bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
2243bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner
2244bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  unsigned a = 0;
2245bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2246bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner       AI != E; ++AI, ++a)
2247bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    if (!AI->use_empty()) {
2248bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      SDL.setValue(AI, Args[a]);
2249fa57702388f139e964befecb4b98c7dfe836945fChris Lattner
2250bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      // If this argument is live outside of the entry block, insert a copy from
2251bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      // whereever we got it to the vreg that other BB's will reference it as.
2252bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      if (FuncInfo.ValueMap.count(AI)) {
2253bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner        SDOperand Copy =
2254bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner          CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]);
2255bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner        UnorderedChains.push_back(Copy);
2256bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      }
22570afa8e348eab21d3e09ae3240544886d61879266Chris Lattner    }
2258bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner
2259bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // Next, if the function has live ins that need to be copied into vregs,
2260bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // emit the copies now, into the top of the block.
2261bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  MachineFunction &MF = SDL.DAG.getMachineFunction();
2262bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  if (MF.livein_begin() != MF.livein_end()) {
2263bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    SSARegMap *RegMap = MF.getSSARegMap();
2264bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo();
2265bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    for (MachineFunction::livein_iterator LI = MF.livein_begin(),
2266bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner         E = MF.livein_end(); LI != E; ++LI)
2267bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner      if (LI->second)
2268bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner        MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
2269bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner                         LI->first, RegMap->getRegClass(LI->second));
22701c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
2271bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner
2272bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // Finally, if the target has anything special to do, allow it to do so.
2273bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction());
2274068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner}
2275068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner
2276068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner
2277068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattnervoid SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
2278068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner       std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
2279068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner                                    FunctionLoweringInfo &FuncInfo) {
2280068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner  SelectionDAGLowering SDL(DAG, TLI, FuncInfo);
2281068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner
2282068a81e9fca511b9a3b3a0f28a8988a57f994652Chris Lattner  std::vector<SDOperand> UnorderedChains;
2283edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
2284bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  // Lower any arguments needed in this block if this is the entry block.
2285bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner  if (LLVMBB == &LLVMBB->getParent()->front())
2286bf209489ad3f7a38ce6b2159b22d8727500e60c2Chris Lattner    LowerArguments(LLVMBB, SDL, UnorderedChains);
22871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
22881c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  BB = FuncInfo.MBBMap[LLVMBB];
22891c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDL.setCurrentBasicBlock(BB);
22901c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
22911c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Lower all of the non-terminator instructions.
22921c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
22931c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner       I != E; ++I)
22941c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    SDL.visit(*I);
22951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
22961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Ensure that all instructions which are used outside of their defining
22971c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // blocks are available as virtual registers.
22981c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
2299f1fdacae8c1bd9e5ff4619546b3a28e93fb75ab5Chris Lattner    if (!I->use_empty() && !isa<PHINode>(I)) {
2300ee749d7488bd42df0f67e2d80048c63415943785Chris Lattner      std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
23011c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      if (VMI != FuncInfo.ValueMap.end())
2302ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner        UnorderedChains.push_back(
2303ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner                           CopyValueToVirtualRegister(SDL, I, VMI->second));
23041c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    }
23051c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
23061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Handle PHI nodes in successor blocks.  Emit code into the SelectionDAG to
23071c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // ensure constants are generated when needed.  Remember the virtual registers
23081c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // that need to be added to the Machine PHI nodes as input.  We cannot just
23091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // directly add them, because expansion might result in multiple MBB's for one
23101c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // BB.  As such, the start of the BB might correspond to a different MBB than
23111c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // the end.
2312edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman  //
23131c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
23141c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Emit constants only once even if used by multiple PHI nodes.
23151c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  std::map<Constant*, unsigned> ConstantsOut;
23161c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
23171c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Check successor nodes PHI nodes that expect a constant to be available from
23181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // this block.
23191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  TerminatorInst *TI = LLVMBB->getTerminator();
23201c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
23211c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    BasicBlock *SuccBB = TI->getSuccessor(succ);
23221c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin();
23231c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    PHINode *PN;
23241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
23251c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // At this point we know that there is a 1-1 correspondence between LLVM PHI
23261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // nodes and Machine PHI nodes, but the incoming operands have not been
23271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    // emitted yet.
23281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    for (BasicBlock::iterator I = SuccBB->begin();
2329f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner         (PN = dyn_cast<PHINode>(I)); ++I)
2330f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner      if (!PN->use_empty()) {
2331f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        unsigned Reg;
2332f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
2333f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        if (Constant *C = dyn_cast<Constant>(PHIOp)) {
2334f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          unsigned &RegOut = ConstantsOut[C];
2335f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          if (RegOut == 0) {
2336f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner            RegOut = FuncInfo.CreateRegForValue(C);
2337ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner            UnorderedChains.push_back(
2338ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner                             CopyValueToVirtualRegister(SDL, C, RegOut));
2339f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          }
2340f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          Reg = RegOut;
2341f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        } else {
2342f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          Reg = FuncInfo.ValueMap[PHIOp];
2343ee749d7488bd42df0f67e2d80048c63415943785Chris Lattner          if (Reg == 0) {
2344edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman            assert(isa<AllocaInst>(PHIOp) &&
2345ee749d7488bd42df0f67e2d80048c63415943785Chris Lattner                   FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
2346ee749d7488bd42df0f67e2d80048c63415943785Chris Lattner                   "Didn't codegen value into a register!??");
2347ee749d7488bd42df0f67e2d80048c63415943785Chris Lattner            Reg = FuncInfo.CreateRegForValue(PHIOp);
2348ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner            UnorderedChains.push_back(
2349ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner                             CopyValueToVirtualRegister(SDL, PHIOp, Reg));
2350ee749d7488bd42df0f67e2d80048c63415943785Chris Lattner          }
23511c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner        }
2352edf128a7fa90f2b0b7ee24741a04a7ae1ecd6f7eMisha Brukman
2353f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        // Remember that this register needs to added to the machine PHI node as
2354f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        // the input for this MBB.
2355f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        unsigned NumElements =
2356f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          TLI.getNumElements(TLI.getValueType(PN->getType()));
2357f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner        for (unsigned i = 0, e = NumElements; i != e; ++i)
2358f44fd88e9cdd7b47acb71ac78e3dccb91319c72dChris Lattner          PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
23591c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner      }
23601c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
23611c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  ConstantsOut.clear();
23621c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2363ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner  // Turn all of the unordered chains into one factored node.
23645a6c6d98d561df671350a56c8031a3611f1c46faChris Lattner  if (!UnorderedChains.empty()) {
23657436b57de32333cc337b8c7cea208c8863eee793Chris Lattner    SDOperand Root = SDL.getRoot();
23667436b57de32333cc337b8c7cea208c8863eee793Chris Lattner    if (Root.getOpcode() != ISD::EntryToken) {
23677436b57de32333cc337b8c7cea208c8863eee793Chris Lattner      unsigned i = 0, e = UnorderedChains.size();
23687436b57de32333cc337b8c7cea208c8863eee793Chris Lattner      for (; i != e; ++i) {
23697436b57de32333cc337b8c7cea208c8863eee793Chris Lattner        assert(UnorderedChains[i].Val->getNumOperands() > 1);
23707436b57de32333cc337b8c7cea208c8863eee793Chris Lattner        if (UnorderedChains[i].Val->getOperand(0) == Root)
23717436b57de32333cc337b8c7cea208c8863eee793Chris Lattner          break;  // Don't add the root if we already indirectly depend on it.
23727436b57de32333cc337b8c7cea208c8863eee793Chris Lattner      }
23737436b57de32333cc337b8c7cea208c8863eee793Chris Lattner
23747436b57de32333cc337b8c7cea208c8863eee793Chris Lattner      if (i == e)
23757436b57de32333cc337b8c7cea208c8863eee793Chris Lattner        UnorderedChains.push_back(Root);
23767436b57de32333cc337b8c7cea208c8863eee793Chris Lattner    }
2377ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner    DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains));
2378ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner  }
2379ddb870b065984007a0df645ad97c6ad6a6f12de0Chris Lattner
23801c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Lower the terminator after the copies are emitted.
23811c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  SDL.visit(*LLVMBB->getTerminator());
2382a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner
2383a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  // Make sure the root of the DAG is up-to-date.
2384a651cf67b77cd477dd7da4baa8688af2ccd4f7f6Chris Lattner  DAG.setRoot(SDL.getRoot());
23851c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
23861c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
23871c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattnervoid SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
23881c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner                                        FunctionLoweringInfo &FuncInfo) {
2389b2efb853f00d45b1c8d57f92acd0028fbdeffda6Jim Laskey  SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>());
23901c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  CurDAG = &DAG;
23911c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
23921c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
23931c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // First step, lower LLVM code to some DAG.  This DAG may use operations and
23941c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // types that are not supported by the target.
23951c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
23961c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2397af21d55aee8d6852fedfd630395a3c7c95df8f12Chris Lattner  // Run the DAG combiner in pre-legalize mode.
2398af21d55aee8d6852fedfd630395a3c7c95df8f12Chris Lattner  DAG.Combine(false);
23992300f5504643eaddc307d3db8a3ccd224c4fa251Nate Begeman
24001c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(std::cerr << "Lowered selection DAG:\n");
24011c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(DAG.dump());
24021c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
24031c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // Second step, hack on the DAG until it only uses operations and types that
24041c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // the target supports.
2405ac9dc08c7f0ae60c125624d72c3022025d79ee9eChris Lattner  DAG.Legalize();
24061c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
24071c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(std::cerr << "Legalized selection DAG:\n");
24081c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(DAG.dump());
24091c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2410af21d55aee8d6852fedfd630395a3c7c95df8f12Chris Lattner  // Run the DAG combiner in post-legalize mode.
2411af21d55aee8d6852fedfd630395a3c7c95df8f12Chris Lattner  DAG.Combine(true);
24122300f5504643eaddc307d3db8a3ccd224c4fa251Nate Begeman
2413a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng  if (ViewISelDAGs) DAG.viewGraph();
2414d48050aa1509130871b8bb2453270c92b969c2e7Chris Lattner
2415a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // Third, instruction select all of the operations to machine code, adding the
2416a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // code to the MachineBasicBlock.
24171c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  InstructionSelectBasicBlock(DAG);
24181c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
24191c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(std::cerr << "Selected machine code:\n");
24201c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  DEBUG(BB->dump());
24211c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner
2422a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // Next, now that we know what the last MBB the LLVM BB expanded is, update
24231c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  // PHI nodes in successors.
24241c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
24251c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    MachineInstr *PHI = PHINodesToUpdate[i].first;
24261c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
24271c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner           "This is not a machine PHI node that we are updating!");
24281c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    PHI->addRegOperand(PHINodesToUpdate[i].second);
24291c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner    PHI->addMachineBasicBlockOperand(BB);
24301c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner  }
2431a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner
2432a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // Finally, add the CFG edges from the last selected MBB to the successor
2433a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  // MBBs.
2434a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  TerminatorInst *TI = LLVMBB->getTerminator();
2435a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
2436a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner    MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)];
2437a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner    BB->addSuccessor(Succ0MBB);
2438a33ef4816d2c192e36e7c025d18c66e89ef9d311Chris Lattner  }
24391c08c714bb3d07c3b39f06bfcbb4559fefca73f9Chris Lattner}
2440a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng
2441a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng//===----------------------------------------------------------------------===//
2442a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng/// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each
2443a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng/// target node in the graph.
2444a9c2091cd38e401c846391c9951ff416e709b65eEvan Chengvoid SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) {
2445a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng  if (ViewSchedDAGs) DAG.viewGraph();
24464ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  ScheduleDAG *SL = NULL;
24474ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng
24484ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  switch (ISHeuristic) {
24494ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  default: assert(0 && "Unrecognized scheduling heuristic");
24503f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng  case defaultScheduling:
24513f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng    if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
24523f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng      SL = createSimpleDAGScheduler(noScheduling, DAG, BB);
24533f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng    else /* TargetLowering::SchedulingForRegPressure */
24543f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng      SL = createBURRListDAGScheduler(DAG, BB);
24553f23952404cd03a8ab934cedcef7916f52a796c0Evan Cheng    break;
24564ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  case noScheduling:
245720a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner    SL = createBFS_DAGScheduler(DAG, BB);
245820a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner    break;
24594ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  case simpleScheduling:
246020a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner    SL = createSimpleDAGScheduler(false, DAG, BB);
246120a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner    break;
24624ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  case simpleNoItinScheduling:
246320a4921791eafc0cce00fb01dcacfcfc15a0d0fcChris Lattner    SL = createSimpleDAGScheduler(true, DAG, BB);
24644ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng    break;
2465f0f9c90204c650b9f3c3feb02ccfcb1e40c6acddEvan Cheng  case listSchedulingBURR:
2466f0f9c90204c650b9f3c3feb02ccfcb1e40c6acddEvan Cheng    SL = createBURRListDAGScheduler(DAG, BB);
2467a5de484bc7c8f896b5903999797c4d57f4e45185Chris Lattner    break;
246803fc53c174e654adae4e42a0c352c7937de2cd87Chris Lattner  case listSchedulingTD:
2469b0d21ef20c29f4ea46d21b488f17feaa6a8760e1Chris Lattner    SL = createTDListDAGScheduler(DAG, BB, CreateTargetHazardRecognizer());
2470a5de484bc7c8f896b5903999797c4d57f4e45185Chris Lattner    break;
24714ef10867499aa146cd819c78d8d37a8353d4f0ffEvan Cheng  }
2472a3818e6f9a62db0c5b6aee28e44c30d5f96c9fa4Chris Lattner  BB = SL->Run();
2473cccf1232a69e2d78516c61a97e7bfa26acefb714Evan Cheng  delete SL;
2474a9c2091cd38e401c846391c9951ff416e709b65eEvan Cheng}
24750e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
2476b0d21ef20c29f4ea46d21b488f17feaa6a8760e1Chris LattnerHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
2477b0d21ef20c29f4ea46d21b488f17feaa6a8760e1Chris Lattner  return new HazardRecognizer();
247803fc53c174e654adae4e42a0c352c7937de2cd87Chris Lattner}
247903fc53c174e654adae4e42a0c352c7937de2cd87Chris Lattner
24800e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
24810e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner/// by tblgen.  Others should not call it.
24820e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattnervoid SelectionDAGISel::
24830e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris LattnerSelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) {
24840e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  std::vector<SDOperand> InOps;
24850e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  std::swap(InOps, Ops);
24860e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
24870e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  Ops.push_back(InOps[0]);  // input chain.
24880e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  Ops.push_back(InOps[1]);  // input asm string.
24890e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
24900e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  const char *AsmStr = cast<ExternalSymbolSDNode>(InOps[1])->getSymbol();
24910e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  unsigned i = 2, e = InOps.size();
24920e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  if (InOps[e-1].getValueType() == MVT::Flag)
24930e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    --e;  // Don't process a flag operand if it is here.
24940e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
24950e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  while (i != e) {
24960e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue();
24970e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    if ((Flags & 7) != 4 /*MEM*/) {
24980e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      // Just skip over this operand, copying the operands verbatim.
24990e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1);
25000e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      i += (Flags >> 3) + 1;
25010e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    } else {
25020e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      assert((Flags >> 3) == 1 && "Memory operand with multiple values?");
25030e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      // Otherwise, this is a memory operand.  Ask the target to select it.
25040e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      std::vector<SDOperand> SelOps;
25050e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) {
25060e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner        std::cerr << "Could not match memory address.  Inline asm failure!\n";
25070e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner        exit(1);
25080e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      }
25090e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
25100e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      // Add this to the output node.
25110e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      Ops.push_back(DAG.getConstant(4/*MEM*/ | (SelOps.size() << 3), MVT::i32));
25120e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
25130e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner      i += 2;
25140e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    }
25150e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  }
25160e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner
25170e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  // Add the flag input back if present.
25180e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner  if (e != InOps.size())
25190e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner    Ops.push_back(InOps.back());
25200e43f2ba114df7bfc1f70d0ef62b663f6ea4c09dChris Lattner}
2521