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