Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the SelectionDAGISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
| 15 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 16 | #include "llvm/CallingConv.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Intrinsics.h" |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 27 | #include "llvm/CodeGen/SelectionDAG.h" |
| 28 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 29 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
| 31 | #include "llvm/Target/TargetFrameInfo.h" |
| 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | #include "llvm/Target/TargetLowering.h" |
| 34 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
| 39 | #include <map> |
| 40 | #include <iostream> |
| 41 | using namespace llvm; |
| 42 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
| 44 | GEPISelTest("enable-gep-isel-opt", cl::Hidden, |
| 45 | cl::desc("temporary for testing")); |
| 46 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 47 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
| 49 | ViewDAGs("view-isel-dags", cl::Hidden, |
| 50 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 51 | #else |
Chris Lattner | a639a43 | 2005-09-02 07:09:28 +0000 | [diff] [blame] | 52 | static const bool ViewDAGs = 0; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 55 | namespace llvm { |
| 56 | //===--------------------------------------------------------------------===// |
| 57 | /// FunctionLoweringInfo - This contains information that is global to a |
| 58 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 59 | class FunctionLoweringInfo { |
| 60 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 61 | TargetLowering &TLI; |
| 62 | Function &Fn; |
| 63 | MachineFunction &MF; |
| 64 | SSARegMap *RegMap; |
| 65 | |
| 66 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 67 | |
| 68 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 69 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 70 | |
| 71 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 72 | /// we must remember which virtual registers hold the values for |
| 73 | /// cross-basic-block values. |
| 74 | std::map<const Value*, unsigned> ValueMap; |
| 75 | |
| 76 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 77 | /// the entry block. This allows the allocas to be efficiently referenced |
| 78 | /// anywhere in the function. |
| 79 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 80 | |
| 81 | unsigned MakeReg(MVT::ValueType VT) { |
| 82 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 83 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 85 | unsigned CreateRegForValue(const Value *V) { |
| 86 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 87 | // The common case is that we will only create one register for this |
| 88 | // value. If we have that case, create and return the virtual register. |
| 89 | unsigned NV = TLI.getNumElements(VT); |
Chris Lattner | fb84980 | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 90 | if (NV == 1) { |
| 91 | // If we are promoting this value, pick the next largest supported type. |
Chris Lattner | 98e5c0e | 2005-01-16 01:11:19 +0000 | [diff] [blame] | 92 | return MakeReg(TLI.getTypeToTransformTo(VT)); |
Chris Lattner | fb84980 | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 93 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 95 | // If this value is represented with multiple target registers, make sure |
| 96 | // to create enough consequtive registers of the right (smaller) type. |
| 97 | unsigned NT = VT-1; // Find the type to use. |
| 98 | while (TLI.getNumElements((MVT::ValueType)NT) != 1) |
| 99 | --NT; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 101 | unsigned R = MakeReg((MVT::ValueType)NT); |
| 102 | for (unsigned i = 1; i != NV; ++i) |
| 103 | MakeReg((MVT::ValueType)NT); |
| 104 | return R; |
| 105 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 107 | unsigned InitializeRegForValue(const Value *V) { |
| 108 | unsigned &R = ValueMap[V]; |
| 109 | assert(R == 0 && "Already initialized this value register!"); |
| 110 | return R = CreateRegForValue(V); |
| 111 | } |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 116 | /// PHI nodes or outside of the basic block that defines it. |
| 117 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 118 | if (isa<PHINode>(I)) return true; |
| 119 | BasicBlock *BB = I->getParent(); |
| 120 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 121 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
| 122 | return true; |
| 123 | return false; |
| 124 | } |
| 125 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 126 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 127 | /// entry block, return true. |
| 128 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 129 | BasicBlock *Entry = A->getParent()->begin(); |
| 130 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 131 | if (cast<Instruction>(*UI)->getParent() != Entry) |
| 132 | return false; // Use not in entry block. |
| 133 | return true; |
| 134 | } |
| 135 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 136 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 137 | Function &fn, MachineFunction &mf) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 138 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 139 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 140 | // Create a vreg for each argument register that is not dead and is used |
| 141 | // outside of the entry block for the function. |
| 142 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 143 | AI != E; ++AI) |
| 144 | if (!isOnlyUsedInEntryBlock(AI)) |
| 145 | InitializeRegForValue(AI); |
| 146 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 147 | // Initialize the mapping of values to registers. This is only set up for |
| 148 | // instruction values that are used outside of the block that defines |
| 149 | // them. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 150 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 151 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 152 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 153 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) { |
| 154 | const Type *Ty = AI->getAllocatedType(); |
| 155 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 156 | unsigned Align = |
| 157 | std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 158 | AI->getAlignment()); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 159 | |
| 160 | // If the alignment of the value is smaller than the size of the value, |
| 161 | // and if the size of the value is particularly small (<= 8 bytes), |
| 162 | // round up to the size of the value for potentially better performance. |
| 163 | // |
| 164 | // FIXME: This could be made better with a preferred alignment hook in |
| 165 | // TargetData. It serves primarily to 8-byte align doubles for X86. |
| 166 | if (Align < TySize && TySize <= 8) Align = TySize; |
Chris Lattner | 2dfa819 | 2005-10-18 22:11:42 +0000 | [diff] [blame] | 167 | TySize *= CUI->getValue(); // Get total allocated size. |
Chris Lattner | d222f6a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 168 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 169 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 170 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 173 | for (; BB != EB; ++BB) |
| 174 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 175 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 176 | if (!isa<AllocaInst>(I) || |
| 177 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 178 | InitializeRegForValue(I); |
| 179 | |
| 180 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 181 | // also creates the initial PHI MachineInstrs, though none of the input |
| 182 | // operands are populated. |
Jeff Cohen | 2aeaf4e | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 183 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 184 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 185 | MBBMap[BB] = MBB; |
| 186 | MF.getBasicBlockList().push_back(MBB); |
| 187 | |
| 188 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 189 | // appropriate. |
| 190 | PHINode *PN; |
| 191 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 192 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 193 | if (!PN->use_empty()) { |
| 194 | unsigned NumElements = |
| 195 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 196 | unsigned PHIReg = ValueMap[PN]; |
| 197 | assert(PHIReg &&"PHI node does not have an assigned virtual register!"); |
| 198 | for (unsigned i = 0; i != NumElements; ++i) |
| 199 | BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i); |
| 200 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | //===----------------------------------------------------------------------===// |
| 207 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 208 | /// implementation that is parameterized by a TargetLowering object. |
| 209 | /// Also, targets can overload any lowering method. |
| 210 | /// |
| 211 | namespace llvm { |
| 212 | class SelectionDAGLowering { |
| 213 | MachineBasicBlock *CurMBB; |
| 214 | |
| 215 | std::map<const Value*, SDOperand> NodeMap; |
| 216 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 217 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 218 | /// them up and then emit token factor nodes when possible. This allows us to |
| 219 | /// get simple disambiguation between loads without worrying about alias |
| 220 | /// analysis. |
| 221 | std::vector<SDOperand> PendingLoads; |
| 222 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 223 | public: |
| 224 | // TLI - This is information that describes the available target features we |
| 225 | // need for lowering. This indicates when operations are unavailable, |
| 226 | // implemented with a libcall, etc. |
| 227 | TargetLowering &TLI; |
| 228 | SelectionDAG &DAG; |
| 229 | const TargetData &TD; |
| 230 | |
| 231 | /// FuncInfo - Information about the function as a whole. |
| 232 | /// |
| 233 | FunctionLoweringInfo &FuncInfo; |
| 234 | |
| 235 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 236 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 237 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
| 238 | FuncInfo(funcinfo) { |
| 239 | } |
| 240 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 241 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 242 | /// |
| 243 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 244 | if (PendingLoads.empty()) |
| 245 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 246 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 247 | if (PendingLoads.size() == 1) { |
| 248 | SDOperand Root = PendingLoads[0]; |
| 249 | DAG.setRoot(Root); |
| 250 | PendingLoads.clear(); |
| 251 | return Root; |
| 252 | } |
| 253 | |
| 254 | // Otherwise, we have to make a token factor node. |
| 255 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads); |
| 256 | PendingLoads.clear(); |
| 257 | DAG.setRoot(Root); |
| 258 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 261 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 262 | |
| 263 | void visit(unsigned Opcode, User &I) { |
| 264 | switch (Opcode) { |
| 265 | default: assert(0 && "Unknown instruction type encountered!"); |
| 266 | abort(); |
| 267 | // Build the switch statement using the Instruction.def file. |
| 268 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 269 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 270 | #include "llvm/Instruction.def" |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 275 | |
| 276 | |
| 277 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 278 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 279 | } |
| 280 | |
| 281 | SDOperand getValue(const Value *V) { |
| 282 | SDOperand &N = NodeMap[V]; |
| 283 | if (N.Val) return N; |
| 284 | |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 285 | const Type *VTy = V->getType(); |
| 286 | MVT::ValueType VT = TLI.getValueType(VTy); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 287 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) |
| 288 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 289 | visit(CE->getOpcode(), *CE); |
| 290 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 291 | return N; |
| 292 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 293 | return N = DAG.getGlobalAddress(GV, VT); |
| 294 | } else if (isa<ConstantPointerNull>(C)) { |
| 295 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 296 | } else if (isa<UndefValue>(C)) { |
Nate Begeman | b882752 | 2005-04-12 23:12:17 +0000 | [diff] [blame] | 297 | return N = DAG.getNode(ISD::UNDEF, VT); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 298 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 299 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 300 | } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) { |
| 301 | unsigned NumElements = PTy->getNumElements(); |
| 302 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
| 303 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
| 304 | |
| 305 | // Now that we know the number and type of the elements, push a |
| 306 | // Constant or ConstantFP node onto the ops list for each element of |
| 307 | // the packed constant. |
| 308 | std::vector<SDOperand> Ops; |
| 309 | for (unsigned i = 0; i < NumElements; ++i) { |
| 310 | const Constant *CEl = C->getOperand(i); |
| 311 | if (MVT::isFloatingPoint(PVT)) |
| 312 | Ops.push_back(DAG.getConstantFP(cast<ConstantFP>(CEl)->getValue(), |
| 313 | PVT)); |
| 314 | else |
| 315 | Ops.push_back( |
| 316 | DAG.getConstant(cast<ConstantIntegral>(CEl)->getRawValue(), |
| 317 | PVT)); |
| 318 | } |
| 319 | // Handle the case where we have a 1-element vector, in which |
| 320 | // case we want to immediately turn it into a scalar constant. |
Nate Begeman | cc827e6 | 2005-12-07 19:48:11 +0000 | [diff] [blame] | 321 | if (Ops.size() == 1) { |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 322 | return N = Ops[0]; |
Nate Begeman | cc827e6 | 2005-12-07 19:48:11 +0000 | [diff] [blame] | 323 | } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 324 | return N = DAG.getNode(ISD::ConstantVec, TVT, Ops); |
| 325 | } else { |
| 326 | // If the packed type isn't legal, then create a ConstantVec node with |
| 327 | // generic Vector type instead. |
| 328 | return N = DAG.getNode(ISD::ConstantVec, MVT::Vector, Ops); |
| 329 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 330 | } else { |
| 331 | // Canonicalize all constant ints to be unsigned. |
| 332 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT); |
| 333 | } |
| 334 | |
| 335 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 336 | std::map<const AllocaInst*, int>::iterator SI = |
| 337 | FuncInfo.StaticAllocaMap.find(AI); |
| 338 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 339 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 340 | } |
| 341 | |
| 342 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 343 | FuncInfo.ValueMap.find(V); |
| 344 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
Chris Lattner | c8ea3c4 | 2005-01-16 02:23:07 +0000 | [diff] [blame] | 345 | |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 346 | unsigned InReg = VMI->second; |
| 347 | |
| 348 | // If this type is not legal, make it so now. |
| 349 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 350 | |
| 351 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 352 | if (DestVT < VT) { |
| 353 | // Source must be expanded. This input value is actually coming from the |
| 354 | // register pair VMI->second and VMI->second+1. |
| 355 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 356 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 357 | } else { |
| 358 | if (DestVT > VT) { // Promotion case |
| 359 | if (MVT::isFloatingPoint(VT)) |
| 360 | N = DAG.getNode(ISD::FP_ROUND, VT, N); |
| 361 | else |
| 362 | N = DAG.getNode(ISD::TRUNCATE, VT, N); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return N; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 370 | SDOperand &N = NodeMap[V]; |
| 371 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 372 | return N = NewN; |
| 373 | } |
| 374 | |
| 375 | // Terminator instructions. |
| 376 | void visitRet(ReturnInst &I); |
| 377 | void visitBr(BranchInst &I); |
| 378 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 379 | |
| 380 | // These all get lowered before this pass. |
| 381 | void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); } |
| 382 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 383 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 384 | |
| 385 | // |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 386 | void visitBinary(User &I, unsigned IntOp, unsigned FPOp, unsigned VecOp); |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 387 | void visitShift(User &I, unsigned Opcode); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 388 | void visitAdd(User &I) { |
| 389 | visitBinary(I, ISD::ADD, ISD::FADD, ISD::VADD); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 390 | } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 391 | void visitSub(User &I); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 392 | void visitMul(User &I) { |
| 393 | visitBinary(I, ISD::MUL, ISD::FMUL, ISD::VMUL); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 395 | void visitDiv(User &I) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 396 | const Type *Ty = I.getType(); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 397 | visitBinary(I, Ty->isSigned() ? ISD::SDIV : ISD::UDIV, ISD::FDIV, 0); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 398 | } |
| 399 | void visitRem(User &I) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 400 | const Type *Ty = I.getType(); |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 401 | visitBinary(I, Ty->isSigned() ? ISD::SREM : ISD::UREM, ISD::FREM, 0); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 402 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 403 | void visitAnd(User &I) { visitBinary(I, ISD::AND, 0, 0); } |
| 404 | void visitOr (User &I) { visitBinary(I, ISD::OR, 0, 0); } |
| 405 | void visitXor(User &I) { visitBinary(I, ISD::XOR, 0, 0); } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 406 | void visitShl(User &I) { visitShift(I, ISD::SHL); } |
| 407 | void visitShr(User &I) { |
| 408 | visitShift(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc); |
| 412 | void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); } |
| 413 | void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); } |
| 414 | void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); } |
| 415 | void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); } |
| 416 | void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); } |
| 417 | void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); } |
| 418 | |
| 419 | void visitGetElementPtr(User &I); |
| 420 | void visitCast(User &I); |
| 421 | void visitSelect(User &I); |
| 422 | // |
| 423 | |
| 424 | void visitMalloc(MallocInst &I); |
| 425 | void visitFree(FreeInst &I); |
| 426 | void visitAlloca(AllocaInst &I); |
| 427 | void visitLoad(LoadInst &I); |
| 428 | void visitStore(StoreInst &I); |
| 429 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 430 | void visitCall(CallInst &I); |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 431 | const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 433 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 434 | void visitVAArg(VAArgInst &I); |
| 435 | void visitVAEnd(CallInst &I); |
| 436 | void visitVACopy(CallInst &I); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 437 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 439 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 440 | |
| 441 | void visitUserOp1(Instruction &I) { |
| 442 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 443 | abort(); |
| 444 | } |
| 445 | void visitUserOp2(Instruction &I) { |
| 446 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 447 | abort(); |
| 448 | } |
| 449 | }; |
| 450 | } // end namespace llvm |
| 451 | |
| 452 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 453 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 454 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
| 458 | SDOperand Op1 = getValue(I.getOperand(0)); |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 459 | MVT::ValueType TmpVT; |
| 460 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 461 | switch (Op1.getValueType()) { |
| 462 | default: assert(0 && "Unknown value type!"); |
| 463 | case MVT::i1: |
| 464 | case MVT::i8: |
| 465 | case MVT::i16: |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 466 | case MVT::i32: |
| 467 | // If this is a machine where 32-bits is legal or expanded, promote to |
| 468 | // 32-bits, otherwise, promote to 64-bits. |
| 469 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 470 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 471 | else |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 472 | TmpVT = MVT::i32; |
| 473 | |
| 474 | // Extend integer types to result type. |
| 475 | if (I.getOperand(0)->getType()->isSigned()) |
| 476 | Op1 = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, Op1); |
| 477 | else |
| 478 | Op1 = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, Op1); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 479 | break; |
| 480 | case MVT::f32: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 481 | case MVT::i64: |
| 482 | case MVT::f64: |
| 483 | break; // No extension needed! |
| 484 | } |
Nate Begeman | 4a95945 | 2005-10-18 23:23:37 +0000 | [diff] [blame] | 485 | // Allow targets to lower this further to meet ABI requirements |
| 486 | DAG.setRoot(TLI.LowerReturnTo(getRoot(), Op1, DAG)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 490 | // Update machine-CFG edges. |
| 491 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 492 | |
| 493 | // Figure out which block is immediately after the current one. |
| 494 | MachineBasicBlock *NextBlock = 0; |
| 495 | MachineFunction::iterator BBI = CurMBB; |
| 496 | if (++BBI != CurMBB->getParent()->end()) |
| 497 | NextBlock = BBI; |
| 498 | |
| 499 | if (I.isUnconditional()) { |
| 500 | // If this is not a fall-through branch, emit the branch. |
| 501 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 502 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 503 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 504 | } else { |
| 505 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 506 | |
| 507 | SDOperand Cond = getValue(I.getCondition()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 508 | if (Succ1MBB == NextBlock) { |
| 509 | // If the condition is false, fall through. This means we should branch |
| 510 | // if the condition is true to Succ #0. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 511 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 512 | Cond, DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 513 | } else if (Succ0MBB == NextBlock) { |
| 514 | // If the condition is true, fall through. This means we should branch if |
| 515 | // the condition is false to Succ #1. Invert the condition first. |
| 516 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 517 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 518 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 519 | Cond, DAG.getBasicBlock(Succ1MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 520 | } else { |
Chris Lattner | e7ccd4a | 2005-04-09 03:30:29 +0000 | [diff] [blame] | 521 | std::vector<SDOperand> Ops; |
| 522 | Ops.push_back(getRoot()); |
| 523 | Ops.push_back(Cond); |
| 524 | Ops.push_back(DAG.getBasicBlock(Succ0MBB)); |
| 525 | Ops.push_back(DAG.getBasicBlock(Succ1MBB)); |
| 526 | DAG.setRoot(DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 531 | void SelectionDAGLowering::visitSub(User &I) { |
| 532 | // -0.0 - X --> fneg |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 533 | if (I.getType()->isFloatingPoint()) { |
| 534 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 535 | if (CFP->isExactlyValue(-0.0)) { |
| 536 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 537 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 538 | return; |
| 539 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 540 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 541 | visitBinary(I, ISD::SUB, ISD::FSUB, ISD::VSUB); |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 544 | void SelectionDAGLowering::visitBinary(User &I, unsigned IntOp, unsigned FPOp, |
| 545 | unsigned VecOp) { |
| 546 | const Type *Ty = I.getType(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 547 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 548 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 549 | |
Chris Lattner | b67eb91 | 2005-11-19 18:40:42 +0000 | [diff] [blame] | 550 | if (Ty->isIntegral()) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 551 | setValue(&I, DAG.getNode(IntOp, Op1.getValueType(), Op1, Op2)); |
| 552 | } else if (Ty->isFloatingPoint()) { |
| 553 | setValue(&I, DAG.getNode(FPOp, Op1.getValueType(), Op1, Op2)); |
| 554 | } else { |
| 555 | const PackedType *PTy = cast<PackedType>(Ty); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 556 | unsigned NumElements = PTy->getNumElements(); |
| 557 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 558 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 559 | |
| 560 | // Immediately scalarize packed types containing only one element, so that |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 561 | // the Legalize pass does not have to deal with them. Similarly, if the |
| 562 | // abstract vector is going to turn into one that the target natively |
| 563 | // supports, generate that type now so that Legalize doesn't have to deal |
| 564 | // with that either. These steps ensure that Legalize only has to handle |
| 565 | // vector types in its Expand case. |
| 566 | unsigned Opc = MVT::isFloatingPoint(PVT) ? FPOp : IntOp; |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 567 | if (NumElements == 1) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 568 | setValue(&I, DAG.getNode(Opc, PVT, Op1, Op2)); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 569 | } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 570 | setValue(&I, DAG.getNode(Opc, TVT, Op1, Op2)); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 571 | } else { |
| 572 | SDOperand Num = DAG.getConstant(NumElements, MVT::i32); |
| 573 | SDOperand Typ = DAG.getValueType(PVT); |
Nate Begeman | ab48be3 | 2005-11-22 18:16:00 +0000 | [diff] [blame] | 574 | setValue(&I, DAG.getNode(VecOp, MVT::Vector, Op1, Op2, Num, Typ)); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 575 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 576 | } |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 577 | } |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 578 | |
Nate Begeman | e21ea61 | 2005-11-18 07:42:56 +0000 | [diff] [blame] | 579 | void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) { |
| 580 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 581 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 582 | |
| 583 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 584 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 585 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 586 | } |
| 587 | |
| 588 | void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode, |
| 589 | ISD::CondCode UnsignedOpcode) { |
| 590 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 591 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 592 | ISD::CondCode Opcode = SignedOpcode; |
| 593 | if (I.getOperand(0)->getType()->isUnsigned()) |
| 594 | Opcode = UnsignedOpcode; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 595 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | void SelectionDAGLowering::visitSelect(User &I) { |
| 599 | SDOperand Cond = getValue(I.getOperand(0)); |
| 600 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 601 | SDOperand FalseVal = getValue(I.getOperand(2)); |
| 602 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 603 | TrueVal, FalseVal)); |
| 604 | } |
| 605 | |
| 606 | void SelectionDAGLowering::visitCast(User &I) { |
| 607 | SDOperand N = getValue(I.getOperand(0)); |
| 608 | MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType()); |
| 609 | MVT::ValueType DestTy = TLI.getValueType(I.getType()); |
| 610 | |
| 611 | if (N.getValueType() == DestTy) { |
| 612 | setValue(&I, N); // noop cast. |
Chris Lattner | ef311aa | 2005-05-09 22:17:13 +0000 | [diff] [blame] | 613 | } else if (DestTy == MVT::i1) { |
| 614 | // Cast to bool is a comparison against zero, not truncation to zero. |
| 615 | SDOperand Zero = isInteger(SrcTy) ? DAG.getConstant(0, N.getValueType()) : |
| 616 | DAG.getConstantFP(0.0, N.getValueType()); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 617 | setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 618 | } else if (isInteger(SrcTy)) { |
| 619 | if (isInteger(DestTy)) { // Int -> Int cast |
| 620 | if (DestTy < SrcTy) // Truncating cast? |
| 621 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N)); |
| 622 | else if (I.getOperand(0)->getType()->isSigned()) |
| 623 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N)); |
| 624 | else |
| 625 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N)); |
| 626 | } else { // Int -> FP cast |
| 627 | if (I.getOperand(0)->getType()->isSigned()) |
| 628 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N)); |
| 629 | else |
| 630 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N)); |
| 631 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 632 | } else { |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 633 | assert(isFloatingPoint(SrcTy) && "Unknown value type!"); |
| 634 | if (isFloatingPoint(DestTy)) { // FP -> FP cast |
| 635 | if (DestTy < SrcTy) // Rounding cast? |
| 636 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N)); |
| 637 | else |
| 638 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N)); |
| 639 | } else { // FP -> Int cast. |
| 640 | if (I.getType()->isSigned()) |
| 641 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N)); |
| 642 | else |
| 643 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N)); |
| 644 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 649 | SDOperand N = getValue(I.getOperand(0)); |
| 650 | const Type *Ty = I.getOperand(0)->getType(); |
| 651 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 652 | |
| 653 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 654 | OI != E; ++OI) { |
| 655 | Value *Idx = *OI; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 656 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 657 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 658 | if (Field) { |
| 659 | // N = N + Offset |
| 660 | uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 661 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 662 | getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 663 | } |
| 664 | Ty = StTy->getElementType(Field); |
| 665 | } else { |
| 666 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 668 | // If this is a constant subscript, handle it quickly. |
| 669 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 670 | if (CI->getRawValue() == 0) continue; |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 671 | |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 672 | uint64_t Offs; |
| 673 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 674 | Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 675 | else |
| 676 | Offs = TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 677 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs)); |
| 678 | continue; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 679 | } |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 680 | |
| 681 | // N = N + Idx * ElementSize; |
| 682 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 683 | SDOperand IdxN = getValue(Idx); |
| 684 | |
| 685 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 686 | // it. |
| 687 | if (IdxN.getValueType() < N.getValueType()) { |
| 688 | if (Idx->getType()->isSigned()) |
| 689 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
| 690 | else |
| 691 | IdxN = DAG.getNode(ISD::ZERO_EXTEND, N.getValueType(), IdxN); |
| 692 | } else if (IdxN.getValueType() > N.getValueType()) |
| 693 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 694 | |
| 695 | // If this is a multiply by a power of two, turn it into a shl |
| 696 | // immediately. This is a very common case. |
| 697 | if (isPowerOf2_64(ElementSize)) { |
| 698 | unsigned Amt = Log2_64(ElementSize); |
| 699 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 6b2d696 | 2005-11-09 16:50:40 +0000 | [diff] [blame] | 700 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 7c0104b | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 701 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | SDOperand Scale = getIntPtrConstant(ElementSize); |
| 706 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 707 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | setValue(&I, N); |
| 711 | } |
| 712 | |
| 713 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 714 | // If this is a fixed sized alloca in the entry block of the function, |
| 715 | // allocate it statically on the stack. |
| 716 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 717 | return; // getValue will auto-populate this. |
| 718 | |
| 719 | const Type *Ty = I.getAllocatedType(); |
| 720 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | ae232e7 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 721 | unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 722 | I.getAlignment()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 723 | |
| 724 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 725 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 726 | if (IntPtr < AllocSize.getValueType()) |
| 727 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 728 | else if (IntPtr > AllocSize.getValueType()) |
| 729 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 731 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 732 | getIntPtrConstant(TySize)); |
| 733 | |
| 734 | // Handle alignment. If the requested alignment is less than or equal to the |
| 735 | // stack alignment, ignore it and round the size of the allocation up to the |
| 736 | // stack alignment size. If the size is greater than the stack alignment, we |
| 737 | // note this in the DYNAMIC_STACKALLOC node. |
| 738 | unsigned StackAlign = |
| 739 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 740 | if (Align <= StackAlign) { |
| 741 | Align = 0; |
| 742 | // Add SA-1 to the size. |
| 743 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 744 | getIntPtrConstant(StackAlign-1)); |
| 745 | // Mask out the low bits for alignment purposes. |
| 746 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 747 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 748 | } |
| 749 | |
Chris Lattner | adf6c2a | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 750 | std::vector<MVT::ValueType> VTs; |
| 751 | VTs.push_back(AllocSize.getValueType()); |
| 752 | VTs.push_back(MVT::Other); |
| 753 | std::vector<SDOperand> Ops; |
| 754 | Ops.push_back(getRoot()); |
| 755 | Ops.push_back(AllocSize); |
| 756 | Ops.push_back(getIntPtrConstant(Align)); |
| 757 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 758 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 759 | |
| 760 | // Inform the Frame Information that we have just allocated a variable-sized |
| 761 | // object. |
| 762 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 763 | } |
| 764 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 765 | /// getStringValue - Turn an LLVM constant pointer that eventually points to a |
| 766 | /// global into a string value. Return an empty string if we can't do it. |
| 767 | /// |
| 768 | static std::string getStringValue(Value *V, unsigned Offset = 0) { |
| 769 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 770 | if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) { |
| 771 | ConstantArray *Init = cast<ConstantArray>(GV->getInitializer()); |
| 772 | if (Init->isString()) { |
| 773 | std::string Result = Init->getAsString(); |
| 774 | if (Offset < Result.size()) { |
| 775 | // If we are pointing INTO The string, erase the beginning... |
| 776 | Result.erase(Result.begin(), Result.begin()+Offset); |
| 777 | |
| 778 | // Take off the null terminator, and any string fragments after it. |
| 779 | std::string::size_type NullPos = Result.find_first_of((char)0); |
| 780 | if (NullPos != std::string::npos) |
| 781 | Result.erase(Result.begin()+NullPos, Result.end()); |
| 782 | return Result; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | } else if (Constant *C = dyn_cast<Constant>(V)) { |
| 787 | if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 788 | return getStringValue(GV, Offset); |
| 789 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 790 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 791 | // Turn a gep into the specified offset. |
| 792 | if (CE->getNumOperands() == 3 && |
| 793 | cast<Constant>(CE->getOperand(1))->isNullValue() && |
| 794 | isa<ConstantInt>(CE->getOperand(2))) { |
| 795 | return getStringValue(CE->getOperand(0), |
| 796 | Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue()); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | return ""; |
| 802 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 803 | |
| 804 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 805 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 806 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 807 | SDOperand Root; |
| 808 | if (I.isVolatile()) |
| 809 | Root = getRoot(); |
| 810 | else { |
| 811 | // Do not serialize non-volatile loads against each other. |
| 812 | Root = DAG.getRoot(); |
| 813 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 814 | |
| 815 | const Type *Ty = I.getType(); |
| 816 | SDOperand L; |
| 817 | |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 818 | if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) { |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 819 | unsigned NumElements = PTy->getNumElements(); |
| 820 | MVT::ValueType PVT = TLI.getValueType(PTy->getElementType()); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 821 | MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 822 | |
| 823 | // Immediately scalarize packed types containing only one element, so that |
| 824 | // the Legalize pass does not have to deal with them. |
| 825 | if (NumElements == 1) { |
| 826 | L = DAG.getLoad(PVT, Root, Ptr, DAG.getSrcValue(I.getOperand(0))); |
Nate Begeman | f43a3ca | 2005-11-30 08:22:07 +0000 | [diff] [blame] | 827 | } else if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 828 | L = DAG.getLoad(TVT, Root, Ptr, DAG.getSrcValue(I.getOperand(0))); |
Nate Begeman | 4ef3b81 | 2005-11-22 01:29:36 +0000 | [diff] [blame] | 829 | } else { |
| 830 | L = DAG.getVecLoad(NumElements, PVT, Root, Ptr, |
| 831 | DAG.getSrcValue(I.getOperand(0))); |
| 832 | } |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 833 | } else { |
| 834 | L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, |
| 835 | DAG.getSrcValue(I.getOperand(0))); |
| 836 | } |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 837 | setValue(&I, L); |
| 838 | |
| 839 | if (I.isVolatile()) |
| 840 | DAG.setRoot(L.getValue(1)); |
| 841 | else |
| 842 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | |
| 846 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 847 | Value *SrcV = I.getOperand(0); |
| 848 | SDOperand Src = getValue(SrcV); |
| 849 | SDOperand Ptr = getValue(I.getOperand(1)); |
Chris Lattner | 369e6db | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 850 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 851 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 854 | /// visitIntrinsicCall - Lower the call to the specified intrinsic function. If |
| 855 | /// we want to emit this as a call to a named external function, return the name |
| 856 | /// otherwise lower it and return null. |
| 857 | const char * |
| 858 | SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { |
| 859 | switch (Intrinsic) { |
| 860 | case Intrinsic::vastart: visitVAStart(I); return 0; |
| 861 | case Intrinsic::vaend: visitVAEnd(I); return 0; |
| 862 | case Intrinsic::vacopy: visitVACopy(I); return 0; |
| 863 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return 0; |
| 864 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return 0; |
| 865 | case Intrinsic::setjmp: |
| 866 | return "_setjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 867 | break; |
| 868 | case Intrinsic::longjmp: |
| 869 | return "_longjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 870 | break; |
| 871 | case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return 0; |
| 872 | case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return 0; |
| 873 | case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return 0; |
| 874 | |
| 875 | case Intrinsic::readport: |
| 876 | case Intrinsic::readio: { |
| 877 | std::vector<MVT::ValueType> VTs; |
| 878 | VTs.push_back(TLI.getValueType(I.getType())); |
| 879 | VTs.push_back(MVT::Other); |
| 880 | std::vector<SDOperand> Ops; |
| 881 | Ops.push_back(getRoot()); |
| 882 | Ops.push_back(getValue(I.getOperand(1))); |
| 883 | SDOperand Tmp = DAG.getNode(Intrinsic == Intrinsic::readport ? |
| 884 | ISD::READPORT : ISD::READIO, VTs, Ops); |
| 885 | |
| 886 | setValue(&I, Tmp); |
| 887 | DAG.setRoot(Tmp.getValue(1)); |
| 888 | return 0; |
| 889 | } |
| 890 | case Intrinsic::writeport: |
| 891 | case Intrinsic::writeio: |
| 892 | DAG.setRoot(DAG.getNode(Intrinsic == Intrinsic::writeport ? |
| 893 | ISD::WRITEPORT : ISD::WRITEIO, MVT::Other, |
| 894 | getRoot(), getValue(I.getOperand(1)), |
| 895 | getValue(I.getOperand(2)))); |
| 896 | return 0; |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 897 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 898 | case Intrinsic::dbg_stoppoint: { |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 899 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 900 | return "llvm_debugger_stop"; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 901 | |
| 902 | std::string fname = "<unknown>"; |
| 903 | std::vector<SDOperand> Ops; |
| 904 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 905 | // Input Chain |
| 906 | Ops.push_back(getRoot()); |
| 907 | |
| 908 | // line number |
| 909 | Ops.push_back(getValue(I.getOperand(2))); |
| 910 | |
| 911 | // column |
| 912 | Ops.push_back(getValue(I.getOperand(3))); |
| 913 | |
Chris Lattner | 86cb643 | 2005-12-13 17:40:33 +0000 | [diff] [blame] | 914 | // filename/working dir |
| 915 | // Pull the filename out of the the compilation unit. |
| 916 | const GlobalVariable *cunit = dyn_cast<GlobalVariable>(I.getOperand(4)); |
| 917 | if (cunit && cunit->hasInitializer()) { |
| 918 | if (ConstantStruct *CS = |
| 919 | dyn_cast<ConstantStruct>(cunit->getInitializer())) { |
| 920 | if (CS->getNumOperands() > 0) { |
| 921 | Ops.push_back(DAG.getString(getStringValue(CS->getOperand(4)))); |
| 922 | Ops.push_back(DAG.getString(getStringValue(CS->getOperand(3)))); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (Ops.size() == 5) // Found filename/workingdir. |
| 928 | DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops)); |
Chris Lattner | d67b3a8 | 2005-12-03 18:50:48 +0000 | [diff] [blame] | 929 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 930 | return 0; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 931 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 932 | case Intrinsic::dbg_region_start: |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 933 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 934 | return "llvm_dbg_region_start"; |
| 935 | if (I.getType() != Type::VoidTy) |
| 936 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 937 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 938 | case Intrinsic::dbg_region_end: |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 939 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 940 | return "llvm_dbg_region_end"; |
| 941 | if (I.getType() != Type::VoidTy) |
| 942 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 943 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 944 | case Intrinsic::dbg_func_start: |
Chris Lattner | b1a5a5c | 2005-11-16 07:22:30 +0000 | [diff] [blame] | 945 | if (TLI.getTargetMachine().getIntrinsicLowering().EmitDebugFunctions()) |
| 946 | return "llvm_dbg_subprogram"; |
| 947 | if (I.getType() != Type::VoidTy) |
| 948 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 949 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 950 | case Intrinsic::dbg_declare: |
| 951 | if (I.getType() != Type::VoidTy) |
| 952 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 953 | return 0; |
| 954 | |
| 955 | case Intrinsic::isunordered: |
| 956 | setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)), |
| 957 | getValue(I.getOperand(2)), ISD::SETUO)); |
| 958 | return 0; |
| 959 | |
| 960 | case Intrinsic::sqrt: |
| 961 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 962 | getValue(I.getOperand(1)).getValueType(), |
| 963 | getValue(I.getOperand(1)))); |
| 964 | return 0; |
| 965 | case Intrinsic::pcmarker: { |
| 966 | SDOperand Tmp = getValue(I.getOperand(1)); |
| 967 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
| 968 | return 0; |
| 969 | } |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 970 | case Intrinsic::readcyclecounter: { |
| 971 | std::vector<MVT::ValueType> VTs; |
| 972 | VTs.push_back(MVT::i64); |
| 973 | VTs.push_back(MVT::Other); |
| 974 | std::vector<SDOperand> Ops; |
| 975 | Ops.push_back(getRoot()); |
| 976 | SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, VTs, Ops); |
| 977 | setValue(&I, Tmp); |
| 978 | DAG.setRoot(Tmp.getValue(1)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 979 | return 0; |
Andrew Lenharth | 8b91c77 | 2005-11-11 22:48:54 +0000 | [diff] [blame] | 980 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 981 | case Intrinsic::cttz: |
| 982 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 983 | getValue(I.getOperand(1)).getValueType(), |
| 984 | getValue(I.getOperand(1)))); |
| 985 | return 0; |
| 986 | case Intrinsic::ctlz: |
| 987 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 988 | getValue(I.getOperand(1)).getValueType(), |
| 989 | getValue(I.getOperand(1)))); |
| 990 | return 0; |
| 991 | case Intrinsic::ctpop: |
| 992 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 993 | getValue(I.getOperand(1)).getValueType(), |
| 994 | getValue(I.getOperand(1)))); |
| 995 | return 0; |
Chris Lattner | ac22c83 | 2005-12-12 22:51:16 +0000 | [diff] [blame] | 996 | case Intrinsic::prefetch: |
| 997 | // FIXME: Currently discarding prefetches. |
| 998 | return 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 999 | default: |
| 1000 | std::cerr << I; |
| 1001 | assert(0 && "This intrinsic is not implemented yet!"); |
| 1002 | return 0; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1007 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 1008 | const char *RenameFn = 0; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1009 | if (Function *F = I.getCalledFunction()) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1010 | if (F->isExternal()) |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1011 | if (unsigned IID = F->getIntrinsicID()) { |
| 1012 | RenameFn = visitIntrinsicCall(I, IID); |
| 1013 | if (!RenameFn) |
| 1014 | return; |
| 1015 | } else { // Not an LLVM intrinsic. |
| 1016 | const std::string &Name = F->getName(); |
| 1017 | if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) { |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1018 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1019 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1020 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1021 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 1022 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 1023 | return; |
| 1024 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1025 | } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1026 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1027 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1028 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1029 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1030 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 1031 | return; |
| 1032 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1033 | } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) { |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1034 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 1035 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 1036 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1037 | SDOperand Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 1038 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 1039 | return; |
| 1040 | } |
| 1041 | } |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 1042 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1043 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 1045 | SDOperand Callee; |
| 1046 | if (!RenameFn) |
| 1047 | Callee = getValue(I.getOperand(0)); |
| 1048 | else |
| 1049 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1050 | std::vector<std::pair<SDOperand, const Type*> > Args; |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1051 | Args.reserve(I.getNumOperands()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1052 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1053 | Value *Arg = I.getOperand(i); |
| 1054 | SDOperand ArgNode = getValue(Arg); |
| 1055 | Args.push_back(std::make_pair(ArgNode, Arg->getType())); |
| 1056 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1057 | |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 1058 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 1059 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1061 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 9092fa3 | 2005-05-12 19:56:57 +0000 | [diff] [blame] | 1062 | TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(), |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1063 | I.isTailCall(), Callee, Args, DAG); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1064 | if (I.getType() != Type::VoidTy) |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1065 | setValue(&I, Result.first); |
| 1066 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 1070 | SDOperand Src = getValue(I.getOperand(0)); |
| 1071 | |
| 1072 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 1073 | |
| 1074 | if (IntPtr < Src.getValueType()) |
| 1075 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 1076 | else if (IntPtr > Src.getValueType()) |
| 1077 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1078 | |
| 1079 | // Scale the source by the type size. |
| 1080 | uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType()); |
| 1081 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 1082 | Src, getIntPtrConstant(ElementSize)); |
| 1083 | |
| 1084 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1085 | Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType())); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1086 | |
| 1087 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1088 | TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1089 | DAG.getExternalSymbol("malloc", IntPtr), |
| 1090 | Args, DAG); |
| 1091 | setValue(&I, Result.first); // Pointers always fit in registers |
| 1092 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 1096 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1097 | Args.push_back(std::make_pair(getValue(I.getOperand(0)), |
| 1098 | TLI.getTargetData().getIntPtrType())); |
| 1099 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1100 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1101 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 1102 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 1103 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 1106 | // InsertAtEndOfBasicBlock - This method should be implemented by targets that |
| 1107 | // mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
| 1108 | // instructions are special in various ways, which require special support to |
| 1109 | // insert. The specified MachineInstr is created but not inserted into any |
| 1110 | // basic blocks, and the scheduler passes ownership of it to this method. |
| 1111 | MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1112 | MachineBasicBlock *MBB) { |
| 1113 | std::cerr << "If a target marks an instruction with " |
| 1114 | "'usesCustomDAGSchedInserter', it must implement " |
| 1115 | "TargetLowering::InsertAtEndOfBasicBlock!\n"; |
| 1116 | abort(); |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Nate Begeman | 4a95945 | 2005-10-18 23:23:37 +0000 | [diff] [blame] | 1120 | SDOperand TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op, |
| 1121 | SelectionDAG &DAG) { |
| 1122 | return DAG.getNode(ISD::RET, MVT::Other, Chain, Op); |
| 1123 | } |
| 1124 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1125 | SDOperand TargetLowering::LowerVAStart(SDOperand Chain, |
| 1126 | SDOperand VAListP, Value *VAListV, |
| 1127 | SelectionDAG &DAG) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1128 | // We have no sane default behavior, just emit a useful error message and bail |
| 1129 | // out. |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1130 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1131 | abort(); |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1132 | return SDOperand(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1135 | SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV, |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1136 | SelectionDAG &DAG) { |
| 1137 | // Default to a noop. |
| 1138 | return Chain; |
| 1139 | } |
| 1140 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1141 | SDOperand TargetLowering::LowerVACopy(SDOperand Chain, |
| 1142 | SDOperand SrcP, Value *SrcV, |
| 1143 | SDOperand DestP, Value *DestV, |
| 1144 | SelectionDAG &DAG) { |
| 1145 | // Default to copying the input list. |
| 1146 | SDOperand Val = DAG.getLoad(getPointerTy(), Chain, |
| 1147 | SrcP, DAG.getSrcValue(SrcV)); |
Andrew Lenharth | 213e557 | 2005-06-22 21:04:42 +0000 | [diff] [blame] | 1148 | SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1149 | Val, DestP, DAG.getSrcValue(DestV)); |
| 1150 | return Result; |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | std::pair<SDOperand,SDOperand> |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1154 | TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 1155 | const Type *ArgTy, SelectionDAG &DAG) { |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1156 | // We have no sane default behavior, just emit a useful error message and bail |
| 1157 | // out. |
| 1158 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
| 1159 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 1160 | return std::make_pair(SDOperand(), SDOperand()); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1165 | DAG.setRoot(TLI.LowerVAStart(getRoot(), getValue(I.getOperand(1)), |
| 1166 | I.getOperand(1), DAG)); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
| 1170 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1171 | TLI.LowerVAArg(getRoot(), getValue(I.getOperand(0)), I.getOperand(0), |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1172 | I.getType(), DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1173 | setValue(&I, Result.first); |
| 1174 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1178 | DAG.setRoot(TLI.LowerVAEnd(getRoot(), getValue(I.getOperand(1)), |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1179 | I.getOperand(1), DAG)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 1183 | SDOperand Result = |
| 1184 | TLI.LowerVACopy(getRoot(), getValue(I.getOperand(2)), I.getOperand(2), |
| 1185 | getValue(I.getOperand(1)), I.getOperand(1), DAG); |
| 1186 | DAG.setRoot(Result); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1189 | |
| 1190 | // It is always conservatively correct for llvm.returnaddress and |
| 1191 | // llvm.frameaddress to return 0. |
| 1192 | std::pair<SDOperand, SDOperand> |
| 1193 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 1194 | unsigned Depth, SelectionDAG &DAG) { |
| 1195 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 1198 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1199 | assert(0 && "LowerOperation not implemented for this target!"); |
| 1200 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 1201 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1204 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
| 1205 | unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue(); |
| 1206 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1207 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1208 | setValue(&I, Result.first); |
| 1209 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1212 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
Reid Spencer | 6ff7240 | 2005-11-30 05:21:10 +0000 | [diff] [blame] | 1213 | #if 0 |
| 1214 | // If the size of the cpy/move/set is constant (known) |
| 1215 | if (ConstantUInt* op3 = dyn_cast<ConstantUInt>(I.getOperand(3))) { |
| 1216 | uint64_t size = op3->getValue(); |
| 1217 | switch (Op) { |
| 1218 | case ISD::MEMSET: |
| 1219 | if (size <= TLI.getMaxStoresPerMemSet()) { |
| 1220 | if (ConstantUInt* op4 = dyn_cast<ConstantUInt>(I.getOperand(4))) { |
| 1221 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
| 1222 | uint64_t align = op4.getValue(); |
| 1223 | while (size > align) { |
| 1224 | size -=align; |
| 1225 | } |
| 1226 | Value *SrcV = I.getOperand(0); |
| 1227 | SDOperand Src = getValue(SrcV); |
| 1228 | SDOperand Ptr = getValue(I.getOperand(1)); |
| 1229 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
| 1230 | DAG.getSrcValue(I.getOperand(1)))); |
| 1231 | } |
| 1232 | break; |
| 1233 | } |
| 1234 | break; // don't do this optimization, use a normal memset |
| 1235 | case ISD::MEMMOVE: |
| 1236 | case ISD::MEMCPY: |
| 1237 | break; // FIXME: not implemented yet |
| 1238 | } |
| 1239 | } |
| 1240 | #endif |
| 1241 | |
| 1242 | // Non-optimized version |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1243 | std::vector<SDOperand> Ops; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1244 | Ops.push_back(getRoot()); |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1245 | Ops.push_back(getValue(I.getOperand(1))); |
| 1246 | Ops.push_back(getValue(I.getOperand(2))); |
| 1247 | Ops.push_back(getValue(I.getOperand(3))); |
| 1248 | Ops.push_back(getValue(I.getOperand(4))); |
| 1249 | DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1252 | //===----------------------------------------------------------------------===// |
| 1253 | // SelectionDAGISel code |
| 1254 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1255 | |
| 1256 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 1257 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 1258 | } |
| 1259 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1260 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1261 | // FIXME: we only modify the CFG to split critical edges. This |
| 1262 | // updates dom and loop info. |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1263 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1265 | |
| 1266 | /// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset, |
| 1267 | /// casting to the type of GEPI. |
| 1268 | static Value *InsertGEPComputeCode(Value *&V, BasicBlock *BB, Instruction *GEPI, |
| 1269 | Value *Ptr, Value *PtrOffset) { |
| 1270 | if (V) return V; // Already computed. |
| 1271 | |
| 1272 | BasicBlock::iterator InsertPt; |
| 1273 | if (BB == GEPI->getParent()) { |
| 1274 | // If insert into the GEP's block, insert right after the GEP. |
| 1275 | InsertPt = GEPI; |
| 1276 | ++InsertPt; |
| 1277 | } else { |
| 1278 | // Otherwise, insert at the top of BB, after any PHI nodes |
| 1279 | InsertPt = BB->begin(); |
| 1280 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 1281 | } |
| 1282 | |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1283 | // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into |
| 1284 | // BB so that there is only one value live across basic blocks (the cast |
| 1285 | // operand). |
| 1286 | if (CastInst *CI = dyn_cast<CastInst>(Ptr)) |
| 1287 | if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType())) |
| 1288 | Ptr = new CastInst(CI->getOperand(0), CI->getType(), "", InsertPt); |
| 1289 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1290 | // Add the offset, cast it to the right type. |
| 1291 | Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt); |
| 1292 | Ptr = new CastInst(Ptr, GEPI->getType(), "", InsertPt); |
| 1293 | return V = Ptr; |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | /// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction |
| 1298 | /// selection, we want to be a bit careful about some things. In particular, if |
| 1299 | /// we have a GEP instruction that is used in a different block than it is |
| 1300 | /// defined, the addressing expression of the GEP cannot be folded into loads or |
| 1301 | /// stores that use it. In this case, decompose the GEP and move constant |
| 1302 | /// indices into blocks that use it. |
| 1303 | static void OptimizeGEPExpression(GetElementPtrInst *GEPI, |
| 1304 | const TargetData &TD) { |
| 1305 | if (!GEPISelTest) return; |
| 1306 | |
| 1307 | // If this GEP is only used inside the block it is defined in, there is no |
| 1308 | // need to rewrite it. |
| 1309 | bool isUsedOutsideDefBB = false; |
| 1310 | BasicBlock *DefBB = GEPI->getParent(); |
| 1311 | for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end(); |
| 1312 | UI != E; ++UI) { |
| 1313 | if (cast<Instruction>(*UI)->getParent() != DefBB) { |
| 1314 | isUsedOutsideDefBB = true; |
| 1315 | break; |
| 1316 | } |
| 1317 | } |
| 1318 | if (!isUsedOutsideDefBB) return; |
| 1319 | |
| 1320 | // If this GEP has no non-zero constant indices, there is nothing we can do, |
| 1321 | // ignore it. |
| 1322 | bool hasConstantIndex = false; |
| 1323 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 1324 | E = GEPI->op_end(); OI != E; ++OI) { |
| 1325 | if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI)) |
| 1326 | if (CI->getRawValue()) { |
| 1327 | hasConstantIndex = true; |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
Chris Lattner | 3802c25 | 2005-12-11 09:05:13 +0000 | [diff] [blame] | 1331 | // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses. |
| 1332 | if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) return; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the |
| 1335 | // constant offset (which we now know is non-zero) and deal with it later. |
| 1336 | uint64_t ConstantOffset = 0; |
| 1337 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 1338 | Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI); |
| 1339 | const Type *Ty = GEPI->getOperand(0)->getType(); |
| 1340 | |
| 1341 | for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1, |
| 1342 | E = GEPI->op_end(); OI != E; ++OI) { |
| 1343 | Value *Idx = *OI; |
| 1344 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
| 1345 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 1346 | if (Field) |
| 1347 | ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 1348 | Ty = StTy->getElementType(Field); |
| 1349 | } else { |
| 1350 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 1351 | |
| 1352 | // Handle constant subscripts. |
| 1353 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 1354 | if (CI->getRawValue() == 0) continue; |
| 1355 | |
| 1356 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 1357 | ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 1358 | else |
| 1359 | ConstantOffset+=TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | // Ptr = Ptr + Idx * ElementSize; |
| 1364 | |
| 1365 | // Cast Idx to UIntPtrTy if needed. |
| 1366 | Idx = new CastInst(Idx, UIntPtrTy, "", GEPI); |
| 1367 | |
| 1368 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 1369 | // Mask off bits that should not be set. |
| 1370 | ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
| 1371 | Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize); |
| 1372 | |
| 1373 | // Multiply by the element size and add to the base. |
| 1374 | Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI); |
| 1375 | Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | // Make sure that the offset fits in uintptr_t. |
| 1380 | ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits()); |
| 1381 | Constant *PtrOffset = ConstantUInt::get(UIntPtrTy, ConstantOffset); |
| 1382 | |
| 1383 | // Okay, we have now emitted all of the variable index parts to the BB that |
| 1384 | // the GEP is defined in. Loop over all of the using instructions, inserting |
| 1385 | // an "add Ptr, ConstantOffset" into each block that uses it and update the |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1386 | // instruction to use the newly computed value, making GEPI dead. When the |
| 1387 | // user is a load or store instruction address, we emit the add into the user |
| 1388 | // block, otherwise we use a canonical version right next to the gep (these |
| 1389 | // won't be foldable as addresses, so we might as well share the computation). |
| 1390 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1391 | std::map<BasicBlock*,Value*> InsertedExprs; |
| 1392 | while (!GEPI->use_empty()) { |
| 1393 | Instruction *User = cast<Instruction>(GEPI->use_back()); |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1394 | |
| 1395 | // If this use is not foldable into the addressing mode, use a version |
| 1396 | // emitted in the GEP block. |
| 1397 | Value *NewVal; |
| 1398 | if (!isa<LoadInst>(User) && |
| 1399 | (!isa<StoreInst>(User) || User->getOperand(0) == GEPI)) { |
| 1400 | NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI, |
| 1401 | Ptr, PtrOffset); |
| 1402 | } else { |
| 1403 | // Otherwise, insert the code in the User's block so it can be folded into |
| 1404 | // any users in that block. |
| 1405 | NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()], |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1406 | User->getParent(), GEPI, |
| 1407 | Ptr, PtrOffset); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1408 | } |
Chris Lattner | c78b0b7 | 2005-12-08 08:00:12 +0000 | [diff] [blame] | 1409 | User->replaceUsesOfWith(GEPI, NewVal); |
| 1410 | } |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1411 | |
| 1412 | // Finally, the GEP is dead, remove it. |
| 1413 | GEPI->eraseFromParent(); |
| 1414 | } |
| 1415 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1416 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 1417 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 1418 | RegMap = MF.getSSARegMap(); |
| 1419 | DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n"); |
| 1420 | |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1421 | // First, split all critical edges for PHI nodes with incoming values that are |
| 1422 | // constants, this way the load of the constant into a vreg will not be placed |
| 1423 | // into MBBs that are used some other way. |
| 1424 | // |
| 1425 | // In this pass we also look for GEP instructions that are used across basic |
| 1426 | // blocks and rewrites them to improve basic-block-at-a-time selection. |
| 1427 | // |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1428 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 1429 | PHINode *PN; |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1430 | BasicBlock::iterator BBI; |
| 1431 | for (BBI = BB->begin(); (PN = dyn_cast<PHINode>(BBI)); ++BBI) |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1432 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 1433 | if (isa<Constant>(PN->getIncomingValue(i))) |
| 1434 | SplitCriticalEdge(PN->getIncomingBlock(i), BB); |
Chris Lattner | c88d8e9 | 2005-12-05 07:10:48 +0000 | [diff] [blame] | 1435 | |
| 1436 | for (BasicBlock::iterator E = BB->end(); BBI != E; ) |
| 1437 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(BBI++)) |
| 1438 | OptimizeGEPExpression(GEPI, TLI.getTargetData()); |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1439 | } |
Chris Lattner | c9ea6fd | 2005-11-09 19:44:01 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1441 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 1442 | |
| 1443 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 1444 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1446 | return true; |
| 1447 | } |
| 1448 | |
| 1449 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1450 | SDOperand SelectionDAGISel:: |
| 1451 | CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) { |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1452 | SDOperand Op = SDL.getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1453 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1454 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1455 | "Copy from a reg to the same reg!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1456 | |
| 1457 | // If this type is not legal, we must make sure to not create an invalid |
| 1458 | // register use. |
| 1459 | MVT::ValueType SrcVT = Op.getValueType(); |
| 1460 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
| 1461 | SelectionDAG &DAG = SDL.DAG; |
| 1462 | if (SrcVT == DestVT) { |
| 1463 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1464 | } else if (SrcVT < DestVT) { |
| 1465 | // The src value is promoted to the register. |
Chris Lattner | fae59b9 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 1466 | if (MVT::isFloatingPoint(SrcVT)) |
| 1467 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 1468 | else |
Chris Lattner | fab0887 | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 1469 | Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1470 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1471 | } else { |
| 1472 | // The src value is expanded into multiple registers. |
| 1473 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1474 | Op, DAG.getConstant(0, MVT::i32)); |
| 1475 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1476 | Op, DAG.getConstant(1, MVT::i32)); |
| 1477 | Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo); |
| 1478 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 1479 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1482 | void SelectionDAGISel:: |
| 1483 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 1484 | std::vector<SDOperand> &UnorderedChains) { |
| 1485 | // If this is the entry block, emit arguments. |
| 1486 | Function &F = *BB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1487 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1488 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 1489 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1490 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1491 | unsigned a = 0; |
| 1492 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 1493 | AI != E; ++AI, ++a) |
| 1494 | if (!AI->use_empty()) { |
| 1495 | SDL.setValue(AI, Args[a]); |
Chris Lattner | fa57702 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1497 | // If this argument is live outside of the entry block, insert a copy from |
| 1498 | // whereever we got it to the vreg that other BB's will reference it as. |
| 1499 | if (FuncInfo.ValueMap.count(AI)) { |
| 1500 | SDOperand Copy = |
| 1501 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 1502 | UnorderedChains.push_back(Copy); |
| 1503 | } |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1504 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1505 | |
| 1506 | // Next, if the function has live ins that need to be copied into vregs, |
| 1507 | // emit the copies now, into the top of the block. |
| 1508 | MachineFunction &MF = SDL.DAG.getMachineFunction(); |
| 1509 | if (MF.livein_begin() != MF.livein_end()) { |
| 1510 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 1511 | const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo(); |
| 1512 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 1513 | E = MF.livein_end(); LI != E; ++LI) |
| 1514 | if (LI->second) |
| 1515 | MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second, |
| 1516 | LI->first, RegMap->getRegClass(LI->second)); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1517 | } |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Finally, if the target has anything special to do, allow it to do so. |
| 1520 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1524 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 1525 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
| 1526 | FunctionLoweringInfo &FuncInfo) { |
| 1527 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1528 | |
| 1529 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1530 | |
Chris Lattner | bf20948 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1531 | // Lower any arguments needed in this block if this is the entry block. |
| 1532 | if (LLVMBB == &LLVMBB->getParent()->front()) |
| 1533 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1534 | |
| 1535 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 1536 | SDL.setCurrentBasicBlock(BB); |
| 1537 | |
| 1538 | // Lower all of the non-terminator instructions. |
| 1539 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 1540 | I != E; ++I) |
| 1541 | SDL.visit(*I); |
| 1542 | |
| 1543 | // Ensure that all instructions which are used outside of their defining |
| 1544 | // blocks are available as virtual registers. |
| 1545 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1546 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1547 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1548 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1549 | UnorderedChains.push_back( |
| 1550 | CopyValueToVirtualRegister(SDL, I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 1554 | // ensure constants are generated when needed. Remember the virtual registers |
| 1555 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 1556 | // directly add them, because expansion might result in multiple MBB's for one |
| 1557 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 1558 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1559 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Emit constants only once even if used by multiple PHI nodes. |
| 1562 | std::map<Constant*, unsigned> ConstantsOut; |
| 1563 | |
| 1564 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 1565 | // this block. |
| 1566 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1567 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1568 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1569 | MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin(); |
| 1570 | PHINode *PN; |
| 1571 | |
| 1572 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1573 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1574 | // emitted yet. |
| 1575 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1576 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 1577 | if (!PN->use_empty()) { |
| 1578 | unsigned Reg; |
| 1579 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1580 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 1581 | unsigned &RegOut = ConstantsOut[C]; |
| 1582 | if (RegOut == 0) { |
| 1583 | RegOut = FuncInfo.CreateRegForValue(C); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1584 | UnorderedChains.push_back( |
| 1585 | CopyValueToVirtualRegister(SDL, C, RegOut)); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1586 | } |
| 1587 | Reg = RegOut; |
| 1588 | } else { |
| 1589 | Reg = FuncInfo.ValueMap[PHIOp]; |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1590 | if (Reg == 0) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1591 | assert(isa<AllocaInst>(PHIOp) && |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1592 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 1593 | "Didn't codegen value into a register!??"); |
| 1594 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1595 | UnorderedChains.push_back( |
| 1596 | CopyValueToVirtualRegister(SDL, PHIOp, Reg)); |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1597 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1598 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1600 | // Remember that this register needs to added to the machine PHI node as |
| 1601 | // the input for this MBB. |
| 1602 | unsigned NumElements = |
| 1603 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 1604 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 1605 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1606 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1607 | } |
| 1608 | ConstantsOut.clear(); |
| 1609 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1610 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 1611 | if (!UnorderedChains.empty()) { |
Chris Lattner | 7436b57 | 2005-11-09 05:03:03 +0000 | [diff] [blame] | 1612 | SDOperand Root = SDL.getRoot(); |
| 1613 | if (Root.getOpcode() != ISD::EntryToken) { |
| 1614 | unsigned i = 0, e = UnorderedChains.size(); |
| 1615 | for (; i != e; ++i) { |
| 1616 | assert(UnorderedChains[i].Val->getNumOperands() > 1); |
| 1617 | if (UnorderedChains[i].Val->getOperand(0) == Root) |
| 1618 | break; // Don't add the root if we already indirectly depend on it. |
| 1619 | } |
| 1620 | |
| 1621 | if (i == e) |
| 1622 | UnorderedChains.push_back(Root); |
| 1623 | } |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1624 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains)); |
| 1625 | } |
| 1626 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1627 | // Lower the terminator after the copies are emitted. |
| 1628 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1629 | |
| 1630 | // Make sure the root of the DAG is up-to-date. |
| 1631 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 1635 | FunctionLoweringInfo &FuncInfo) { |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1636 | SelectionDAG DAG(TLI, MF); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1637 | CurDAG = &DAG; |
| 1638 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 1639 | |
| 1640 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 1641 | // types that are not supported by the target. |
| 1642 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 1643 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 1644 | // Run the DAG combiner in pre-legalize mode. |
| 1645 | DAG.Combine(false); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 1646 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1647 | DEBUG(std::cerr << "Lowered selection DAG:\n"); |
| 1648 | DEBUG(DAG.dump()); |
| 1649 | |
| 1650 | // Second step, hack on the DAG until it only uses operations and types that |
| 1651 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1652 | DAG.Legalize(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1653 | |
| 1654 | DEBUG(std::cerr << "Legalized selection DAG:\n"); |
| 1655 | DEBUG(DAG.dump()); |
| 1656 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 1657 | // Run the DAG combiner in post-legalize mode. |
| 1658 | DAG.Combine(true); |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | d48050a | 2005-10-05 06:09:10 +0000 | [diff] [blame] | 1660 | if (ViewDAGs) DAG.viewGraph(); |
| 1661 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1662 | // Third, instruction select all of the operations to machine code, adding the |
| 1663 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1664 | InstructionSelectBasicBlock(DAG); |
| 1665 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1666 | DEBUG(std::cerr << "Selected machine code:\n"); |
| 1667 | DEBUG(BB->dump()); |
| 1668 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1669 | // Next, now that we know what the last MBB the LLVM BB expanded is, update |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1670 | // PHI nodes in successors. |
| 1671 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 1672 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 1673 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1674 | "This is not a machine PHI node that we are updating!"); |
| 1675 | PHI->addRegOperand(PHINodesToUpdate[i].second); |
| 1676 | PHI->addMachineBasicBlockOperand(BB); |
| 1677 | } |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1678 | |
| 1679 | // Finally, add the CFG edges from the last selected MBB to the successor |
| 1680 | // MBBs. |
| 1681 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1682 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 1683 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)]; |
| 1684 | BB->addSuccessor(Succ0MBB); |
| 1685 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1686 | } |