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