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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Dan Gohman | 84fbac5 | 2009-02-06 17:22:58 +0000 | [diff] [blame] | 15 | #include "ScheduleDAGSDNodes.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 16 | #include "SelectionDAGBuild.h" |
Dan Gohman | 84fbac5 | 2009-02-06 17:22:58 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Anton Korobeynikov | 5502bf6 | 2007-04-04 21:14:49 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 20 | #include "llvm/CallingConv.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Function.h" |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 24 | #include "llvm/InlineAsm.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Intrinsics.h" |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 27 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 78eca17 | 2008-08-19 22:33:34 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/FastISel.h" |
Gordon Henriksen | 5a29c9e | 2008-08-17 12:56:54 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/GCStrategy.h" |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/GCMetadata.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunction.h" |
| 32 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 35 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 36 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | fc54c55 | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/SelectionDAG.h" |
Devang Patel | 6e7a161 | 2009-01-09 19:11:50 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/DwarfWriter.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetData.h" |
| 43 | #include "llvm/Target/TargetFrameInfo.h" |
| 44 | #include "llvm/Target/TargetInstrInfo.h" |
| 45 | #include "llvm/Target/TargetLowering.h" |
| 46 | #include "llvm/Target/TargetMachine.h" |
Vladimir Prus | 1247291 | 2006-05-23 13:43:15 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Compiler.h" |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 51 | #include "llvm/Support/MathExtras.h" |
| 52 | #include "llvm/Support/Timer.h" |
Jeff Cohen | 7e88103 | 2006-02-24 02:52:40 +0000 | [diff] [blame] | 53 | #include <algorithm> |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 54 | using namespace llvm; |
| 55 | |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 56 | static cl::opt<bool> |
Duncan Sands | 7cb0787 | 2008-10-27 08:42:46 +0000 | [diff] [blame] | 57 | DisableLegalizeTypes("disable-legalize-types", cl::Hidden); |
Dan Gohman | 78eca17 | 2008-08-19 22:33:34 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
Dan Gohman | 293d5f8 | 2008-09-09 22:06:46 +0000 | [diff] [blame] | 59 | EnableFastISelVerbose("fast-isel-verbose", cl::Hidden, |
Dan Gohman | d659d50 | 2008-10-20 21:30:12 +0000 | [diff] [blame] | 60 | cl::desc("Enable verbose messages in the \"fast\" " |
Dan Gohman | 293d5f8 | 2008-09-09 22:06:46 +0000 | [diff] [blame] | 61 | "instruction selector")); |
| 62 | static cl::opt<bool> |
Dan Gohman | 4344a5d | 2008-09-09 23:05:00 +0000 | [diff] [blame] | 63 | EnableFastISelAbort("fast-isel-abort", cl::Hidden, |
| 64 | cl::desc("Enable abort calls when \"fast\" instruction fails")); |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
| 66 | SchedLiveInCopies("schedule-livein-copies", |
| 67 | cl::desc("Schedule copies of livein registers"), |
| 68 | cl::init(false)); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 69 | |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 70 | #ifndef NDEBUG |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 71 | static cl::opt<bool> |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 72 | ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden, |
| 73 | cl::desc("Pop up a window to show dags before the first " |
| 74 | "dag combine pass")); |
| 75 | static cl::opt<bool> |
| 76 | ViewLegalizeTypesDAGs("view-legalize-types-dags", cl::Hidden, |
| 77 | cl::desc("Pop up a window to show dags before legalize types")); |
| 78 | static cl::opt<bool> |
| 79 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 80 | cl::desc("Pop up a window to show dags before legalize")); |
| 81 | static cl::opt<bool> |
| 82 | ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden, |
| 83 | cl::desc("Pop up a window to show dags before the second " |
| 84 | "dag combine pass")); |
| 85 | static cl::opt<bool> |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 86 | ViewDAGCombineLT("view-dag-combine-lt-dags", cl::Hidden, |
| 87 | cl::desc("Pop up a window to show dags before the post legalize types" |
| 88 | " dag combine pass")); |
| 89 | static cl::opt<bool> |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 90 | ViewISelDAGs("view-isel-dags", cl::Hidden, |
| 91 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 92 | static cl::opt<bool> |
| 93 | ViewSchedDAGs("view-sched-dags", cl::Hidden, |
| 94 | cl::desc("Pop up a window to show sched dags as they are processed")); |
Dan Gohman | 3e1a7ae | 2007-08-28 20:32:58 +0000 | [diff] [blame] | 95 | static cl::opt<bool> |
| 96 | ViewSUnitDAGs("view-sunit-dags", cl::Hidden, |
Chris Lattner | 5bab785 | 2008-01-25 17:24:52 +0000 | [diff] [blame] | 97 | cl::desc("Pop up a window to show SUnit dags after they are processed")); |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 98 | #else |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 99 | static const bool ViewDAGCombine1 = false, |
| 100 | ViewLegalizeTypesDAGs = false, ViewLegalizeDAGs = false, |
| 101 | ViewDAGCombine2 = false, |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 102 | ViewDAGCombineLT = false, |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 103 | ViewISelDAGs = false, ViewSchedDAGs = false, |
| 104 | ViewSUnitDAGs = false; |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 107 | //===---------------------------------------------------------------------===// |
| 108 | /// |
| 109 | /// RegisterScheduler class - Track the registration of instruction schedulers. |
| 110 | /// |
| 111 | //===---------------------------------------------------------------------===// |
| 112 | MachinePassRegistry RegisterScheduler::Registry; |
| 113 | |
| 114 | //===---------------------------------------------------------------------===// |
| 115 | /// |
| 116 | /// ISHeuristic command line option for instruction schedulers. |
| 117 | /// |
| 118 | //===---------------------------------------------------------------------===// |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 119 | static cl::opt<RegisterScheduler::FunctionPassCtor, false, |
| 120 | RegisterPassParser<RegisterScheduler> > |
| 121 | ISHeuristic("pre-RA-sched", |
| 122 | cl::init(&createDefaultScheduler), |
| 123 | cl::desc("Instruction schedulers available (before register" |
| 124 | " allocation):")); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 125 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 126 | static RegisterScheduler |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 127 | defaultListDAGScheduler("default", "Best scheduler for the target", |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 128 | createDefaultScheduler); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 130 | namespace llvm { |
| 131 | //===--------------------------------------------------------------------===// |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 132 | /// createDefaultScheduler - This creates an instruction scheduler appropriate |
| 133 | /// for the target. |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 134 | ScheduleDAGSDNodes* createDefaultScheduler(SelectionDAGISel *IS, |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 135 | CodeGenOpt::Level OptLevel) { |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 136 | const TargetLowering &TLI = IS->getTargetLowering(); |
| 137 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 138 | if (OptLevel == CodeGenOpt::None) |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 139 | return createFastDAGScheduler(IS, OptLevel); |
Dan Gohman | 9e76fea | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 140 | if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency) |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 141 | return createTDListDAGScheduler(IS, OptLevel); |
Dan Gohman | 9e76fea | 2008-11-20 03:11:19 +0000 | [diff] [blame] | 142 | assert(TLI.getSchedulingPreference() == |
| 143 | TargetLowering::SchedulingForRegPressure && "Unknown sched type!"); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 144 | return createBURRListDAGScheduler(IS, OptLevel); |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 148 | // EmitInstrWithCustomInserter - This method should be implemented by targets |
| 149 | // that mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 150 | // instructions are special in various ways, which require special support to |
| 151 | // insert. The specified MachineInstr is created but not inserted into any |
| 152 | // basic blocks, and the scheduler passes ownership of it to this method. |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 153 | MachineBasicBlock *TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 154 | MachineBasicBlock *MBB) const { |
Torok Edwin | f368923 | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 155 | #ifndef NDEBUG |
| 156 | cerr << "If a target marks an instruction with " |
| 157 | "'usesCustomDAGSchedInserter', it must implement " |
| 158 | "TargetLowering::EmitInstrWithCustomInserter!"; |
| 159 | #endif |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 160 | llvm_unreachable(0); |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 164 | /// EmitLiveInCopy - Emit a copy for a live in physical register. If the |
| 165 | /// physical register has only a single copy use, then coalesced the copy |
| 166 | /// if possible. |
| 167 | static void EmitLiveInCopy(MachineBasicBlock *MBB, |
| 168 | MachineBasicBlock::iterator &InsertPos, |
| 169 | unsigned VirtReg, unsigned PhysReg, |
| 170 | const TargetRegisterClass *RC, |
| 171 | DenseMap<MachineInstr*, unsigned> &CopyRegMap, |
| 172 | const MachineRegisterInfo &MRI, |
| 173 | const TargetRegisterInfo &TRI, |
| 174 | const TargetInstrInfo &TII) { |
| 175 | unsigned NumUses = 0; |
| 176 | MachineInstr *UseMI = NULL; |
| 177 | for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg), |
| 178 | UE = MRI.use_end(); UI != UE; ++UI) { |
| 179 | UseMI = &*UI; |
| 180 | if (++NumUses > 1) |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | // If the number of uses is not one, or the use is not a move instruction, |
| 185 | // don't coalesce. Also, only coalesce away a virtual register to virtual |
| 186 | // register copy. |
| 187 | bool Coalesced = false; |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 188 | unsigned SrcReg, DstReg, SrcSubReg, DstSubReg; |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 189 | if (NumUses == 1 && |
Evan Cheng | 04ee5a1 | 2009-01-20 19:12:24 +0000 | [diff] [blame] | 190 | TII.isMoveInstr(*UseMI, SrcReg, DstReg, SrcSubReg, DstSubReg) && |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 191 | TargetRegisterInfo::isVirtualRegister(DstReg)) { |
| 192 | VirtReg = DstReg; |
| 193 | Coalesced = true; |
| 194 | } |
| 195 | |
| 196 | // Now find an ideal location to insert the copy. |
| 197 | MachineBasicBlock::iterator Pos = InsertPos; |
| 198 | while (Pos != MBB->begin()) { |
| 199 | MachineInstr *PrevMI = prior(Pos); |
| 200 | DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI); |
| 201 | // copyRegToReg might emit multiple instructions to do a copy. |
| 202 | unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second; |
| 203 | if (CopyDstReg && !TRI.regsOverlap(CopyDstReg, PhysReg)) |
| 204 | // This is what the BB looks like right now: |
| 205 | // r1024 = mov r0 |
| 206 | // ... |
| 207 | // r1 = mov r1024 |
| 208 | // |
| 209 | // We want to insert "r1025 = mov r1". Inserting this copy below the |
| 210 | // move to r1024 makes it impossible for that move to be coalesced. |
| 211 | // |
| 212 | // r1025 = mov r1 |
| 213 | // r1024 = mov r0 |
| 214 | // ... |
| 215 | // r1 = mov 1024 |
| 216 | // r2 = mov 1025 |
| 217 | break; // Woot! Found a good location. |
| 218 | --Pos; |
| 219 | } |
| 220 | |
David Goodwin | f1daf7d | 2009-07-08 23:10:31 +0000 | [diff] [blame] | 221 | bool Emitted = TII.copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC); |
| 222 | assert(Emitted && "Unable to issue a live-in copy instruction!\n"); |
| 223 | (void) Emitted; |
| 224 | |
| 225 | CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg)); |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 226 | if (Coalesced) { |
| 227 | if (&*InsertPos == UseMI) ++InsertPos; |
| 228 | MBB->erase(UseMI); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// EmitLiveInCopies - If this is the first basic block in the function, |
| 233 | /// and if it has live ins that need to be copied into vregs, emit the |
| 234 | /// copies into the block. |
| 235 | static void EmitLiveInCopies(MachineBasicBlock *EntryMBB, |
| 236 | const MachineRegisterInfo &MRI, |
| 237 | const TargetRegisterInfo &TRI, |
| 238 | const TargetInstrInfo &TII) { |
| 239 | if (SchedLiveInCopies) { |
| 240 | // Emit the copies at a heuristically-determined location in the block. |
| 241 | DenseMap<MachineInstr*, unsigned> CopyRegMap; |
| 242 | MachineBasicBlock::iterator InsertPos = EntryMBB->begin(); |
| 243 | for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(), |
| 244 | E = MRI.livein_end(); LI != E; ++LI) |
| 245 | if (LI->second) { |
| 246 | const TargetRegisterClass *RC = MRI.getRegClass(LI->second); |
| 247 | EmitLiveInCopy(EntryMBB, InsertPos, LI->second, LI->first, |
| 248 | RC, CopyRegMap, MRI, TRI, TII); |
| 249 | } |
| 250 | } else { |
| 251 | // Emit the copies into the top of the block. |
| 252 | for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(), |
| 253 | E = MRI.livein_end(); LI != E; ++LI) |
| 254 | if (LI->second) { |
| 255 | const TargetRegisterClass *RC = MRI.getRegClass(LI->second); |
David Goodwin | f1daf7d | 2009-07-08 23:10:31 +0000 | [diff] [blame] | 256 | bool Emitted = TII.copyRegToReg(*EntryMBB, EntryMBB->begin(), |
| 257 | LI->second, LI->first, RC, RC); |
| 258 | assert(Emitted && "Unable to issue a live-in copy instruction!\n"); |
| 259 | (void) Emitted; |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 264 | //===----------------------------------------------------------------------===// |
| 265 | // SelectionDAGISel code |
| 266 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 267 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 268 | SelectionDAGISel::SelectionDAGISel(TargetMachine &tm, CodeGenOpt::Level OL) : |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 269 | FunctionPass(&ID), TM(tm), TLI(*tm.getTargetLowering()), |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 270 | FuncInfo(new FunctionLoweringInfo(TLI)), |
| 271 | CurDAG(new SelectionDAG(TLI, *FuncInfo)), |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 272 | SDL(new SelectionDAGLowering(*CurDAG, TLI, *FuncInfo, OL)), |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 273 | GFI(), |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 274 | OptLevel(OL), |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 275 | DAGSize(0) |
| 276 | {} |
| 277 | |
| 278 | SelectionDAGISel::~SelectionDAGISel() { |
| 279 | delete SDL; |
| 280 | delete CurDAG; |
| 281 | delete FuncInfo; |
| 282 | } |
| 283 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 284 | unsigned SelectionDAGISel::MakeReg(MVT VT) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 285 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 288 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Jim Laskey | c7c3f11 | 2006-10-16 20:52:31 +0000 | [diff] [blame] | 289 | AU.addRequired<AliasAnalysis>(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 290 | AU.addRequired<GCModuleInfo>(); |
Devang Patel | 6e7a161 | 2009-01-09 19:11:50 +0000 | [diff] [blame] | 291 | AU.addRequired<DwarfWriter>(); |
Chris Lattner | c8d288f | 2007-03-31 04:18:03 +0000 | [diff] [blame] | 292 | AU.setPreservesAll(); |
Chris Lattner | 495a0b5 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 295 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
Dan Gohman | 4344a5d | 2008-09-09 23:05:00 +0000 | [diff] [blame] | 296 | // Do some sanity-checking on the command-line options. |
| 297 | assert((!EnableFastISelVerbose || EnableFastISel) && |
| 298 | "-fast-isel-verbose requires -fast-isel"); |
| 299 | assert((!EnableFastISelAbort || EnableFastISel) && |
| 300 | "-fast-isel-abort requires -fast-isel"); |
| 301 | |
Devang Patel | 16f2ffd | 2009-04-16 02:33:41 +0000 | [diff] [blame] | 302 | // Do not codegen any 'available_externally' functions at all, they have |
| 303 | // definitions outside the translation unit. |
| 304 | if (Fn.hasAvailableExternallyLinkage()) |
| 305 | return false; |
| 306 | |
| 307 | |
Dan Gohman | 5f43f92 | 2007-08-27 16:26:13 +0000 | [diff] [blame] | 308 | // Get alias analysis for load/store combining. |
| 309 | AA = &getAnalysis<AliasAnalysis>(); |
| 310 | |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 311 | TargetMachine &TM = TLI.getTargetMachine(); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 312 | MF = &MachineFunction::construct(&Fn, TM); |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 313 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 314 | const TargetRegisterInfo &TRI = *TM.getRegisterInfo(); |
| 315 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 316 | if (MF->getFunction()->hasGC()) |
| 317 | GFI = &getAnalysis<GCModuleInfo>().getFunctionInfo(*MF->getFunction()); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 318 | else |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 319 | GFI = 0; |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 320 | RegInfo = &MF->getRegInfo(); |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 321 | DOUT << "\n\n\n=== " << Fn.getName() << "\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 322 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 323 | MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
| 324 | DwarfWriter *DW = getAnalysisIfAvailable<DwarfWriter>(); |
Owen Anderson | 5dcaceb | 2009-07-09 18:44:09 +0000 | [diff] [blame] | 325 | CurDAG->init(*MF, MMI, DW); |
Devang Patel | b51d40c | 2009-02-03 18:46:32 +0000 | [diff] [blame] | 326 | FuncInfo->set(Fn, *MF, *CurDAG, EnableFastISel); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 327 | SDL->init(GFI, *AA); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 328 | |
Dale Johannesen | 1532f3d | 2008-04-02 00:25:04 +0000 | [diff] [blame] | 329 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 330 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator())) |
| 331 | // Mark landing pad. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 332 | FuncInfo->MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Duncan Sands | 9fac0b5 | 2007-06-06 10:05:18 +0000 | [diff] [blame] | 333 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 334 | SelectAllBasicBlocks(Fn, *MF, MMI, DW, TII); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 335 | |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 336 | // If the first basic block in the function has live ins that need to be |
| 337 | // copied into vregs, emit the copies into the top of the block before |
| 338 | // emitting the code for the block. |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 339 | EmitLiveInCopies(MF->begin(), *RegInfo, TRI, TII); |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 340 | |
Evan Cheng | ad2070c | 2007-02-10 02:43:39 +0000 | [diff] [blame] | 341 | // Add function live-ins to entry block live-in set. |
Dan Gohman | 8a11053 | 2008-09-05 22:59:21 +0000 | [diff] [blame] | 342 | for (MachineRegisterInfo::livein_iterator I = RegInfo->livein_begin(), |
| 343 | E = RegInfo->livein_end(); I != E; ++I) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 344 | MF->begin()->addLiveIn(I->first); |
Evan Cheng | ad2070c | 2007-02-10 02:43:39 +0000 | [diff] [blame] | 345 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 346 | #ifndef NDEBUG |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 347 | assert(FuncInfo->CatchInfoFound.size() == FuncInfo->CatchInfoLost.size() && |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 348 | "Not all catch info was assigned to a landing pad!"); |
| 349 | #endif |
| 350 | |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 351 | FuncInfo->clear(); |
| 352 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 356 | static void copyCatchInfo(BasicBlock *SrcBB, BasicBlock *DestBB, |
| 357 | MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 358 | for (BasicBlock::iterator I = SrcBB->begin(), E = --SrcBB->end(); I != E; ++I) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 359 | if (EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) { |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 360 | // Apply the catch info to DestBB. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 361 | AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]); |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 362 | #ifndef NDEBUG |
Duncan Sands | 560a737 | 2007-11-15 09:54:37 +0000 | [diff] [blame] | 363 | if (!FLI.MBBMap[SrcBB]->isLandingPad()) |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 364 | FLI.CatchInfoFound.insert(EHSel); |
Duncan Sands | f407082 | 2007-06-15 19:04:19 +0000 | [diff] [blame] | 365 | #endif |
| 366 | } |
| 367 | } |
| 368 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 369 | /// IsFixedFrameObjectWithPosOffset - Check if object is a fixed frame object and |
| 370 | /// whether object offset >= 0. |
| 371 | static bool |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 372 | IsFixedFrameObjectWithPosOffset(MachineFrameInfo *MFI, SDValue Op) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 373 | if (!isa<FrameIndexSDNode>(Op)) return false; |
| 374 | |
| 375 | FrameIndexSDNode * FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op); |
| 376 | int FrameIdx = FrameIdxNode->getIndex(); |
| 377 | return MFI->isFixedObjectIndex(FrameIdx) && |
| 378 | MFI->getObjectOffset(FrameIdx) >= 0; |
| 379 | } |
| 380 | |
| 381 | /// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could |
| 382 | /// possibly be overwritten when lowering the outgoing arguments in a tail |
| 383 | /// call. Currently the implementation of this call is very conservative and |
| 384 | /// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with |
| 385 | /// virtual registers would be overwritten by direct lowering. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 386 | static bool IsPossiblyOverwrittenArgumentOfTailCall(SDValue Op, |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 387 | MachineFrameInfo *MFI) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 388 | RegisterSDNode * OpReg = NULL; |
| 389 | if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS || |
| 390 | (Op.getOpcode()== ISD::CopyFromReg && |
| 391 | (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) && |
| 392 | (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) || |
| 393 | (Op.getOpcode() == ISD::LOAD && |
| 394 | IsFixedFrameObjectWithPosOffset(MFI, Op.getOperand(1))) || |
| 395 | (Op.getOpcode() == ISD::MERGE_VALUES && |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 396 | Op.getOperand(Op.getResNo()).getOpcode() == ISD::LOAD && |
| 397 | IsFixedFrameObjectWithPosOffset(MFI, Op.getOperand(Op.getResNo()). |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 398 | getOperand(1)))) |
| 399 | return true; |
| 400 | return false; |
| 401 | } |
| 402 | |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 403 | /// CheckDAGForTailCallsAndFixThem - This Function looks for CALL nodes in the |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 404 | /// DAG and fixes their tailcall attribute operand. |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 405 | static void CheckDAGForTailCallsAndFixThem(SelectionDAG &DAG, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 406 | const TargetLowering& TLI) { |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 407 | SDNode * Ret = NULL; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 408 | SDValue Terminator = DAG.getRoot(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 409 | |
| 410 | // Find RET node. |
| 411 | if (Terminator.getOpcode() == ISD::RET) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 412 | Ret = Terminator.getNode(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | // Fix tail call attribute of CALL nodes. |
| 416 | for (SelectionDAG::allnodes_iterator BE = DAG.allnodes_begin(), |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 417 | BI = DAG.allnodes_end(); BI != BE; ) { |
| 418 | --BI; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 419 | if (CallSDNode *TheCall = dyn_cast<CallSDNode>(BI)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 420 | SDValue OpRet(Ret, 0); |
| 421 | SDValue OpCall(BI, 0); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 422 | bool isMarkedTailCall = TheCall->isTailCall(); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 423 | // If CALL node has tail call attribute set to true and the call is not |
| 424 | // eligible (no RET or the target rejects) the attribute is fixed to |
Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 425 | // false. The TargetLowering::IsEligibleForTailCallOptimization function |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 426 | // must correctly identify tail call optimizable calls. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 427 | if (!isMarkedTailCall) continue; |
| 428 | if (Ret==NULL || |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 429 | !TLI.IsEligibleForTailCallOptimization(TheCall, OpRet, DAG)) { |
| 430 | // Not eligible. Mark CALL node as non tail call. Note that we |
| 431 | // can modify the call node in place since calls are not CSE'd. |
| 432 | TheCall->setNotTailCall(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 433 | } else { |
| 434 | // Look for tail call clobbered arguments. Emit a series of |
| 435 | // copyto/copyfrom virtual register nodes to protect them. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 436 | SmallVector<SDValue, 32> Ops; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 437 | SDValue Chain = TheCall->getChain(), InFlag; |
| 438 | Ops.push_back(Chain); |
| 439 | Ops.push_back(TheCall->getCallee()); |
| 440 | for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; ++i) { |
| 441 | SDValue Arg = TheCall->getArg(i); |
| 442 | bool isByVal = TheCall->getArgFlags(i).isByVal(); |
| 443 | MachineFunction &MF = DAG.getMachineFunction(); |
| 444 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 445 | if (!isByVal && |
| 446 | IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) { |
| 447 | MVT VT = Arg.getValueType(); |
| 448 | unsigned VReg = MF.getRegInfo(). |
| 449 | createVirtualRegister(TLI.getRegClassFor(VT)); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 450 | Chain = DAG.getCopyToReg(Chain, Arg.getDebugLoc(), |
Dale Johannesen | c460ae9 | 2009-02-04 00:13:36 +0000 | [diff] [blame] | 451 | VReg, Arg, InFlag); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 452 | InFlag = Chain.getValue(1); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 453 | Arg = DAG.getCopyFromReg(Chain, Arg.getDebugLoc(), |
Dale Johannesen | c460ae9 | 2009-02-04 00:13:36 +0000 | [diff] [blame] | 454 | VReg, VT, InFlag); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 455 | Chain = Arg.getValue(1); |
| 456 | InFlag = Arg.getValue(2); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 457 | } |
| 458 | Ops.push_back(Arg); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 459 | Ops.push_back(TheCall->getArgFlagsVal(i)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 460 | } |
| 461 | // Link in chain of CopyTo/CopyFromReg. |
| 462 | Ops[0] = Chain; |
| 463 | DAG.UpdateNodeOperands(OpCall, Ops.begin(), Ops.size()); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 469 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, |
| 470 | BasicBlock::iterator Begin, |
Dan Gohman | 5edd361 | 2008-08-28 20:28:56 +0000 | [diff] [blame] | 471 | BasicBlock::iterator End) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 472 | SDL->setCurrentBasicBlock(BB); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 473 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 474 | // Lower all of the non-terminator instructions. |
| 475 | for (BasicBlock::iterator I = Begin; I != End; ++I) |
| 476 | if (!isa<TerminatorInst>(I)) |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 477 | SDL->visit(*I); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 478 | |
| 479 | // Ensure that all instructions which are used outside of their defining |
| 480 | // blocks are available as virtual registers. Invoke is handled elsewhere. |
| 481 | for (BasicBlock::iterator I = Begin; I != End; ++I) |
Dan Gohman | ad62f53 | 2009-04-23 23:13:24 +0000 | [diff] [blame] | 482 | if (!isa<PHINode>(I) && !isa<InvokeInst>(I)) |
| 483 | SDL->CopyToExportRegsIfNeeded(I); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 484 | |
| 485 | // Handle PHI nodes in successor blocks. |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 486 | if (End == LLVMBB->end()) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 487 | HandlePHINodesInSuccessorBlocks(LLVMBB); |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 488 | |
| 489 | // Lower the terminator after the copies are emitted. |
| 490 | SDL->visit(*LLVMBB->getTerminator()); |
| 491 | } |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 492 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 493 | // Make sure the root of the DAG is up-to-date. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 494 | CurDAG->setRoot(SDL->getControlRoot()); |
Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 495 | |
| 496 | // Check whether calls in this block are real tail calls. Fix up CALL nodes |
| 497 | // with correct tailcall attribute so that the target can rely on the tailcall |
| 498 | // attribute indicating whether the call is really eligible for tail call |
| 499 | // optimization. |
Dan Gohman | 1937e2f | 2008-09-16 01:42:28 +0000 | [diff] [blame] | 500 | if (PerformTailCallOpt) |
| 501 | CheckDAGForTailCallsAndFixThem(*CurDAG, TLI); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 502 | |
| 503 | // Final step, emit the lowered DAG as machine code. |
| 504 | CodeGenAndEmitDAG(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 505 | SDL->clear(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 508 | void SelectionDAGISel::ComputeLiveOutVRegInfo() { |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 509 | SmallPtrSet<SDNode*, 128> VisitedNodes; |
| 510 | SmallVector<SDNode*, 128> Worklist; |
| 511 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 512 | Worklist.push_back(CurDAG->getRoot().getNode()); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 513 | |
| 514 | APInt Mask; |
| 515 | APInt KnownZero; |
| 516 | APInt KnownOne; |
| 517 | |
| 518 | while (!Worklist.empty()) { |
| 519 | SDNode *N = Worklist.back(); |
| 520 | Worklist.pop_back(); |
| 521 | |
| 522 | // If we've already seen this node, ignore it. |
| 523 | if (!VisitedNodes.insert(N)) |
| 524 | continue; |
| 525 | |
| 526 | // Otherwise, add all chain operands to the worklist. |
| 527 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 528 | if (N->getOperand(i).getValueType() == MVT::Other) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 529 | Worklist.push_back(N->getOperand(i).getNode()); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 530 | |
| 531 | // If this is a CopyToReg with a vreg dest, process it. |
| 532 | if (N->getOpcode() != ISD::CopyToReg) |
| 533 | continue; |
| 534 | |
| 535 | unsigned DestReg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); |
| 536 | if (!TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 537 | continue; |
| 538 | |
| 539 | // Ignore non-scalar or non-integer values. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 540 | SDValue Src = N->getOperand(2); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 541 | MVT SrcVT = Src.getValueType(); |
| 542 | if (!SrcVT.isInteger() || SrcVT.isVector()) |
| 543 | continue; |
| 544 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 545 | unsigned NumSignBits = CurDAG->ComputeNumSignBits(Src); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 546 | Mask = APInt::getAllOnesValue(SrcVT.getSizeInBits()); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 547 | CurDAG->ComputeMaskedBits(Src, Mask, KnownZero, KnownOne); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 548 | |
| 549 | // Only install this information if it tells us something. |
| 550 | if (NumSignBits != 1 || KnownZero != 0 || KnownOne != 0) { |
| 551 | DestReg -= TargetRegisterInfo::FirstVirtualRegister; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 552 | FunctionLoweringInfo &FLI = CurDAG->getFunctionLoweringInfo(); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 553 | if (DestReg >= FLI.LiveOutRegInfo.size()) |
| 554 | FLI.LiveOutRegInfo.resize(DestReg+1); |
| 555 | FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[DestReg]; |
| 556 | LOI.NumSignBits = NumSignBits; |
Dan Gohman | a80efce | 2009-03-27 23:55:04 +0000 | [diff] [blame] | 557 | LOI.KnownOne = KnownOne; |
| 558 | LOI.KnownZero = KnownZero; |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 563 | void SelectionDAGISel::CodeGenAndEmitDAG() { |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 564 | std::string GroupName; |
| 565 | if (TimePassesIsEnabled) |
| 566 | GroupName = "Instruction Selection and Scheduling"; |
| 567 | std::string BlockName; |
| 568 | if (ViewDAGCombine1 || ViewLegalizeTypesDAGs || ViewLegalizeDAGs || |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 569 | ViewDAGCombine2 || ViewDAGCombineLT || ViewISelDAGs || ViewSchedDAGs || |
| 570 | ViewSUnitDAGs) |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 571 | BlockName = CurDAG->getMachineFunction().getFunction()->getName() + ':' + |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 572 | BB->getBasicBlock()->getName(); |
| 573 | |
| 574 | DOUT << "Initial selection DAG:\n"; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 575 | DEBUG(CurDAG->dump()); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 576 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 577 | if (ViewDAGCombine1) CurDAG->viewGraph("dag-combine1 input for " + BlockName); |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 578 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 579 | // Run the DAG combiner in pre-legalize mode. |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 580 | if (TimePassesIsEnabled) { |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 581 | NamedRegionTimer T("DAG Combining 1", GroupName); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 582 | CurDAG->Combine(Unrestricted, *AA, OptLevel); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 583 | } else { |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 584 | CurDAG->Combine(Unrestricted, *AA, OptLevel); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 585 | } |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 586 | |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 587 | DOUT << "Optimized lowered selection DAG:\n"; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 588 | DEBUG(CurDAG->dump()); |
Duncan Sands | f00e74f | 2008-07-17 17:06:03 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 590 | // Second step, hack on the DAG until it only uses operations and types that |
| 591 | // the target supports. |
Duncan Sands | 7cb0787 | 2008-10-27 08:42:46 +0000 | [diff] [blame] | 592 | if (!DisableLegalizeTypes) { |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 593 | if (ViewLegalizeTypesDAGs) CurDAG->viewGraph("legalize-types input for " + |
| 594 | BlockName); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 595 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 596 | bool Changed; |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 597 | if (TimePassesIsEnabled) { |
| 598 | NamedRegionTimer T("Type Legalization", GroupName); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 599 | Changed = CurDAG->LegalizeTypes(); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 600 | } else { |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 601 | Changed = CurDAG->LegalizeTypes(); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | DOUT << "Type-legalized selection DAG:\n"; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 605 | DEBUG(CurDAG->dump()); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 606 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 607 | if (Changed) { |
| 608 | if (ViewDAGCombineLT) |
| 609 | CurDAG->viewGraph("dag-combine-lt input for " + BlockName); |
| 610 | |
| 611 | // Run the DAG combiner in post-type-legalize mode. |
| 612 | if (TimePassesIsEnabled) { |
| 613 | NamedRegionTimer T("DAG Combining after legalize types", GroupName); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 614 | CurDAG->Combine(NoIllegalTypes, *AA, OptLevel); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 615 | } else { |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 616 | CurDAG->Combine(NoIllegalTypes, *AA, OptLevel); |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | DOUT << "Optimized type-legalized selection DAG:\n"; |
| 620 | DEBUG(CurDAG->dump()); |
| 621 | } |
Eli Friedman | 5c22c80 | 2009-05-23 12:35:30 +0000 | [diff] [blame] | 622 | |
| 623 | if (TimePassesIsEnabled) { |
| 624 | NamedRegionTimer T("Vector Legalization", GroupName); |
| 625 | Changed = CurDAG->LegalizeVectors(); |
| 626 | } else { |
| 627 | Changed = CurDAG->LegalizeVectors(); |
| 628 | } |
| 629 | |
| 630 | if (Changed) { |
| 631 | if (TimePassesIsEnabled) { |
| 632 | NamedRegionTimer T("Type Legalization 2", GroupName); |
| 633 | Changed = CurDAG->LegalizeTypes(); |
| 634 | } else { |
| 635 | Changed = CurDAG->LegalizeTypes(); |
| 636 | } |
| 637 | |
| 638 | if (ViewDAGCombineLT) |
| 639 | CurDAG->viewGraph("dag-combine-lv input for " + BlockName); |
| 640 | |
| 641 | // Run the DAG combiner in post-type-legalize mode. |
| 642 | if (TimePassesIsEnabled) { |
| 643 | NamedRegionTimer T("DAG Combining after legalize vectors", GroupName); |
| 644 | CurDAG->Combine(NoIllegalOperations, *AA, OptLevel); |
| 645 | } else { |
| 646 | CurDAG->Combine(NoIllegalOperations, *AA, OptLevel); |
| 647 | } |
| 648 | |
| 649 | DOUT << "Optimized vector-legalized selection DAG:\n"; |
| 650 | DEBUG(CurDAG->dump()); |
| 651 | } |
Chris Lattner | 70587ea | 2008-07-10 23:37:50 +0000 | [diff] [blame] | 652 | } |
Duncan Sands | f00e74f | 2008-07-17 17:06:03 +0000 | [diff] [blame] | 653 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 654 | if (ViewLegalizeDAGs) CurDAG->viewGraph("legalize input for " + BlockName); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 655 | |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 656 | if (TimePassesIsEnabled) { |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 657 | NamedRegionTimer T("DAG Legalization", GroupName); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 658 | CurDAG->Legalize(DisableLegalizeTypes, OptLevel); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 659 | } else { |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 660 | CurDAG->Legalize(DisableLegalizeTypes, OptLevel); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 661 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 662 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 663 | DOUT << "Legalized selection DAG:\n"; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 664 | DEBUG(CurDAG->dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 665 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 666 | if (ViewDAGCombine2) CurDAG->viewGraph("dag-combine2 input for " + BlockName); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 667 | |
Chris Lattner | af21d55 | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 668 | // Run the DAG combiner in post-legalize mode. |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 669 | if (TimePassesIsEnabled) { |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 670 | NamedRegionTimer T("DAG Combining 2", GroupName); |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 671 | CurDAG->Combine(NoIllegalOperations, *AA, OptLevel); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 672 | } else { |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 673 | CurDAG->Combine(NoIllegalOperations, *AA, OptLevel); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 674 | } |
Nate Begeman | 2300f55 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 675 | |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 676 | DOUT << "Optimized legalized selection DAG:\n"; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 677 | DEBUG(CurDAG->dump()); |
Dan Gohman | 417e11b | 2007-10-08 15:12:17 +0000 | [diff] [blame] | 678 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 679 | if (ViewISelDAGs) CurDAG->viewGraph("isel input for " + BlockName); |
Chris Lattner | ead0d88 | 2008-06-17 06:09:18 +0000 | [diff] [blame] | 680 | |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 681 | if (OptLevel != CodeGenOpt::None) |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 682 | ComputeLiveOutVRegInfo(); |
Evan Cheng | 552c4a8 | 2006-04-28 02:09:19 +0000 | [diff] [blame] | 683 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 684 | // Third, instruction select all of the operations to machine code, adding the |
| 685 | // code to the MachineBasicBlock. |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 686 | if (TimePassesIsEnabled) { |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 687 | NamedRegionTimer T("Instruction Selection", GroupName); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 688 | InstructionSelect(); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 689 | } else { |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 690 | InstructionSelect(); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 691 | } |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 692 | |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 693 | DOUT << "Selected selection DAG:\n"; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 694 | DEBUG(CurDAG->dump()); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 695 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 696 | if (ViewSchedDAGs) CurDAG->viewGraph("scheduler input for " + BlockName); |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 697 | |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 698 | // Schedule machine code. |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 699 | ScheduleDAGSDNodes *Scheduler = CreateScheduler(); |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 700 | if (TimePassesIsEnabled) { |
| 701 | NamedRegionTimer T("Instruction Scheduling", GroupName); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 702 | Scheduler->Run(CurDAG, BB, BB->end()); |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 703 | } else { |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 704 | Scheduler->Run(CurDAG, BB, BB->end()); |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Dan Gohman | 462dc7f | 2008-07-21 20:00:07 +0000 | [diff] [blame] | 707 | if (ViewSUnitDAGs) Scheduler->viewGraph(); |
| 708 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 709 | // Emit machine code to BB. This can change 'BB' to the last block being |
| 710 | // inserted into. |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 711 | if (TimePassesIsEnabled) { |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 712 | NamedRegionTimer T("Instruction Creation", GroupName); |
| 713 | BB = Scheduler->EmitSchedule(); |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 714 | } else { |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 715 | BB = Scheduler->EmitSchedule(); |
| 716 | } |
| 717 | |
| 718 | // Free the scheduler state. |
| 719 | if (TimePassesIsEnabled) { |
| 720 | NamedRegionTimer T("Instruction Scheduling Cleanup", GroupName); |
| 721 | delete Scheduler; |
| 722 | } else { |
| 723 | delete Scheduler; |
Evan Cheng | ebffb66 | 2008-07-01 17:59:20 +0000 | [diff] [blame] | 724 | } |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 725 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 726 | DOUT << "Selected machine code:\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 727 | DEBUG(BB->dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 728 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 729 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 730 | void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn, |
| 731 | MachineFunction &MF, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 732 | MachineModuleInfo *MMI, |
Devang Patel | 83489bb | 2009-01-13 00:35:13 +0000 | [diff] [blame] | 733 | DwarfWriter *DW, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 734 | const TargetInstrInfo &TII) { |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 735 | // Initialize the Fast-ISel state, if needed. |
| 736 | FastISel *FastIS = 0; |
| 737 | if (EnableFastISel) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 738 | FastIS = TLI.createFastISel(MF, MMI, DW, |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 739 | FuncInfo->ValueMap, |
| 740 | FuncInfo->MBBMap, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 741 | FuncInfo->StaticAllocaMap |
| 742 | #ifndef NDEBUG |
| 743 | , FuncInfo->CatchInfoLost |
| 744 | #endif |
| 745 | ); |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 746 | |
| 747 | // Iterate over all basic blocks in the function. |
Evan Cheng | 39fd6e8 | 2008-08-07 00:43:25 +0000 | [diff] [blame] | 748 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { |
| 749 | BasicBlock *LLVMBB = &*I; |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 750 | BB = FuncInfo->MBBMap[LLVMBB]; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 751 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 752 | BasicBlock::iterator const Begin = LLVMBB->begin(); |
| 753 | BasicBlock::iterator const End = LLVMBB->end(); |
Evan Cheng | 9f11850 | 2008-09-08 16:01:27 +0000 | [diff] [blame] | 754 | BasicBlock::iterator BI = Begin; |
Dan Gohman | 5edd361 | 2008-08-28 20:28:56 +0000 | [diff] [blame] | 755 | |
| 756 | // Lower any arguments needed in this block if this is the entry block. |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 757 | bool SuppressFastISel = false; |
| 758 | if (LLVMBB == &Fn.getEntryBlock()) { |
Dan Gohman | 5edd361 | 2008-08-28 20:28:56 +0000 | [diff] [blame] | 759 | LowerArguments(LLVMBB); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 760 | |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 761 | // If any of the arguments has the byval attribute, forgo |
| 762 | // fast-isel in the entry block. |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 763 | if (FastIS) { |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 764 | unsigned j = 1; |
| 765 | for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(); |
| 766 | I != E; ++I, ++j) |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 767 | if (Fn.paramHasAttr(j, Attribute::ByVal)) { |
Dan Gohman | 77ca41e | 2008-09-25 17:21:42 +0000 | [diff] [blame] | 768 | if (EnableFastISelVerbose || EnableFastISelAbort) |
| 769 | cerr << "FastISel skips entry block due to byval argument\n"; |
Dan Gohman | 33134c4 | 2008-09-25 17:05:24 +0000 | [diff] [blame] | 770 | SuppressFastISel = true; |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 776 | if (MMI && BB->isLandingPad()) { |
| 777 | // Add a label to mark the beginning of the landing pad. Deletion of the |
| 778 | // landing pad can thus be detected via the MachineModuleInfo. |
| 779 | unsigned LabelID = MMI->addLandingPad(BB); |
| 780 | |
| 781 | const TargetInstrDesc &II = TII.get(TargetInstrInfo::EH_LABEL); |
Bill Wendling | b288487 | 2009-02-03 01:55:42 +0000 | [diff] [blame] | 782 | BuildMI(BB, SDL->getCurDebugLoc(), II).addImm(LabelID); |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 783 | |
| 784 | // Mark exception register as live in. |
| 785 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 786 | if (Reg) BB->addLiveIn(Reg); |
| 787 | |
| 788 | // Mark exception selector register as live in. |
| 789 | Reg = TLI.getExceptionSelectorRegister(); |
| 790 | if (Reg) BB->addLiveIn(Reg); |
| 791 | |
| 792 | // FIXME: Hack around an exception handling flaw (PR1508): the personality |
| 793 | // function and list of typeids logically belong to the invoke (or, if you |
| 794 | // like, the basic block containing the invoke), and need to be associated |
| 795 | // with it in the dwarf exception handling tables. Currently however the |
| 796 | // information is provided by an intrinsic (eh.selector) that can be moved |
| 797 | // to unexpected places by the optimizers: if the unwind edge is critical, |
| 798 | // then breaking it can result in the intrinsics being in the successor of |
| 799 | // the landing pad, not the landing pad itself. This results in exceptions |
| 800 | // not being caught because no typeids are associated with the invoke. |
| 801 | // This may not be the only way things can go wrong, but it is the only way |
| 802 | // we try to work around for the moment. |
| 803 | BranchInst *Br = dyn_cast<BranchInst>(LLVMBB->getTerminator()); |
| 804 | |
| 805 | if (Br && Br->isUnconditional()) { // Critical edge? |
| 806 | BasicBlock::iterator I, E; |
| 807 | for (I = LLVMBB->begin(), E = --LLVMBB->end(); I != E; ++I) |
| 808 | if (isa<EHSelectorInst>(I)) |
| 809 | break; |
| 810 | |
| 811 | if (I == E) |
| 812 | // No catch info found - try to extract some from the successor. |
| 813 | copyCatchInfo(Br->getSuccessor(0), LLVMBB, MMI, *FuncInfo); |
| 814 | } |
| 815 | } |
| 816 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 817 | // Before doing SelectionDAG ISel, see if FastISel has been requested. |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 818 | if (FastIS && !SuppressFastISel) { |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 819 | // Emit code for any incoming arguments. This must happen before |
| 820 | // beginning FastISel on the entry block. |
| 821 | if (LLVMBB == &Fn.getEntryBlock()) { |
| 822 | CurDAG->setRoot(SDL->getControlRoot()); |
| 823 | CodeGenAndEmitDAG(); |
| 824 | SDL->clear(); |
| 825 | } |
Dan Gohman | 241f464 | 2008-10-04 00:56:36 +0000 | [diff] [blame] | 826 | FastIS->startNewBlock(BB); |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 827 | // Do FastISel on as many instructions as possible. |
| 828 | for (; BI != End; ++BI) { |
| 829 | // Just before the terminator instruction, insert instructions to |
| 830 | // feed PHI nodes in successor blocks. |
| 831 | if (isa<TerminatorInst>(BI)) |
| 832 | if (!HandlePHINodesInSuccessorBlocksFast(LLVMBB, FastIS)) { |
Dan Gohman | 4344a5d | 2008-09-09 23:05:00 +0000 | [diff] [blame] | 833 | if (EnableFastISelVerbose || EnableFastISelAbort) { |
Dan Gohman | 293d5f8 | 2008-09-09 22:06:46 +0000 | [diff] [blame] | 834 | cerr << "FastISel miss: "; |
| 835 | BI->dump(); |
| 836 | } |
Torok Edwin | f368923 | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 837 | assert(!EnableFastISelAbort && |
| 838 | "FastISel didn't handle a PHI in a successor"); |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 839 | break; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 840 | } |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 841 | |
| 842 | // First try normal tablegen-generated "fast" selection. |
| 843 | if (FastIS->SelectInstruction(BI)) |
| 844 | continue; |
| 845 | |
| 846 | // Next, try calling the target to attempt to handle the instruction. |
| 847 | if (FastIS->TargetSelectInstruction(BI)) |
| 848 | continue; |
| 849 | |
| 850 | // Then handle certain instructions as single-LLVM-Instruction blocks. |
| 851 | if (isa<CallInst>(BI)) { |
| 852 | if (EnableFastISelVerbose || EnableFastISelAbort) { |
| 853 | cerr << "FastISel missed call: "; |
| 854 | BI->dump(); |
| 855 | } |
| 856 | |
| 857 | if (BI->getType() != Type::VoidTy) { |
| 858 | unsigned &R = FuncInfo->ValueMap[BI]; |
| 859 | if (!R) |
| 860 | R = FuncInfo->CreateRegForValue(BI); |
| 861 | } |
| 862 | |
Devang Patel | 390f3ac | 2009-04-16 01:33:10 +0000 | [diff] [blame] | 863 | SDL->setCurDebugLoc(FastIS->getCurDebugLoc()); |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 864 | SelectBasicBlock(LLVMBB, BI, next(BI)); |
Dan Gohman | 241f464 | 2008-10-04 00:56:36 +0000 | [diff] [blame] | 865 | // If the instruction was codegen'd with multiple blocks, |
| 866 | // inform the FastISel object where to resume inserting. |
| 867 | FastIS->setCurrentBlock(BB); |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 868 | continue; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 869 | } |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 870 | |
| 871 | // Otherwise, give up on FastISel for the rest of the block. |
| 872 | // For now, be a little lenient about non-branch terminators. |
| 873 | if (!isa<TerminatorInst>(BI) || isa<BranchInst>(BI)) { |
| 874 | if (EnableFastISelVerbose || EnableFastISelAbort) { |
| 875 | cerr << "FastISel miss: "; |
| 876 | BI->dump(); |
| 877 | } |
| 878 | if (EnableFastISelAbort) |
| 879 | // The "fast" selector couldn't handle something and bailed. |
| 880 | // For the purpose of debugging, just abort. |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 881 | llvm_unreachable("FastISel didn't select the entire block"); |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 882 | } |
| 883 | break; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
Dan Gohman | d2ff647 | 2008-09-02 20:17:56 +0000 | [diff] [blame] | 887 | // Run SelectionDAG instruction selection on the remainder of the block |
| 888 | // not handled by FastISel. If FastISel is not run, this is the entire |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 889 | // block. |
Devang Patel | 390f3ac | 2009-04-16 01:33:10 +0000 | [diff] [blame] | 890 | if (BI != End) { |
| 891 | // If FastISel is run and it has known DebugLoc then use it. |
| 892 | if (FastIS && !FastIS->getCurDebugLoc().isUnknown()) |
| 893 | SDL->setCurDebugLoc(FastIS->getCurDebugLoc()); |
Evan Cheng | 9f11850 | 2008-09-08 16:01:27 +0000 | [diff] [blame] | 894 | SelectBasicBlock(LLVMBB, BI, End); |
Devang Patel | 390f3ac | 2009-04-16 01:33:10 +0000 | [diff] [blame] | 895 | } |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 896 | |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 897 | FinishBasicBlock(); |
Evan Cheng | 39fd6e8 | 2008-08-07 00:43:25 +0000 | [diff] [blame] | 898 | } |
Dan Gohman | a43abd1 | 2008-09-29 21:55:50 +0000 | [diff] [blame] | 899 | |
| 900 | delete FastIS; |
Dan Gohman | 0e5f130 | 2008-07-07 23:02:41 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 903 | void |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 904 | SelectionDAGISel::FinishBasicBlock() { |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 905 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 906 | DOUT << "Target-post-processed machine code:\n"; |
| 907 | DEBUG(BB->dump()); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 908 | |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 909 | DOUT << "Total amount of phi nodes to update: " |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 910 | << SDL->PHINodesToUpdate.size() << "\n"; |
| 911 | DEBUG(for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) |
| 912 | DOUT << "Node " << i << " : (" << SDL->PHINodesToUpdate[i].first |
| 913 | << ", " << SDL->PHINodesToUpdate[i].second << ")\n";); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 914 | |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 915 | // 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] | 916 | // PHI nodes in successors. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 917 | if (SDL->SwitchCases.empty() && |
| 918 | SDL->JTCases.empty() && |
| 919 | SDL->BitTestCases.empty()) { |
| 920 | for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) { |
| 921 | MachineInstr *PHI = SDL->PHINodesToUpdate[i].first; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 922 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 923 | "This is not a machine PHI node that we are updating!"); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 924 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[i].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 925 | false)); |
| 926 | PHI->addOperand(MachineOperand::CreateMBB(BB)); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 927 | } |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 928 | SDL->PHINodesToUpdate.clear(); |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 929 | return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 930 | } |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 931 | |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 932 | for (unsigned i = 0, e = SDL->BitTestCases.size(); i != e; ++i) { |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 933 | // Lower header first, if it wasn't already lowered |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 934 | if (!SDL->BitTestCases[i].Emitted) { |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 935 | // Set the current basic block to the mbb we wish to insert the code into |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 936 | BB = SDL->BitTestCases[i].Parent; |
| 937 | SDL->setCurrentBasicBlock(BB); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 938 | // Emit the code |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 939 | SDL->visitBitTestHeader(SDL->BitTestCases[i]); |
| 940 | CurDAG->setRoot(SDL->getRoot()); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 941 | CodeGenAndEmitDAG(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 942 | SDL->clear(); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 945 | for (unsigned j = 0, ej = SDL->BitTestCases[i].Cases.size(); j != ej; ++j) { |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 946 | // Set the current basic block to the mbb we wish to insert the code into |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 947 | BB = SDL->BitTestCases[i].Cases[j].ThisBB; |
| 948 | SDL->setCurrentBasicBlock(BB); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 949 | // Emit the code |
| 950 | if (j+1 != ej) |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 951 | SDL->visitBitTestCase(SDL->BitTestCases[i].Cases[j+1].ThisBB, |
| 952 | SDL->BitTestCases[i].Reg, |
| 953 | SDL->BitTestCases[i].Cases[j]); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 954 | else |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 955 | SDL->visitBitTestCase(SDL->BitTestCases[i].Default, |
| 956 | SDL->BitTestCases[i].Reg, |
| 957 | SDL->BitTestCases[i].Cases[j]); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 958 | |
| 959 | |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 960 | CurDAG->setRoot(SDL->getRoot()); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 961 | CodeGenAndEmitDAG(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 962 | SDL->clear(); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | // Update PHI Nodes |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 966 | for (unsigned pi = 0, pe = SDL->PHINodesToUpdate.size(); pi != pe; ++pi) { |
| 967 | MachineInstr *PHI = SDL->PHINodesToUpdate[pi].first; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 968 | MachineBasicBlock *PHIBB = PHI->getParent(); |
| 969 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 970 | "This is not a machine PHI node that we are updating!"); |
| 971 | // This is "default" BB. We have two jumps to it. From "header" BB and |
| 972 | // from last "case" BB. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 973 | if (PHIBB == SDL->BitTestCases[i].Default) { |
| 974 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 975 | false)); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 976 | PHI->addOperand(MachineOperand::CreateMBB(SDL->BitTestCases[i].Parent)); |
| 977 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 978 | false)); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 979 | PHI->addOperand(MachineOperand::CreateMBB(SDL->BitTestCases[i].Cases. |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 980 | back().ThisBB)); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 981 | } |
| 982 | // One of "cases" BB. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 983 | for (unsigned j = 0, ej = SDL->BitTestCases[i].Cases.size(); |
| 984 | j != ej; ++j) { |
| 985 | MachineBasicBlock* cBB = SDL->BitTestCases[i].Cases[j].ThisBB; |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 986 | if (cBB->succ_end() != |
| 987 | std::find(cBB->succ_begin(),cBB->succ_end(), PHIBB)) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 988 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 989 | false)); |
| 990 | PHI->addOperand(MachineOperand::CreateMBB(cBB)); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
| 994 | } |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 995 | SDL->BitTestCases.clear(); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 996 | |
Nate Begeman | 9453eea | 2006-04-23 06:26:20 +0000 | [diff] [blame] | 997 | // If the JumpTable record is filled in, then we need to emit a jump table. |
| 998 | // Updating the PHI nodes is tricky in this case, since we need to determine |
| 999 | // whether the PHI is a successor of the range check MBB or the jump table MBB |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1000 | for (unsigned i = 0, e = SDL->JTCases.size(); i != e; ++i) { |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1001 | // Lower header first, if it wasn't already lowered |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1002 | if (!SDL->JTCases[i].first.Emitted) { |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1003 | // Set the current basic block to the mbb we wish to insert the code into |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1004 | BB = SDL->JTCases[i].first.HeaderBB; |
| 1005 | SDL->setCurrentBasicBlock(BB); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1006 | // Emit the code |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1007 | SDL->visitJumpTableHeader(SDL->JTCases[i].second, SDL->JTCases[i].first); |
| 1008 | CurDAG->setRoot(SDL->getRoot()); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1009 | CodeGenAndEmitDAG(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1010 | SDL->clear(); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1011 | } |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1012 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1013 | // Set the current basic block to the mbb we wish to insert the code into |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1014 | BB = SDL->JTCases[i].second.MBB; |
| 1015 | SDL->setCurrentBasicBlock(BB); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1016 | // Emit the code |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1017 | SDL->visitJumpTable(SDL->JTCases[i].second); |
| 1018 | CurDAG->setRoot(SDL->getRoot()); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1019 | CodeGenAndEmitDAG(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1020 | SDL->clear(); |
Anton Korobeynikov | 3a84b9b | 2007-03-25 15:07:15 +0000 | [diff] [blame] | 1021 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1022 | // Update PHI Nodes |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1023 | for (unsigned pi = 0, pe = SDL->PHINodesToUpdate.size(); pi != pe; ++pi) { |
| 1024 | MachineInstr *PHI = SDL->PHINodesToUpdate[pi].first; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1025 | MachineBasicBlock *PHIBB = PHI->getParent(); |
| 1026 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1027 | "This is not a machine PHI node that we are updating!"); |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1028 | // "default" BB. We can go there only from header BB. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1029 | if (PHIBB == SDL->JTCases[i].second.Default) { |
| 1030 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 1031 | false)); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1032 | PHI->addOperand(MachineOperand::CreateMBB(SDL->JTCases[i].first.HeaderBB)); |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1033 | } |
Anton Korobeynikov | 4198c58 | 2007-04-09 12:31:58 +0000 | [diff] [blame] | 1034 | // JT BB. Just iterate over successors here |
Nate Begeman | f4360a4 | 2006-05-03 03:48:02 +0000 | [diff] [blame] | 1035 | if (BB->succ_end() != std::find(BB->succ_begin(),BB->succ_end(), PHIBB)) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1036 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pi].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 1037 | false)); |
| 1038 | PHI->addOperand(MachineOperand::CreateMBB(BB)); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1041 | } |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1042 | SDL->JTCases.clear(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 1044 | // If the switch block involved a branch to one of the actual successors, we |
| 1045 | // need to update PHI nodes in that block. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1046 | for (unsigned i = 0, e = SDL->PHINodesToUpdate.size(); i != e; ++i) { |
| 1047 | MachineInstr *PHI = SDL->PHINodesToUpdate[i].first; |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 1048 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1049 | "This is not a machine PHI node that we are updating!"); |
| 1050 | if (BB->isSuccessor(PHI->getParent())) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1051 | PHI->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[i].second, |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 1052 | false)); |
| 1053 | PHI->addOperand(MachineOperand::CreateMBB(BB)); |
Chris Lattner | b2e806e | 2006-10-22 23:00:53 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1057 | // If we generated any switch lowering information, build and codegen any |
| 1058 | // additional DAGs necessary. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1059 | for (unsigned i = 0, e = SDL->SwitchCases.size(); i != e; ++i) { |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1060 | // Set the current basic block to the mbb we wish to insert the code into |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1061 | BB = SDL->SwitchCases[i].ThisBB; |
| 1062 | SDL->setCurrentBasicBlock(BB); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 1063 | |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1064 | // Emit the code |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1065 | SDL->visitSwitchCase(SDL->SwitchCases[i]); |
| 1066 | CurDAG->setRoot(SDL->getRoot()); |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1067 | CodeGenAndEmitDAG(); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1068 | SDL->clear(); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 1069 | |
| 1070 | // Handle any PHI nodes in successors of this chunk, as if we were coming |
| 1071 | // from the original BB before switch expansion. Note that PHI nodes can |
| 1072 | // occur multiple times in PHINodesToUpdate. We have to be very careful to |
| 1073 | // handle them the right number of times. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1074 | while ((BB = SDL->SwitchCases[i].TrueBB)) { // Handle LHS and RHS. |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 1075 | for (MachineBasicBlock::iterator Phi = BB->begin(); |
| 1076 | Phi != BB->end() && Phi->getOpcode() == TargetInstrInfo::PHI; ++Phi){ |
| 1077 | // This value for this PHI node is recorded in PHINodesToUpdate, get it. |
| 1078 | for (unsigned pn = 0; ; ++pn) { |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1079 | assert(pn != SDL->PHINodesToUpdate.size() && |
| 1080 | "Didn't find PHI entry!"); |
| 1081 | if (SDL->PHINodesToUpdate[pn].first == Phi) { |
| 1082 | Phi->addOperand(MachineOperand::CreateReg(SDL->PHINodesToUpdate[pn]. |
Chris Lattner | 9ce2e9d | 2007-12-30 00:57:42 +0000 | [diff] [blame] | 1083 | second, false)); |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1084 | Phi->addOperand(MachineOperand::CreateMBB(SDL->SwitchCases[i].ThisBB)); |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | } |
| 1087 | } |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1088 | } |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Don't process RHS if same block as LHS. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1091 | if (BB == SDL->SwitchCases[i].FalseBB) |
| 1092 | SDL->SwitchCases[i].FalseBB = 0; |
Chris Lattner | d5e93c0 | 2006-09-07 01:59:34 +0000 | [diff] [blame] | 1093 | |
| 1094 | // If we haven't handled the RHS, do so now. Otherwise, we're done. |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1095 | SDL->SwitchCases[i].TrueBB = SDL->SwitchCases[i].FalseBB; |
| 1096 | SDL->SwitchCases[i].FalseBB = 0; |
Nate Begeman | f15485a | 2006-03-27 01:32:24 +0000 | [diff] [blame] | 1097 | } |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1098 | assert(SDL->SwitchCases[i].TrueBB == 0 && SDL->SwitchCases[i].FalseBB == 0); |
Chris Lattner | a33ef48 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1099 | } |
Dan Gohman | 7c3234c | 2008-08-27 23:52:12 +0000 | [diff] [blame] | 1100 | SDL->SwitchCases.clear(); |
| 1101 | |
| 1102 | SDL->PHINodesToUpdate.clear(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1103 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1104 | |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1105 | |
Dan Gohman | 0a3776d | 2009-02-06 18:26:51 +0000 | [diff] [blame] | 1106 | /// Create the scheduler. If a specific scheduler was specified |
| 1107 | /// via the SchedulerRegistry, use it, otherwise select the |
| 1108 | /// one preferred by the target. |
Dan Gohman | 5e84368 | 2008-07-14 18:19:29 +0000 | [diff] [blame] | 1109 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 1110 | ScheduleDAGSDNodes *SelectionDAGISel::CreateScheduler() { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 1111 | RegisterScheduler::FunctionPassCtor Ctor = RegisterScheduler::getDefault(); |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (!Ctor) { |
Jim Laskey | eb577ba | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 1114 | Ctor = ISHeuristic; |
Jim Laskey | 9373beb | 2006-08-01 19:14:14 +0000 | [diff] [blame] | 1115 | RegisterScheduler::setDefault(Ctor); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 1116 | } |
Jim Laskey | 13ec702 | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 1117 | |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 1118 | return Ctor(this, OptLevel); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1119 | } |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1120 | |
Dan Gohman | fc54c55 | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 1121 | ScheduleHazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() { |
| 1122 | return new ScheduleHazardRecognizer(); |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1125 | //===----------------------------------------------------------------------===// |
| 1126 | // Helper functions used by the generated instruction selector. |
| 1127 | //===----------------------------------------------------------------------===// |
| 1128 | // Calls to these methods are generated by tblgen. |
| 1129 | |
| 1130 | /// CheckAndMask - The isel is trying to match something like (and X, 255). If |
| 1131 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 1132 | /// actual value in the DAG on the RHS of an AND, and DesiredMaskS is the value |
| 1133 | /// specified in the .td file (e.g. 255). |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1134 | bool SelectionDAGISel::CheckAndMask(SDValue LHS, ConstantSDNode *RHS, |
Dan Gohman | dc9b3d0 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 1135 | int64_t DesiredMaskS) const { |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1136 | const APInt &ActualMask = RHS->getAPIntValue(); |
| 1137 | const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1138 | |
| 1139 | // If the actual mask exactly matches, success! |
| 1140 | if (ActualMask == DesiredMask) |
| 1141 | return true; |
| 1142 | |
| 1143 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1144 | if (ActualMask.intersects(~DesiredMask)) |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1145 | return false; |
| 1146 | |
| 1147 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 1148 | // either already zero or is not demanded. Check for known zero input bits. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1149 | APInt NeededMask = DesiredMask & ~ActualMask; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1150 | if (CurDAG->MaskedValueIsZero(LHS, NeededMask)) |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1151 | return true; |
| 1152 | |
| 1153 | // TODO: check to see if missing bits are just not demanded. |
| 1154 | |
| 1155 | // Otherwise, this pattern doesn't match. |
| 1156 | return false; |
| 1157 | } |
| 1158 | |
| 1159 | /// CheckOrMask - The isel is trying to match something like (or X, 255). If |
| 1160 | /// the dag combiner simplified the 255, we still want to match. RHS is the |
| 1161 | /// actual value in the DAG on the RHS of an OR, and DesiredMaskS is the value |
| 1162 | /// specified in the .td file (e.g. 255). |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1163 | bool SelectionDAGISel::CheckOrMask(SDValue LHS, ConstantSDNode *RHS, |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1164 | int64_t DesiredMaskS) const { |
| 1165 | const APInt &ActualMask = RHS->getAPIntValue(); |
| 1166 | const APInt &DesiredMask = APInt(LHS.getValueSizeInBits(), DesiredMaskS); |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1167 | |
| 1168 | // If the actual mask exactly matches, success! |
| 1169 | if (ActualMask == DesiredMask) |
| 1170 | return true; |
| 1171 | |
| 1172 | // If the actual AND mask is allowing unallowed bits, this doesn't match. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1173 | if (ActualMask.intersects(~DesiredMask)) |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1174 | return false; |
| 1175 | |
| 1176 | // Otherwise, the DAG Combiner may have proven that the value coming in is |
| 1177 | // either already zero or is not demanded. Check for known zero input bits. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1178 | APInt NeededMask = DesiredMask & ~ActualMask; |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1179 | |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1180 | APInt KnownZero, KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1181 | CurDAG->ComputeMaskedBits(LHS, NeededMask, KnownZero, KnownOne); |
Chris Lattner | 7554806 | 2006-10-11 03:58:02 +0000 | [diff] [blame] | 1182 | |
| 1183 | // If all the missing bits in the or are already known to be set, match! |
| 1184 | if ((NeededMask & KnownOne) == NeededMask) |
| 1185 | return true; |
| 1186 | |
| 1187 | // TODO: check to see if missing bits are just not demanded. |
| 1188 | |
| 1189 | // Otherwise, this pattern doesn't match. |
| 1190 | return false; |
| 1191 | } |
| 1192 | |
Jim Laskey | 9ff542f | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 1193 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1194 | /// SelectInlineAsmMemoryOperands - Calls to this are automatically generated |
| 1195 | /// by tblgen. Others should not call it. |
| 1196 | void SelectionDAGISel:: |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1197 | SelectInlineAsmMemoryOperands(std::vector<SDValue> &Ops) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1198 | std::vector<SDValue> InOps; |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1199 | std::swap(InOps, Ops); |
| 1200 | |
| 1201 | Ops.push_back(InOps[0]); // input chain. |
| 1202 | Ops.push_back(InOps[1]); // input asm string. |
| 1203 | |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1204 | unsigned i = 2, e = InOps.size(); |
| 1205 | if (InOps[e-1].getValueType() == MVT::Flag) |
| 1206 | --e; // Don't process a flag operand if it is here. |
| 1207 | |
| 1208 | while (i != e) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1209 | unsigned Flags = cast<ConstantSDNode>(InOps[i])->getZExtValue(); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 1210 | if ((Flags & 7) != 4 /*MEM*/) { |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1211 | // Just skip over this operand, copying the operands verbatim. |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 1212 | Ops.insert(Ops.end(), InOps.begin()+i, |
| 1213 | InOps.begin()+i+InlineAsm::getNumOperandRegisters(Flags) + 1); |
| 1214 | i += InlineAsm::getNumOperandRegisters(Flags) + 1; |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1215 | } else { |
Evan Cheng | 697cbbf | 2009-03-20 18:03:34 +0000 | [diff] [blame] | 1216 | assert(InlineAsm::getNumOperandRegisters(Flags) == 1 && |
| 1217 | "Memory operand with multiple values?"); |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1218 | // Otherwise, this is a memory operand. Ask the target to select it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1219 | std::vector<SDValue> SelOps; |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1220 | if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps)) { |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1221 | llvm_report_error("Could not match memory address. Inline asm" |
| 1222 | " failure!"); |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | // Add this to the output node. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1226 | MVT IntPtrTy = CurDAG->getTargetLoweringInfo().getPointerTy(); |
Dale Johannesen | 86b49f8 | 2008-09-24 01:07:17 +0000 | [diff] [blame] | 1227 | Ops.push_back(CurDAG->getTargetConstant(4/*MEM*/ | (SelOps.size()<< 3), |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 1228 | IntPtrTy)); |
Chris Lattner | 0e43f2b | 2006-02-24 02:13:54 +0000 | [diff] [blame] | 1229 | Ops.insert(Ops.end(), SelOps.begin(), SelOps.end()); |
| 1230 | i += 2; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | // Add the flag input back if present. |
| 1235 | if (e != InOps.size()) |
| 1236 | Ops.push_back(InOps.back()); |
| 1237 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 1238 | |
Anton Korobeynikov | c1c6ef8 | 2009-05-08 18:51:58 +0000 | [diff] [blame] | 1239 | /// findFlagUse - Return use of MVT::Flag value produced by the specified |
| 1240 | /// SDNode. |
| 1241 | /// |
| 1242 | static SDNode *findFlagUse(SDNode *N) { |
| 1243 | unsigned FlagResNo = N->getNumValues()-1; |
| 1244 | for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { |
| 1245 | SDUse &Use = I.getUse(); |
| 1246 | if (Use.getResNo() == FlagResNo) |
| 1247 | return Use.getUser(); |
| 1248 | } |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | |
| 1252 | /// findNonImmUse - Return true if "Use" is a non-immediate use of "Def". |
| 1253 | /// This function recursively traverses up the operand chain, ignoring |
| 1254 | /// certain nodes. |
| 1255 | static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse, |
| 1256 | SDNode *Root, |
| 1257 | SmallPtrSet<SDNode*, 16> &Visited) { |
| 1258 | if (Use->getNodeId() < Def->getNodeId() || |
| 1259 | !Visited.insert(Use)) |
| 1260 | return false; |
| 1261 | |
| 1262 | for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) { |
| 1263 | SDNode *N = Use->getOperand(i).getNode(); |
| 1264 | if (N == Def) { |
| 1265 | if (Use == ImmedUse || Use == Root) |
| 1266 | continue; // We are not looking for immediate use. |
| 1267 | assert(N != Root); |
| 1268 | return true; |
| 1269 | } |
| 1270 | |
| 1271 | // Traverse up the operand chain. |
| 1272 | if (findNonImmUse(N, Def, ImmedUse, Root, Visited)) |
| 1273 | return true; |
| 1274 | } |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | /// isNonImmUse - Start searching from Root up the DAG to check is Def can |
| 1279 | /// be reached. Return true if that's the case. However, ignore direct uses |
| 1280 | /// by ImmedUse (which would be U in the example illustrated in |
| 1281 | /// IsLegalAndProfitableToFold) and by Root (which can happen in the store |
| 1282 | /// case). |
| 1283 | /// FIXME: to be really generic, we should allow direct use by any node |
| 1284 | /// that is being folded. But realisticly since we only fold loads which |
| 1285 | /// have one non-chain use, we only need to watch out for load/op/store |
| 1286 | /// and load/op/cmp case where the root (store / cmp) may reach the load via |
| 1287 | /// its chain operand. |
| 1288 | static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) { |
| 1289 | SmallPtrSet<SDNode*, 16> Visited; |
| 1290 | return findNonImmUse(Root, Def, ImmedUse, Root, Visited); |
| 1291 | } |
| 1292 | |
| 1293 | /// IsLegalAndProfitableToFold - Returns true if the specific operand node N of |
| 1294 | /// U can be folded during instruction selection that starts at Root and |
| 1295 | /// folding N is profitable. |
| 1296 | bool SelectionDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U, |
| 1297 | SDNode *Root) const { |
| 1298 | if (OptLevel == CodeGenOpt::None) return false; |
| 1299 | |
| 1300 | // If Root use can somehow reach N through a path that that doesn't contain |
| 1301 | // U then folding N would create a cycle. e.g. In the following |
| 1302 | // diagram, Root can reach N through X. If N is folded into into Root, then |
| 1303 | // X is both a predecessor and a successor of U. |
| 1304 | // |
| 1305 | // [N*] // |
| 1306 | // ^ ^ // |
| 1307 | // / \ // |
| 1308 | // [U*] [X]? // |
| 1309 | // ^ ^ // |
| 1310 | // \ / // |
| 1311 | // \ / // |
| 1312 | // [Root*] // |
| 1313 | // |
| 1314 | // * indicates nodes to be folded together. |
| 1315 | // |
| 1316 | // If Root produces a flag, then it gets (even more) interesting. Since it |
| 1317 | // will be "glued" together with its flag use in the scheduler, we need to |
| 1318 | // check if it might reach N. |
| 1319 | // |
| 1320 | // [N*] // |
| 1321 | // ^ ^ // |
| 1322 | // / \ // |
| 1323 | // [U*] [X]? // |
| 1324 | // ^ ^ // |
| 1325 | // \ \ // |
| 1326 | // \ | // |
| 1327 | // [Root*] | // |
| 1328 | // ^ | // |
| 1329 | // f | // |
| 1330 | // | / // |
| 1331 | // [Y] / // |
| 1332 | // ^ / // |
| 1333 | // f / // |
| 1334 | // | / // |
| 1335 | // [FU] // |
| 1336 | // |
| 1337 | // If FU (flag use) indirectly reaches N (the load), and Root folds N |
| 1338 | // (call it Fold), then X is a predecessor of FU and a successor of |
| 1339 | // Fold. But since Fold and FU are flagged together, this will create |
| 1340 | // a cycle in the scheduling graph. |
| 1341 | |
| 1342 | MVT VT = Root->getValueType(Root->getNumValues()-1); |
| 1343 | while (VT == MVT::Flag) { |
| 1344 | SDNode *FU = findFlagUse(Root); |
| 1345 | if (FU == NULL) |
| 1346 | break; |
| 1347 | Root = FU; |
| 1348 | VT = Root->getValueType(Root->getNumValues()-1); |
| 1349 | } |
| 1350 | |
| 1351 | return !isNonImmUse(Root, N, U); |
| 1352 | } |
| 1353 | |
| 1354 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1355 | char SelectionDAGISel::ID = 0; |