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" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Intrinsics.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SSARegMap.h" |
| 27 | #include "llvm/Target/TargetData.h" |
| 28 | #include "llvm/Target/TargetFrameInfo.h" |
| 29 | #include "llvm/Target/TargetInstrInfo.h" |
| 30 | #include "llvm/Target/TargetLowering.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | #include <map> |
| 36 | #include <iostream> |
| 37 | using namespace llvm; |
| 38 | |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 39 | #ifndef _NDEBUG |
| 40 | static cl::opt<bool> |
| 41 | ViewDAGs("view-isel-dags", cl::Hidden, |
| 42 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 43 | #else |
| 44 | static const bool ViewDAGS = 0; |
| 45 | #endif |
| 46 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 47 | namespace llvm { |
| 48 | //===--------------------------------------------------------------------===// |
| 49 | /// FunctionLoweringInfo - This contains information that is global to a |
| 50 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 51 | class FunctionLoweringInfo { |
| 52 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 53 | TargetLowering &TLI; |
| 54 | Function &Fn; |
| 55 | MachineFunction &MF; |
| 56 | SSARegMap *RegMap; |
| 57 | |
| 58 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 59 | |
| 60 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 61 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 62 | |
| 63 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 64 | /// we must remember which virtual registers hold the values for |
| 65 | /// cross-basic-block values. |
| 66 | std::map<const Value*, unsigned> ValueMap; |
| 67 | |
| 68 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 69 | /// the entry block. This allows the allocas to be efficiently referenced |
| 70 | /// anywhere in the function. |
| 71 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 72 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 73 | /// BlockLocalArguments - If any arguments are only used in a single basic |
| 74 | /// block, and if the target can access the arguments without side-effects, |
| 75 | /// avoid emitting CopyToReg nodes for those arguments. This map keeps |
| 76 | /// track of which arguments are local to each BB. |
| 77 | std::multimap<BasicBlock*, std::pair<Argument*, |
| 78 | unsigned> > BlockLocalArguments; |
| 79 | |
| 80 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 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 | |
| 126 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 127 | Function &fn, MachineFunction &mf) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 128 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 129 | |
| 130 | // Initialize the mapping of values to registers. This is only set up for |
| 131 | // instruction values that are used outside of the block that defines |
| 132 | // them. |
Chris Lattner | 16ce0df | 2005-05-11 18:57:06 +0000 | [diff] [blame] | 133 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 134 | AI != E; ++AI) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 135 | InitializeRegForValue(AI); |
| 136 | |
| 137 | Function::iterator BB = Fn.begin(), E = Fn.end(); |
| 138 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 139 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 140 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) { |
| 141 | const Type *Ty = AI->getAllocatedType(); |
| 142 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
| 143 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
Chris Lattner | a8217e3 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 144 | |
| 145 | // If the alignment of the value is smaller than the size of the value, |
| 146 | // and if the size of the value is particularly small (<= 8 bytes), |
| 147 | // round up to the size of the value for potentially better performance. |
| 148 | // |
| 149 | // FIXME: This could be made better with a preferred alignment hook in |
| 150 | // TargetData. It serves primarily to 8-byte align doubles for X86. |
| 151 | if (Align < TySize && TySize <= 8) Align = TySize; |
| 152 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 153 | TySize *= CUI->getValue(); // Get total allocated size. |
| 154 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 155 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | for (; BB != E; ++BB) |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 159 | for (BasicBlock::iterator I = BB->begin(), e = BB->end(); I != e; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 160 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 161 | if (!isa<AllocaInst>(I) || |
| 162 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 163 | InitializeRegForValue(I); |
| 164 | |
| 165 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 166 | // also creates the initial PHI MachineInstrs, though none of the input |
| 167 | // operands are populated. |
| 168 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 169 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 170 | MBBMap[BB] = MBB; |
| 171 | MF.getBasicBlockList().push_back(MBB); |
| 172 | |
| 173 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 174 | // appropriate. |
| 175 | PHINode *PN; |
| 176 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 177 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 178 | if (!PN->use_empty()) { |
| 179 | unsigned NumElements = |
| 180 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 181 | unsigned PHIReg = ValueMap[PN]; |
| 182 | assert(PHIReg &&"PHI node does not have an assigned virtual register!"); |
| 183 | for (unsigned i = 0; i != NumElements; ++i) |
| 184 | BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i); |
| 185 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | //===----------------------------------------------------------------------===// |
| 192 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 193 | /// implementation that is parameterized by a TargetLowering object. |
| 194 | /// Also, targets can overload any lowering method. |
| 195 | /// |
| 196 | namespace llvm { |
| 197 | class SelectionDAGLowering { |
| 198 | MachineBasicBlock *CurMBB; |
| 199 | |
| 200 | std::map<const Value*, SDOperand> NodeMap; |
| 201 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 202 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 203 | /// them up and then emit token factor nodes when possible. This allows us to |
| 204 | /// get simple disambiguation between loads without worrying about alias |
| 205 | /// analysis. |
| 206 | std::vector<SDOperand> PendingLoads; |
| 207 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 208 | public: |
| 209 | // TLI - This is information that describes the available target features we |
| 210 | // need for lowering. This indicates when operations are unavailable, |
| 211 | // implemented with a libcall, etc. |
| 212 | TargetLowering &TLI; |
| 213 | SelectionDAG &DAG; |
| 214 | const TargetData &TD; |
| 215 | |
| 216 | /// FuncInfo - Information about the function as a whole. |
| 217 | /// |
| 218 | FunctionLoweringInfo &FuncInfo; |
| 219 | |
| 220 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 221 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 222 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
| 223 | FuncInfo(funcinfo) { |
| 224 | } |
| 225 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 226 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 227 | /// |
| 228 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 229 | if (PendingLoads.empty()) |
| 230 | return DAG.getRoot(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 231 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 232 | if (PendingLoads.size() == 1) { |
| 233 | SDOperand Root = PendingLoads[0]; |
| 234 | DAG.setRoot(Root); |
| 235 | PendingLoads.clear(); |
| 236 | return Root; |
| 237 | } |
| 238 | |
| 239 | // Otherwise, we have to make a token factor node. |
| 240 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads); |
| 241 | PendingLoads.clear(); |
| 242 | DAG.setRoot(Root); |
| 243 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 246 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 247 | |
| 248 | void visit(unsigned Opcode, User &I) { |
| 249 | switch (Opcode) { |
| 250 | default: assert(0 && "Unknown instruction type encountered!"); |
| 251 | abort(); |
| 252 | // Build the switch statement using the Instruction.def file. |
| 253 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 254 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 255 | #include "llvm/Instruction.def" |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 260 | |
| 261 | |
| 262 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 263 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 264 | } |
| 265 | |
| 266 | SDOperand getValue(const Value *V) { |
| 267 | SDOperand &N = NodeMap[V]; |
| 268 | if (N.Val) return N; |
| 269 | |
| 270 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 271 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) |
| 272 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 273 | visit(CE->getOpcode(), *CE); |
| 274 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 275 | return N; |
| 276 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 277 | return N = DAG.getGlobalAddress(GV, VT); |
| 278 | } else if (isa<ConstantPointerNull>(C)) { |
| 279 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 280 | } else if (isa<UndefValue>(C)) { |
Nate Begeman | b882752 | 2005-04-12 23:12:17 +0000 | [diff] [blame] | 281 | return N = DAG.getNode(ISD::UNDEF, VT); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 282 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 283 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
| 284 | } else { |
| 285 | // Canonicalize all constant ints to be unsigned. |
| 286 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT); |
| 287 | } |
| 288 | |
| 289 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 290 | std::map<const AllocaInst*, int>::iterator SI = |
| 291 | FuncInfo.StaticAllocaMap.find(AI); |
| 292 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 293 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 294 | } |
| 295 | |
| 296 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 297 | FuncInfo.ValueMap.find(V); |
| 298 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
Chris Lattner | c8ea3c4 | 2005-01-16 02:23:07 +0000 | [diff] [blame] | 299 | |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 300 | unsigned InReg = VMI->second; |
| 301 | |
| 302 | // If this type is not legal, make it so now. |
| 303 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 304 | |
| 305 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 306 | if (DestVT < VT) { |
| 307 | // Source must be expanded. This input value is actually coming from the |
| 308 | // register pair VMI->second and VMI->second+1. |
| 309 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 310 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 311 | } else { |
| 312 | if (DestVT > VT) { // Promotion case |
| 313 | if (MVT::isFloatingPoint(VT)) |
| 314 | N = DAG.getNode(ISD::FP_ROUND, VT, N); |
| 315 | else |
| 316 | N = DAG.getNode(ISD::TRUNCATE, VT, N); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return N; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 324 | SDOperand &N = NodeMap[V]; |
| 325 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 326 | return N = NewN; |
| 327 | } |
| 328 | |
| 329 | // Terminator instructions. |
| 330 | void visitRet(ReturnInst &I); |
| 331 | void visitBr(BranchInst &I); |
| 332 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 333 | |
| 334 | // These all get lowered before this pass. |
| 335 | void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); } |
| 336 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 337 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 338 | |
| 339 | // |
Chris Lattner | 8f03405 | 2005-08-22 17:28:31 +0000 | [diff] [blame^] | 340 | void visitBinary(User &I, unsigned Opcode, bool isShift = false); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 341 | void visitAdd(User &I) { visitBinary(I, ISD::ADD); } |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 342 | void visitSub(User &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 343 | void visitMul(User &I) { visitBinary(I, ISD::MUL); } |
| 344 | void visitDiv(User &I) { |
| 345 | visitBinary(I, I.getType()->isUnsigned() ? ISD::UDIV : ISD::SDIV); |
| 346 | } |
| 347 | void visitRem(User &I) { |
| 348 | visitBinary(I, I.getType()->isUnsigned() ? ISD::UREM : ISD::SREM); |
| 349 | } |
| 350 | void visitAnd(User &I) { visitBinary(I, ISD::AND); } |
| 351 | void visitOr (User &I) { visitBinary(I, ISD::OR); } |
| 352 | void visitXor(User &I) { visitBinary(I, ISD::XOR); } |
Chris Lattner | 8f03405 | 2005-08-22 17:28:31 +0000 | [diff] [blame^] | 353 | void visitShl(User &I) { visitBinary(I, ISD::SHL, true); } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 354 | void visitShr(User &I) { |
Chris Lattner | 8f03405 | 2005-08-22 17:28:31 +0000 | [diff] [blame^] | 355 | visitBinary(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA, true); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc); |
| 359 | void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); } |
| 360 | void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); } |
| 361 | void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); } |
| 362 | void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); } |
| 363 | void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); } |
| 364 | void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); } |
| 365 | |
| 366 | void visitGetElementPtr(User &I); |
| 367 | void visitCast(User &I); |
| 368 | void visitSelect(User &I); |
| 369 | // |
| 370 | |
| 371 | void visitMalloc(MallocInst &I); |
| 372 | void visitFree(FreeInst &I); |
| 373 | void visitAlloca(AllocaInst &I); |
| 374 | void visitLoad(LoadInst &I); |
| 375 | void visitStore(StoreInst &I); |
| 376 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 377 | void visitCall(CallInst &I); |
| 378 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 379 | void visitVAStart(CallInst &I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 380 | void visitVAArg(VAArgInst &I); |
| 381 | void visitVAEnd(CallInst &I); |
| 382 | void visitVACopy(CallInst &I); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 383 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 385 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 386 | |
| 387 | void visitUserOp1(Instruction &I) { |
| 388 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 389 | abort(); |
| 390 | } |
| 391 | void visitUserOp2(Instruction &I) { |
| 392 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 393 | abort(); |
| 394 | } |
| 395 | }; |
| 396 | } // end namespace llvm |
| 397 | |
| 398 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 399 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 400 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 401 | return; |
| 402 | } |
| 403 | |
| 404 | SDOperand Op1 = getValue(I.getOperand(0)); |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 405 | MVT::ValueType TmpVT; |
| 406 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 407 | switch (Op1.getValueType()) { |
| 408 | default: assert(0 && "Unknown value type!"); |
| 409 | case MVT::i1: |
| 410 | case MVT::i8: |
| 411 | case MVT::i16: |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 412 | case MVT::i32: |
| 413 | // If this is a machine where 32-bits is legal or expanded, promote to |
| 414 | // 32-bits, otherwise, promote to 64-bits. |
| 415 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 416 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 417 | else |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 418 | TmpVT = MVT::i32; |
| 419 | |
| 420 | // Extend integer types to result type. |
| 421 | if (I.getOperand(0)->getType()->isSigned()) |
| 422 | Op1 = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, Op1); |
| 423 | else |
| 424 | Op1 = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, Op1); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 425 | break; |
| 426 | case MVT::f32: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 427 | case MVT::i64: |
| 428 | case MVT::f64: |
| 429 | break; // No extension needed! |
| 430 | } |
| 431 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 432 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot(), Op1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 436 | // Update machine-CFG edges. |
| 437 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 438 | |
| 439 | // Figure out which block is immediately after the current one. |
| 440 | MachineBasicBlock *NextBlock = 0; |
| 441 | MachineFunction::iterator BBI = CurMBB; |
| 442 | if (++BBI != CurMBB->getParent()->end()) |
| 443 | NextBlock = BBI; |
| 444 | |
| 445 | if (I.isUnconditional()) { |
| 446 | // If this is not a fall-through branch, emit the branch. |
| 447 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 448 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 449 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 450 | } else { |
| 451 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 452 | |
| 453 | SDOperand Cond = getValue(I.getCondition()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 454 | if (Succ1MBB == NextBlock) { |
| 455 | // If the condition is false, fall through. This means we should branch |
| 456 | // if the condition is true to Succ #0. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 457 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 458 | Cond, DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 459 | } else if (Succ0MBB == NextBlock) { |
| 460 | // If the condition is true, fall through. This means we should branch if |
| 461 | // the condition is false to Succ #1. Invert the condition first. |
| 462 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 463 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 464 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 465 | Cond, DAG.getBasicBlock(Succ1MBB))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 466 | } else { |
Chris Lattner | e7ccd4a | 2005-04-09 03:30:29 +0000 | [diff] [blame] | 467 | std::vector<SDOperand> Ops; |
| 468 | Ops.push_back(getRoot()); |
| 469 | Ops.push_back(Cond); |
| 470 | Ops.push_back(DAG.getBasicBlock(Succ0MBB)); |
| 471 | Ops.push_back(DAG.getBasicBlock(Succ1MBB)); |
| 472 | DAG.setRoot(DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Chris Lattner | b9fccc4 | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 477 | void SelectionDAGLowering::visitSub(User &I) { |
| 478 | // -0.0 - X --> fneg |
| 479 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 480 | if (CFP->isExactlyValue(-0.0)) { |
| 481 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 482 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | visitBinary(I, ISD::SUB); |
| 487 | } |
| 488 | |
Chris Lattner | 8f03405 | 2005-08-22 17:28:31 +0000 | [diff] [blame^] | 489 | void SelectionDAGLowering::visitBinary(User &I, unsigned Opcode, bool isShift) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 490 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 491 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 492 | |
Chris Lattner | 8f03405 | 2005-08-22 17:28:31 +0000 | [diff] [blame^] | 493 | if (isShift) |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 494 | Op2 = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 495 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 496 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 497 | } |
| 498 | |
| 499 | void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode, |
| 500 | ISD::CondCode UnsignedOpcode) { |
| 501 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 502 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 503 | ISD::CondCode Opcode = SignedOpcode; |
| 504 | if (I.getOperand(0)->getType()->isUnsigned()) |
| 505 | Opcode = UnsignedOpcode; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 506 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void SelectionDAGLowering::visitSelect(User &I) { |
| 510 | SDOperand Cond = getValue(I.getOperand(0)); |
| 511 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 512 | SDOperand FalseVal = getValue(I.getOperand(2)); |
| 513 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 514 | TrueVal, FalseVal)); |
| 515 | } |
| 516 | |
| 517 | void SelectionDAGLowering::visitCast(User &I) { |
| 518 | SDOperand N = getValue(I.getOperand(0)); |
| 519 | MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType()); |
| 520 | MVT::ValueType DestTy = TLI.getValueType(I.getType()); |
| 521 | |
| 522 | if (N.getValueType() == DestTy) { |
| 523 | setValue(&I, N); // noop cast. |
Chris Lattner | ef311aa | 2005-05-09 22:17:13 +0000 | [diff] [blame] | 524 | } else if (DestTy == MVT::i1) { |
| 525 | // Cast to bool is a comparison against zero, not truncation to zero. |
| 526 | SDOperand Zero = isInteger(SrcTy) ? DAG.getConstant(0, N.getValueType()) : |
| 527 | DAG.getConstantFP(0.0, N.getValueType()); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 528 | setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE)); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 529 | } else if (isInteger(SrcTy)) { |
| 530 | if (isInteger(DestTy)) { // Int -> Int cast |
| 531 | if (DestTy < SrcTy) // Truncating cast? |
| 532 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N)); |
| 533 | else if (I.getOperand(0)->getType()->isSigned()) |
| 534 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N)); |
| 535 | else |
| 536 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N)); |
| 537 | } else { // Int -> FP cast |
| 538 | if (I.getOperand(0)->getType()->isSigned()) |
| 539 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N)); |
| 540 | else |
| 541 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N)); |
| 542 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 543 | } else { |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 544 | assert(isFloatingPoint(SrcTy) && "Unknown value type!"); |
| 545 | if (isFloatingPoint(DestTy)) { // FP -> FP cast |
| 546 | if (DestTy < SrcTy) // Rounding cast? |
| 547 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N)); |
| 548 | else |
| 549 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N)); |
| 550 | } else { // FP -> Int cast. |
| 551 | if (I.getType()->isSigned()) |
| 552 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N)); |
| 553 | else |
| 554 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N)); |
| 555 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 560 | SDOperand N = getValue(I.getOperand(0)); |
| 561 | const Type *Ty = I.getOperand(0)->getType(); |
| 562 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 563 | |
| 564 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 565 | OI != E; ++OI) { |
| 566 | Value *Idx = *OI; |
| 567 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 568 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 569 | if (Field) { |
| 570 | // N = N + Offset |
| 571 | uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 572 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 573 | getIntPtrConstant(Offset)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 574 | } |
| 575 | Ty = StTy->getElementType(Field); |
| 576 | } else { |
| 577 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 578 | if (!isa<Constant>(Idx) || !cast<Constant>(Idx)->isNullValue()) { |
| 579 | // N = N + Idx * ElementSize; |
| 580 | uint64_t ElementSize = TD.getTypeSize(Ty); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 581 | SDOperand IdxN = getValue(Idx), Scale = getIntPtrConstant(ElementSize); |
| 582 | |
| 583 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 584 | // it. |
| 585 | if (IdxN.getValueType() < Scale.getValueType()) { |
| 586 | if (Idx->getType()->isSigned()) |
| 587 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, Scale.getValueType(), IdxN); |
| 588 | else |
| 589 | IdxN = DAG.getNode(ISD::ZERO_EXTEND, Scale.getValueType(), IdxN); |
| 590 | } else if (IdxN.getValueType() > Scale.getValueType()) |
| 591 | IdxN = DAG.getNode(ISD::TRUNCATE, Scale.getValueType(), IdxN); |
| 592 | |
| 593 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 594 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | setValue(&I, N); |
| 599 | } |
| 600 | |
| 601 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 602 | // If this is a fixed sized alloca in the entry block of the function, |
| 603 | // allocate it statically on the stack. |
| 604 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 605 | return; // getValue will auto-populate this. |
| 606 | |
| 607 | const Type *Ty = I.getAllocatedType(); |
| 608 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
| 609 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 610 | |
| 611 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 612 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 613 | if (IntPtr < AllocSize.getValueType()) |
| 614 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 615 | else if (IntPtr > AllocSize.getValueType()) |
| 616 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 618 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 619 | getIntPtrConstant(TySize)); |
| 620 | |
| 621 | // Handle alignment. If the requested alignment is less than or equal to the |
| 622 | // stack alignment, ignore it and round the size of the allocation up to the |
| 623 | // stack alignment size. If the size is greater than the stack alignment, we |
| 624 | // note this in the DYNAMIC_STACKALLOC node. |
| 625 | unsigned StackAlign = |
| 626 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 627 | if (Align <= StackAlign) { |
| 628 | Align = 0; |
| 629 | // Add SA-1 to the size. |
| 630 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 631 | getIntPtrConstant(StackAlign-1)); |
| 632 | // Mask out the low bits for alignment purposes. |
| 633 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 634 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 635 | } |
| 636 | |
Chris Lattner | adf6c2a | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 637 | std::vector<MVT::ValueType> VTs; |
| 638 | VTs.push_back(AllocSize.getValueType()); |
| 639 | VTs.push_back(MVT::Other); |
| 640 | std::vector<SDOperand> Ops; |
| 641 | Ops.push_back(getRoot()); |
| 642 | Ops.push_back(AllocSize); |
| 643 | Ops.push_back(getIntPtrConstant(Align)); |
| 644 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 645 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 646 | |
| 647 | // Inform the Frame Information that we have just allocated a variable-sized |
| 648 | // object. |
| 649 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 650 | } |
| 651 | |
| 652 | |
| 653 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 654 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 655 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 656 | SDOperand Root; |
| 657 | if (I.isVolatile()) |
| 658 | Root = getRoot(); |
| 659 | else { |
| 660 | // Do not serialize non-volatile loads against each other. |
| 661 | Root = DAG.getRoot(); |
| 662 | } |
| 663 | |
Chris Lattner | 369e6db | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 664 | SDOperand L = DAG.getLoad(TLI.getValueType(I.getType()), Root, Ptr, |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 665 | DAG.getSrcValue(I.getOperand(0))); |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 666 | setValue(&I, L); |
| 667 | |
| 668 | if (I.isVolatile()) |
| 669 | DAG.setRoot(L.getValue(1)); |
| 670 | else |
| 671 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
| 675 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 676 | Value *SrcV = I.getOperand(0); |
| 677 | SDOperand Src = getValue(SrcV); |
| 678 | SDOperand Ptr = getValue(I.getOperand(1)); |
Chris Lattner | 369e6db | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 679 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 680 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 684 | const char *RenameFn = 0; |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 685 | SDOperand Tmp; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 686 | if (Function *F = I.getCalledFunction()) |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 687 | if (F->isExternal()) |
| 688 | switch (F->getIntrinsicID()) { |
| 689 | case 0: // Not an LLVM intrinsic. |
| 690 | if (F->getName() == "fabs" || F->getName() == "fabsf") { |
| 691 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 692 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 693 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 694 | Tmp = getValue(I.getOperand(1)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 695 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 696 | return; |
| 697 | } |
| 698 | } |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 699 | else if (F->getName() == "sin" || F->getName() == "sinf") { |
| 700 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 701 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 702 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 703 | Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 704 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 705 | return; |
| 706 | } |
| 707 | } |
| 708 | else if (F->getName() == "cos" || F->getName() == "cosf") { |
| 709 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 710 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 711 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 712 | Tmp = getValue(I.getOperand(1)); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 713 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 714 | return; |
| 715 | } |
| 716 | } |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 717 | break; |
| 718 | case Intrinsic::vastart: visitVAStart(I); return; |
| 719 | case Intrinsic::vaend: visitVAEnd(I); return; |
| 720 | case Intrinsic::vacopy: visitVACopy(I); return; |
| 721 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return; |
| 722 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return; |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 723 | |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 724 | case Intrinsic::setjmp: RenameFn = "setjmp"; break; |
| 725 | case Intrinsic::longjmp: RenameFn = "longjmp"; break; |
| 726 | case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return; |
| 727 | case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return; |
| 728 | case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 729 | |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 730 | case Intrinsic::readport: |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 731 | case Intrinsic::readio: { |
| 732 | std::vector<MVT::ValueType> VTs; |
| 733 | VTs.push_back(TLI.getValueType(I.getType())); |
| 734 | VTs.push_back(MVT::Other); |
| 735 | std::vector<SDOperand> Ops; |
| 736 | Ops.push_back(getRoot()); |
| 737 | Ops.push_back(getValue(I.getOperand(1))); |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 738 | Tmp = DAG.getNode(F->getIntrinsicID() == Intrinsic::readport ? |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 739 | ISD::READPORT : ISD::READIO, VTs, Ops); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 740 | |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 741 | setValue(&I, Tmp); |
| 742 | DAG.setRoot(Tmp.getValue(1)); |
| 743 | return; |
Chris Lattner | 1ca85d5 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 744 | } |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 745 | case Intrinsic::writeport: |
| 746 | case Intrinsic::writeio: |
| 747 | DAG.setRoot(DAG.getNode(F->getIntrinsicID() == Intrinsic::writeport ? |
| 748 | ISD::WRITEPORT : ISD::WRITEIO, MVT::Other, |
| 749 | getRoot(), getValue(I.getOperand(1)), |
| 750 | getValue(I.getOperand(2)))); |
| 751 | return; |
Chris Lattner | 7ea0ade | 2005-05-05 17:55:17 +0000 | [diff] [blame] | 752 | case Intrinsic::dbg_stoppoint: |
| 753 | case Intrinsic::dbg_region_start: |
| 754 | case Intrinsic::dbg_region_end: |
| 755 | case Intrinsic::dbg_func_start: |
| 756 | case Intrinsic::dbg_declare: |
| 757 | if (I.getType() != Type::VoidTy) |
| 758 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 759 | return; |
| 760 | |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 761 | case Intrinsic::isunordered: |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 762 | setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)), |
| 763 | getValue(I.getOperand(2)), ISD::SETUO)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 764 | return; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 765 | |
| 766 | case Intrinsic::sqrt: |
| 767 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 768 | getValue(I.getOperand(1)).getValueType(), |
| 769 | getValue(I.getOperand(1)))); |
| 770 | return; |
| 771 | |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 772 | case Intrinsic::pcmarker: |
| 773 | Tmp = getValue(I.getOperand(1)); |
| 774 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 775 | return; |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 776 | case Intrinsic::cttz: |
| 777 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 778 | getValue(I.getOperand(1)).getValueType(), |
| 779 | getValue(I.getOperand(1)))); |
| 780 | return; |
| 781 | case Intrinsic::ctlz: |
| 782 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 783 | getValue(I.getOperand(1)).getValueType(), |
| 784 | getValue(I.getOperand(1)))); |
| 785 | return; |
| 786 | case Intrinsic::ctpop: |
| 787 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 788 | getValue(I.getOperand(1)).getValueType(), |
| 789 | getValue(I.getOperand(1)))); |
| 790 | return; |
Chris Lattner | d0f6c1f | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 791 | default: |
| 792 | std::cerr << I; |
| 793 | assert(0 && "This intrinsic is not implemented yet!"); |
| 794 | return; |
Chris Lattner | c0f1815 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 795 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 796 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 797 | SDOperand Callee; |
| 798 | if (!RenameFn) |
| 799 | Callee = getValue(I.getOperand(0)); |
| 800 | else |
| 801 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 802 | std::vector<std::pair<SDOperand, const Type*> > Args; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 803 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 804 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 805 | Value *Arg = I.getOperand(i); |
| 806 | SDOperand ArgNode = getValue(Arg); |
| 807 | Args.push_back(std::make_pair(ArgNode, Arg->getType())); |
| 808 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 809 | |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 810 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 811 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 812 | |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 813 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 9092fa3 | 2005-05-12 19:56:57 +0000 | [diff] [blame] | 814 | TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(), |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 815 | I.isTailCall(), Callee, Args, DAG); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 816 | if (I.getType() != Type::VoidTy) |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 817 | setValue(&I, Result.first); |
| 818 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 822 | SDOperand Src = getValue(I.getOperand(0)); |
| 823 | |
| 824 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 825 | |
| 826 | if (IntPtr < Src.getValueType()) |
| 827 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 828 | else if (IntPtr > Src.getValueType()) |
| 829 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 830 | |
| 831 | // Scale the source by the type size. |
| 832 | uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType()); |
| 833 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 834 | Src, getIntPtrConstant(ElementSize)); |
| 835 | |
| 836 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 837 | Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType())); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 838 | |
| 839 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 840 | TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 841 | DAG.getExternalSymbol("malloc", IntPtr), |
| 842 | Args, DAG); |
| 843 | setValue(&I, Result.first); // Pointers always fit in registers |
| 844 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 848 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 849 | Args.push_back(std::make_pair(getValue(I.getOperand(0)), |
| 850 | TLI.getTargetData().getIntPtrType())); |
| 851 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 852 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 853 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 854 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 855 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 858 | SDOperand TargetLowering::LowerVAStart(SDOperand Chain, |
| 859 | SDOperand VAListP, Value *VAListV, |
| 860 | SelectionDAG &DAG) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 861 | // We have no sane default behavior, just emit a useful error message and bail |
| 862 | // out. |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 863 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 864 | abort(); |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 865 | return SDOperand(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 868 | SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV, |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 869 | SelectionDAG &DAG) { |
| 870 | // Default to a noop. |
| 871 | return Chain; |
| 872 | } |
| 873 | |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 874 | SDOperand TargetLowering::LowerVACopy(SDOperand Chain, |
| 875 | SDOperand SrcP, Value *SrcV, |
| 876 | SDOperand DestP, Value *DestV, |
| 877 | SelectionDAG &DAG) { |
| 878 | // Default to copying the input list. |
| 879 | SDOperand Val = DAG.getLoad(getPointerTy(), Chain, |
| 880 | SrcP, DAG.getSrcValue(SrcV)); |
Andrew Lenharth | 213e557 | 2005-06-22 21:04:42 +0000 | [diff] [blame] | 881 | SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 882 | Val, DestP, DAG.getSrcValue(DestV)); |
| 883 | return Result; |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | std::pair<SDOperand,SDOperand> |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 887 | TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 888 | const Type *ArgTy, SelectionDAG &DAG) { |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 889 | // We have no sane default behavior, just emit a useful error message and bail |
| 890 | // out. |
| 891 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
| 892 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 893 | return std::make_pair(SDOperand(), SDOperand()); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | |
| 897 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 898 | DAG.setRoot(TLI.LowerVAStart(getRoot(), getValue(I.getOperand(1)), |
| 899 | I.getOperand(1), DAG)); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
| 903 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 904 | TLI.LowerVAArg(getRoot(), getValue(I.getOperand(0)), I.getOperand(0), |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 905 | I.getType(), DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 906 | setValue(&I, Result.first); |
| 907 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 911 | DAG.setRoot(TLI.LowerVAEnd(getRoot(), getValue(I.getOperand(1)), |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 912 | I.getOperand(1), DAG)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Chris Lattner | e64e72b | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 916 | SDOperand Result = |
| 917 | TLI.LowerVACopy(getRoot(), getValue(I.getOperand(2)), I.getOperand(2), |
| 918 | getValue(I.getOperand(1)), I.getOperand(1), DAG); |
| 919 | DAG.setRoot(Result); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 922 | |
| 923 | // It is always conservatively correct for llvm.returnaddress and |
| 924 | // llvm.frameaddress to return 0. |
| 925 | std::pair<SDOperand, SDOperand> |
| 926 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 927 | unsigned Depth, SelectionDAG &DAG) { |
| 928 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Chris Lattner | 50381b6 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 931 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 932 | assert(0 && "LowerOperation not implemented for this target!"); |
| 933 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 934 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 937 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
| 938 | unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue(); |
| 939 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 940 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 941 | setValue(&I, Result.first); |
| 942 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 945 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
| 946 | std::vector<SDOperand> Ops; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 947 | Ops.push_back(getRoot()); |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 948 | Ops.push_back(getValue(I.getOperand(1))); |
| 949 | Ops.push_back(getValue(I.getOperand(2))); |
| 950 | Ops.push_back(getValue(I.getOperand(3))); |
| 951 | Ops.push_back(getValue(I.getOperand(4))); |
| 952 | DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 955 | //===----------------------------------------------------------------------===// |
| 956 | // SelectionDAGISel code |
| 957 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 958 | |
| 959 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 960 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 961 | } |
| 962 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 963 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 964 | // FIXME: we only modify the CFG to split critical edges. This |
| 965 | // updates dom and loop info. |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 966 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 967 | |
| 968 | |
| 969 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 970 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 971 | RegMap = MF.getSSARegMap(); |
| 972 | DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n"); |
| 973 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 974 | // First pass, split all critical edges for PHI nodes with incoming values |
| 975 | // that are constants, this way the load of the constant into a vreg will not |
| 976 | // be placed into MBBs that are used some other way. |
Chris Lattner | 36b708f | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 977 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 978 | PHINode *PN; |
| 979 | for (BasicBlock::iterator BBI = BB->begin(); |
| 980 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) |
| 981 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 982 | if (isa<Constant>(PN->getIncomingValue(i))) |
| 983 | SplitCriticalEdge(PN->getIncomingBlock(i), BB); |
| 984 | } |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 986 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 987 | |
| 988 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 989 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 991 | return true; |
| 992 | } |
| 993 | |
| 994 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 995 | SDOperand SelectionDAGISel:: |
| 996 | CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) { |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 997 | SDOperand Op = SDL.getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 998 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 999 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1000 | "Copy from a reg to the same reg!"); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1001 | |
| 1002 | // If this type is not legal, we must make sure to not create an invalid |
| 1003 | // register use. |
| 1004 | MVT::ValueType SrcVT = Op.getValueType(); |
| 1005 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
| 1006 | SelectionDAG &DAG = SDL.DAG; |
| 1007 | if (SrcVT == DestVT) { |
| 1008 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1009 | } else if (SrcVT < DestVT) { |
| 1010 | // The src value is promoted to the register. |
Chris Lattner | fae59b9 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 1011 | if (MVT::isFloatingPoint(SrcVT)) |
| 1012 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 1013 | else |
| 1014 | Op = DAG.getNode(ISD::ZERO_EXTEND, DestVT, Op); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1015 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1016 | } else { |
| 1017 | // The src value is expanded into multiple registers. |
| 1018 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1019 | Op, DAG.getConstant(0, MVT::i32)); |
| 1020 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1021 | Op, DAG.getConstant(1, MVT::i32)); |
| 1022 | Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo); |
| 1023 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 1024 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1027 | /// IsOnlyUsedInOneBasicBlock - If the specified argument is only used in a |
| 1028 | /// single basic block, return that block. Otherwise, return a null pointer. |
| 1029 | static BasicBlock *IsOnlyUsedInOneBasicBlock(Argument *A) { |
| 1030 | if (A->use_empty()) return 0; |
| 1031 | BasicBlock *BB = cast<Instruction>(A->use_back())->getParent(); |
| 1032 | for (Argument::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; |
| 1033 | ++UI) |
| 1034 | if (isa<PHINode>(*UI) || cast<Instruction>(*UI)->getParent() != BB) |
| 1035 | return 0; // Disagreement among the users? |
Chris Lattner | aa781b3 | 2005-02-17 19:40:32 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Okay, there is a single BB user. Only permit this optimization if this is |
| 1038 | // the entry block, otherwise, we might sink argument loads into loops and |
| 1039 | // stuff. Later, when we have global instruction selection, this won't be an |
| 1040 | // issue clearly. |
| 1041 | if (BB == BB->getParent()->begin()) |
| 1042 | return BB; |
| 1043 | return 0; |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1046 | void SelectionDAGISel:: |
| 1047 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 1048 | std::vector<SDOperand> &UnorderedChains) { |
| 1049 | // If this is the entry block, emit arguments. |
| 1050 | Function &F = *BB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1051 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (BB == &F.front()) { |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1054 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 1055 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1056 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
| 1057 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1058 | // If there were side effects accessing the argument list, do not do |
| 1059 | // anything special. |
| 1060 | if (OldRoot != SDL.DAG.getRoot()) { |
| 1061 | unsigned a = 0; |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1062 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 1063 | AI != E; ++AI,++a) |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1064 | if (!AI->use_empty()) { |
| 1065 | SDL.setValue(AI, Args[a]); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1066 | SDOperand Copy = |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1067 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 1068 | UnorderedChains.push_back(Copy); |
| 1069 | } |
| 1070 | } else { |
| 1071 | // Otherwise, if any argument is only accessed in a single basic block, |
| 1072 | // emit that argument only to that basic block. |
| 1073 | unsigned a = 0; |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1074 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 1075 | AI != E; ++AI,++a) |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1076 | if (!AI->use_empty()) { |
| 1077 | if (BasicBlock *BBU = IsOnlyUsedInOneBasicBlock(AI)) { |
| 1078 | FuncInfo.BlockLocalArguments.insert(std::make_pair(BBU, |
| 1079 | std::make_pair(AI, a))); |
| 1080 | } else { |
| 1081 | SDL.setValue(AI, Args[a]); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1082 | SDOperand Copy = |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1083 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 1084 | UnorderedChains.push_back(Copy); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
Chris Lattner | 405ef9e | 2005-05-13 07:33:32 +0000 | [diff] [blame] | 1088 | |
| 1089 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1090 | } |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1092 | // See if there are any block-local arguments that need to be emitted in this |
| 1093 | // block. |
| 1094 | |
| 1095 | if (!FuncInfo.BlockLocalArguments.empty()) { |
| 1096 | std::multimap<BasicBlock*, std::pair<Argument*, unsigned> >::iterator BLAI = |
| 1097 | FuncInfo.BlockLocalArguments.lower_bound(BB); |
| 1098 | if (BLAI != FuncInfo.BlockLocalArguments.end() && BLAI->first == BB) { |
| 1099 | // Lower the arguments into this block. |
| 1100 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1102 | // Set up the value mapping for the local arguments. |
| 1103 | for (; BLAI != FuncInfo.BlockLocalArguments.end() && BLAI->first == BB; |
| 1104 | ++BLAI) |
| 1105 | SDL.setValue(BLAI->second.first, Args[BLAI->second.second]); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1106 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1107 | // Any dead arguments will just be ignored here. |
| 1108 | } |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1113 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 1114 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
| 1115 | FunctionLoweringInfo &FuncInfo) { |
| 1116 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1117 | |
| 1118 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1119 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1120 | // Lower any arguments needed in this block. |
| 1121 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1122 | |
| 1123 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 1124 | SDL.setCurrentBasicBlock(BB); |
| 1125 | |
| 1126 | // Lower all of the non-terminator instructions. |
| 1127 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 1128 | I != E; ++I) |
| 1129 | SDL.visit(*I); |
| 1130 | |
| 1131 | // Ensure that all instructions which are used outside of their defining |
| 1132 | // blocks are available as virtual registers. |
| 1133 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1134 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1135 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1136 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1137 | UnorderedChains.push_back( |
| 1138 | CopyValueToVirtualRegister(SDL, I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 1142 | // ensure constants are generated when needed. Remember the virtual registers |
| 1143 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 1144 | // directly add them, because expansion might result in multiple MBB's for one |
| 1145 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 1146 | // the end. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1147 | // |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1148 | |
| 1149 | // Emit constants only once even if used by multiple PHI nodes. |
| 1150 | std::map<Constant*, unsigned> ConstantsOut; |
| 1151 | |
| 1152 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 1153 | // this block. |
| 1154 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1155 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1156 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1157 | MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin(); |
| 1158 | PHINode *PN; |
| 1159 | |
| 1160 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1161 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1162 | // emitted yet. |
| 1163 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1164 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 1165 | if (!PN->use_empty()) { |
| 1166 | unsigned Reg; |
| 1167 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1168 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 1169 | unsigned &RegOut = ConstantsOut[C]; |
| 1170 | if (RegOut == 0) { |
| 1171 | RegOut = FuncInfo.CreateRegForValue(C); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1172 | UnorderedChains.push_back( |
| 1173 | CopyValueToVirtualRegister(SDL, C, RegOut)); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1174 | } |
| 1175 | Reg = RegOut; |
| 1176 | } else { |
| 1177 | Reg = FuncInfo.ValueMap[PHIOp]; |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1178 | if (Reg == 0) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1179 | assert(isa<AllocaInst>(PHIOp) && |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1180 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 1181 | "Didn't codegen value into a register!??"); |
| 1182 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1183 | UnorderedChains.push_back( |
| 1184 | CopyValueToVirtualRegister(SDL, PHIOp, Reg)); |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1185 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1186 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1188 | // Remember that this register needs to added to the machine PHI node as |
| 1189 | // the input for this MBB. |
| 1190 | unsigned NumElements = |
| 1191 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 1192 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 1193 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1194 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1195 | } |
| 1196 | ConstantsOut.clear(); |
| 1197 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1198 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 1199 | if (!UnorderedChains.empty()) { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1200 | UnorderedChains.push_back(SDL.getRoot()); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1201 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains)); |
| 1202 | } |
| 1203 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1204 | // Lower the terminator after the copies are emitted. |
| 1205 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1206 | |
| 1207 | // Make sure the root of the DAG is up-to-date. |
| 1208 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 1212 | FunctionLoweringInfo &FuncInfo) { |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1213 | SelectionDAG DAG(TLI, MF); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1214 | CurDAG = &DAG; |
| 1215 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 1216 | |
| 1217 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 1218 | // types that are not supported by the target. |
| 1219 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 1220 | |
| 1221 | DEBUG(std::cerr << "Lowered selection DAG:\n"); |
| 1222 | DEBUG(DAG.dump()); |
| 1223 | |
| 1224 | // Second step, hack on the DAG until it only uses operations and types that |
| 1225 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1226 | DAG.Legalize(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1227 | |
| 1228 | DEBUG(std::cerr << "Legalized selection DAG:\n"); |
| 1229 | DEBUG(DAG.dump()); |
| 1230 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1231 | // Third, instruction select all of the operations to machine code, adding the |
| 1232 | // code to the MachineBasicBlock. |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1233 | InstructionSelectBasicBlock(DAG); |
| 1234 | |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 1235 | if (ViewDAGs) DAG.viewGraph(); |
| 1236 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1237 | DEBUG(std::cerr << "Selected machine code:\n"); |
| 1238 | DEBUG(BB->dump()); |
| 1239 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1240 | // 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] | 1241 | // PHI nodes in successors. |
| 1242 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 1243 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 1244 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1245 | "This is not a machine PHI node that we are updating!"); |
| 1246 | PHI->addRegOperand(PHINodesToUpdate[i].second); |
| 1247 | PHI->addMachineBasicBlockOperand(BB); |
| 1248 | } |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Finally, add the CFG edges from the last selected MBB to the successor |
| 1251 | // MBBs. |
| 1252 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1253 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 1254 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)]; |
| 1255 | BB->addSuccessor(Succ0MBB); |
| 1256 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1257 | } |