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