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