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