Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 1 | #include "llvm/ADT/STLExtras.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 2 | #include "llvm/CodeGen/LiveIntervals.h" |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 3 | #include "llvm/CodeGen/MIRParser/MIRParser.h" |
| 4 | #include "llvm/CodeGen/MachineFunction.h" |
Matthias Braun | 7938eee | 2016-05-10 03:03:55 +0000 | [diff] [blame] | 5 | #include "llvm/CodeGen/MachineModuleInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 6 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 7 | #include "llvm/IR/LegacyPassManager.h" |
Heejin Ahn | 3306fe1 | 2019-11-13 18:19:54 -0800 | [diff] [blame^] | 8 | #include "llvm/InitializePasses.h" |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 9 | #include "llvm/Support/MemoryBuffer.h" |
| 10 | #include "llvm/Support/SourceMgr.h" |
| 11 | #include "llvm/Support/TargetRegistry.h" |
| 12 | #include "llvm/Support/TargetSelect.h" |
| 13 | #include "llvm/Target/TargetMachine.h" |
| 14 | #include "llvm/Target/TargetOptions.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
NAKAMURA Takumi | c1857d1 | 2016-02-18 07:37:17 +0000 | [diff] [blame] | 16 | |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | namespace llvm { |
| 20 | void initializeTestPassPass(PassRegistry &); |
| 21 | } |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | void initLLVM() { |
| 26 | InitializeAllTargets(); |
| 27 | InitializeAllTargetMCs(); |
| 28 | InitializeAllAsmPrinters(); |
| 29 | InitializeAllAsmParsers(); |
| 30 | |
| 31 | PassRegistry *Registry = PassRegistry::getPassRegistry(); |
| 32 | initializeCore(*Registry); |
| 33 | initializeCodeGen(*Registry); |
| 34 | } |
| 35 | |
| 36 | /// Create a TargetMachine. As we lack a dedicated always available target for |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 37 | /// unittests, we go for "AMDGPU" to be able to test normal and subregister |
| 38 | /// liveranges. |
Matthias Braun | 3d849f6 | 2018-11-05 23:49:13 +0000 | [diff] [blame] | 39 | std::unique_ptr<LLVMTargetMachine> createTargetMachine() { |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 40 | Triple TargetTriple("amdgcn--"); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 41 | std::string Error; |
| 42 | const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error); |
| 43 | if (!T) |
| 44 | return nullptr; |
| 45 | |
| 46 | TargetOptions Options; |
Matthias Braun | 3d849f6 | 2018-11-05 23:49:13 +0000 | [diff] [blame] | 47 | return std::unique_ptr<LLVMTargetMachine>(static_cast<LLVMTargetMachine*>( |
| 48 | T->createTargetMachine("AMDGPU", "", "", Options, None, None, |
| 49 | CodeGenOpt::Aggressive))); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | std::unique_ptr<Module> parseMIR(LLVMContext &Context, |
| 53 | legacy::PassManagerBase &PM, std::unique_ptr<MIRParser> &MIR, |
Matthias Braun | 3d849f6 | 2018-11-05 23:49:13 +0000 | [diff] [blame] | 54 | const LLVMTargetMachine &TM, StringRef MIRCode, const char *FuncName) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 55 | SMDiagnostic Diagnostic; |
| 56 | std::unique_ptr<MemoryBuffer> MBuffer = MemoryBuffer::getMemBuffer(MIRCode); |
| 57 | MIR = createMIRParser(std::move(MBuffer), Context); |
| 58 | if (!MIR) |
| 59 | return nullptr; |
| 60 | |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 61 | std::unique_ptr<Module> M = MIR->parseIRModule(); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 62 | if (!M) |
| 63 | return nullptr; |
| 64 | |
| 65 | M->setDataLayout(TM.createDataLayout()); |
| 66 | |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 67 | MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(&TM); |
| 68 | if (MIR->parseMachineFunctions(*M, MMIWP->getMMI())) |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 69 | return nullptr; |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 70 | PM.add(MMIWP); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 71 | |
| 72 | return M; |
| 73 | } |
| 74 | |
| 75 | typedef std::function<void(MachineFunction&,LiveIntervals&)> LiveIntervalTest; |
| 76 | |
| 77 | struct TestPass : public MachineFunctionPass { |
| 78 | static char ID; |
| 79 | TestPass() : MachineFunctionPass(ID) { |
| 80 | // We should never call this but always use PM.add(new TestPass(...)) |
| 81 | abort(); |
| 82 | } |
| 83 | TestPass(LiveIntervalTest T) : MachineFunctionPass(ID), T(T) { |
| 84 | initializeTestPassPass(*PassRegistry::getPassRegistry()); |
| 85 | } |
| 86 | |
| 87 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 88 | LiveIntervals &LIS = getAnalysis<LiveIntervals>(); |
| 89 | T(MF, LIS); |
| 90 | EXPECT_TRUE(MF.verify(this)); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 95 | AU.setPreservesAll(); |
| 96 | AU.addRequired<LiveIntervals>(); |
| 97 | AU.addPreserved<LiveIntervals>(); |
| 98 | MachineFunctionPass::getAnalysisUsage(AU); |
| 99 | } |
| 100 | private: |
| 101 | LiveIntervalTest T; |
| 102 | }; |
| 103 | |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 104 | static MachineInstr &getMI(MachineFunction &MF, unsigned At, |
| 105 | unsigned BlockNum) { |
| 106 | MachineBasicBlock &MBB = *MF.getBlockNumbered(BlockNum); |
| 107 | |
| 108 | unsigned I = 0; |
| 109 | for (MachineInstr &MI : MBB) { |
| 110 | if (I == At) |
| 111 | return MI; |
| 112 | ++I; |
| 113 | } |
| 114 | llvm_unreachable("Instruction not found"); |
| 115 | } |
| 116 | |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Move instruction number \p From in front of instruction number \p To and |
| 119 | * update affected liveness intervals with LiveIntervalAnalysis::handleMove(). |
| 120 | */ |
| 121 | static void testHandleMove(MachineFunction &MF, LiveIntervals &LIS, |
Matthias Braun | fc4c8a1 | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 122 | unsigned From, unsigned To, unsigned BlockNum = 0) { |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 123 | MachineInstr &FromInstr = getMI(MF, From, BlockNum); |
| 124 | MachineInstr &ToInstr = getMI(MF, To, BlockNum); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 125 | |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 126 | MachineBasicBlock &MBB = *FromInstr.getParent(); |
| 127 | MBB.splice(ToInstr.getIterator(), &MBB, FromInstr.getIterator()); |
| 128 | LIS.handleMove(FromInstr, true); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void liveIntervalTest(StringRef MIRFunc, LiveIntervalTest T) { |
| 132 | LLVMContext Context; |
Matthias Braun | 3d849f6 | 2018-11-05 23:49:13 +0000 | [diff] [blame] | 133 | std::unique_ptr<LLVMTargetMachine> TM = createTargetMachine(); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 134 | // This test is designed for the X86 backend; stop if it is not available. |
| 135 | if (!TM) |
| 136 | return; |
| 137 | |
| 138 | legacy::PassManager PM; |
| 139 | |
| 140 | SmallString<160> S; |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 141 | StringRef MIRString = (Twine(R"MIR( |
| 142 | --- |
| 143 | ... |
| 144 | name: func |
| 145 | registers: |
| 146 | - { id: 0, class: sreg_64 } |
| 147 | body: | |
| 148 | bb.0: |
| 149 | )MIR") + Twine(MIRFunc) + Twine("...\n")).toNullTerminatedStringRef(S); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 150 | std::unique_ptr<MIRParser> MIR; |
| 151 | std::unique_ptr<Module> M = parseMIR(Context, PM, MIR, *TM, MIRString, |
| 152 | "func"); |
Matthias Braun | 531f3a9 | 2017-06-15 22:50:57 +0000 | [diff] [blame] | 153 | ASSERT_TRUE(M); |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 154 | |
| 155 | PM.add(new TestPass(T)); |
| 156 | |
| 157 | PM.run(*M); |
| 158 | } |
| 159 | |
| 160 | } // End of anonymous namespace. |
| 161 | |
| 162 | char TestPass::ID = 0; |
| 163 | INITIALIZE_PASS(TestPass, "testpass", "testpass", false, false) |
| 164 | |
| 165 | TEST(LiveIntervalTest, MoveUpDef) { |
| 166 | // Value defined. |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 167 | liveIntervalTest(R"MIR( |
| 168 | S_NOP 0 |
| 169 | S_NOP 0 |
| 170 | early-clobber %0 = IMPLICIT_DEF |
| 171 | S_NOP 0, implicit %0 |
| 172 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 173 | testHandleMove(MF, LIS, 2, 1); |
| 174 | }); |
| 175 | } |
| 176 | |
| 177 | TEST(LiveIntervalTest, MoveUpRedef) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 178 | liveIntervalTest(R"MIR( |
| 179 | %0 = IMPLICIT_DEF |
| 180 | S_NOP 0 |
| 181 | %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 182 | S_NOP 0, implicit %0 |
| 183 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 184 | testHandleMove(MF, LIS, 2, 1); |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | TEST(LiveIntervalTest, MoveUpEarlyDef) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 189 | liveIntervalTest(R"MIR( |
| 190 | S_NOP 0 |
| 191 | S_NOP 0 |
| 192 | early-clobber %0 = IMPLICIT_DEF |
| 193 | S_NOP 0, implicit %0 |
| 194 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 195 | testHandleMove(MF, LIS, 2, 1); |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | TEST(LiveIntervalTest, MoveUpEarlyRedef) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 200 | liveIntervalTest(R"MIR( |
| 201 | %0 = IMPLICIT_DEF |
| 202 | S_NOP 0 |
| 203 | early-clobber %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 204 | S_NOP 0, implicit %0 |
| 205 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 206 | testHandleMove(MF, LIS, 2, 1); |
| 207 | }); |
| 208 | } |
| 209 | |
| 210 | TEST(LiveIntervalTest, MoveUpKill) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 211 | liveIntervalTest(R"MIR( |
| 212 | %0 = IMPLICIT_DEF |
| 213 | S_NOP 0 |
| 214 | S_NOP 0, implicit %0 |
| 215 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 216 | testHandleMove(MF, LIS, 2, 1); |
| 217 | }); |
| 218 | } |
| 219 | |
| 220 | TEST(LiveIntervalTest, MoveUpKillFollowing) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 221 | liveIntervalTest(R"MIR( |
| 222 | %0 = IMPLICIT_DEF |
| 223 | S_NOP 0 |
| 224 | S_NOP 0, implicit %0 |
| 225 | S_NOP 0, implicit %0 |
| 226 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 227 | testHandleMove(MF, LIS, 2, 1); |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | // TODO: Construct a situation where we have intervals following a hole |
| 232 | // while still having connected components. |
| 233 | |
| 234 | TEST(LiveIntervalTest, MoveDownDef) { |
| 235 | // Value defined. |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 236 | liveIntervalTest(R"MIR( |
| 237 | S_NOP 0 |
| 238 | early-clobber %0 = IMPLICIT_DEF |
| 239 | S_NOP 0 |
| 240 | S_NOP 0, implicit %0 |
| 241 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 242 | testHandleMove(MF, LIS, 1, 2); |
| 243 | }); |
| 244 | } |
| 245 | |
| 246 | TEST(LiveIntervalTest, MoveDownRedef) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 247 | liveIntervalTest(R"MIR( |
| 248 | %0 = IMPLICIT_DEF |
| 249 | %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 250 | S_NOP 0 |
| 251 | S_NOP 0, implicit %0 |
| 252 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 253 | testHandleMove(MF, LIS, 1, 2); |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | TEST(LiveIntervalTest, MoveDownEarlyDef) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 258 | liveIntervalTest(R"MIR( |
| 259 | S_NOP 0 |
| 260 | early-clobber %0 = IMPLICIT_DEF |
| 261 | S_NOP 0 |
| 262 | S_NOP 0, implicit %0 |
| 263 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 264 | testHandleMove(MF, LIS, 1, 2); |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | TEST(LiveIntervalTest, MoveDownEarlyRedef) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 269 | liveIntervalTest(R"MIR( |
| 270 | %0 = IMPLICIT_DEF |
| 271 | early-clobber %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 272 | S_NOP 0 |
| 273 | S_NOP 0, implicit %0 |
| 274 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 275 | testHandleMove(MF, LIS, 1, 2); |
| 276 | }); |
| 277 | } |
| 278 | |
| 279 | TEST(LiveIntervalTest, MoveDownKill) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 280 | liveIntervalTest(R"MIR( |
| 281 | %0 = IMPLICIT_DEF |
| 282 | S_NOP 0, implicit %0 |
| 283 | S_NOP 0 |
| 284 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 285 | testHandleMove(MF, LIS, 1, 2); |
| 286 | }); |
| 287 | } |
| 288 | |
| 289 | TEST(LiveIntervalTest, MoveDownKillFollowing) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 290 | liveIntervalTest(R"MIR( |
| 291 | %0 = IMPLICIT_DEF |
| 292 | S_NOP 0 |
| 293 | S_NOP 0, implicit %0 |
| 294 | S_NOP 0, implicit %0 |
| 295 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 296 | testHandleMove(MF, LIS, 1, 2); |
| 297 | }); |
| 298 | } |
| 299 | |
Matthias Braun | 71474e8 | 2016-05-06 21:47:41 +0000 | [diff] [blame] | 300 | TEST(LiveIntervalTest, MoveUndefUse) { |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 301 | liveIntervalTest(R"MIR( |
| 302 | %0 = IMPLICIT_DEF |
| 303 | S_NOP 0, implicit undef %0 |
| 304 | S_NOP 0, implicit %0 |
| 305 | S_NOP 0 |
| 306 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 71474e8 | 2016-05-06 21:47:41 +0000 | [diff] [blame] | 307 | testHandleMove(MF, LIS, 1, 3); |
| 308 | }); |
| 309 | } |
| 310 | |
Matthias Braun | fc4c8a1 | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 311 | TEST(LiveIntervalTest, MoveUpValNos) { |
| 312 | // handleMoveUp() had a bug where it would reuse the value number of the |
Matt Arsenault | df3af00 | 2019-09-26 15:20:16 +0000 | [diff] [blame] | 313 | // destination segment, even though we have no guarantee that this valno |
| 314 | // wasn't used in other segments. |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 315 | liveIntervalTest(R"MIR( |
| 316 | successors: %bb.1, %bb.2 |
| 317 | %0 = IMPLICIT_DEF |
Puyan Lotfi | 43e94b1 | 2018-01-31 22:04:26 +0000 | [diff] [blame] | 318 | S_CBRANCH_VCCNZ %bb.2, implicit undef $vcc |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 319 | S_BRANCH %bb.1 |
| 320 | bb.2: |
| 321 | S_NOP 0, implicit %0 |
| 322 | bb.1: |
| 323 | successors: %bb.2 |
| 324 | %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 325 | %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 326 | %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 327 | S_BRANCH %bb.2 |
| 328 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | fc4c8a1 | 2016-05-24 21:54:01 +0000 | [diff] [blame] | 329 | testHandleMove(MF, LIS, 2, 0, 2); |
| 330 | }); |
| 331 | } |
| 332 | |
Matthias Braun | 959a8c9 | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 333 | TEST(LiveIntervalTest, MoveOverUndefUse0) { |
| 334 | // findLastUseBefore() used by handleMoveUp() must ignore undef operands. |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 335 | liveIntervalTest(R"MIR( |
| 336 | %0 = IMPLICIT_DEF |
| 337 | S_NOP 0 |
| 338 | S_NOP 0, implicit undef %0 |
| 339 | %0 = IMPLICIT_DEF implicit %0(tied-def 0) |
| 340 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 959a8c9 | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 341 | testHandleMove(MF, LIS, 3, 1); |
| 342 | }); |
| 343 | } |
| 344 | |
| 345 | TEST(LiveIntervalTest, MoveOverUndefUse1) { |
| 346 | // findLastUseBefore() used by handleMoveUp() must ignore undef operands. |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 347 | liveIntervalTest(R"MIR( |
Puyan Lotfi | 43e94b1 | 2018-01-31 22:04:26 +0000 | [diff] [blame] | 348 | $sgpr0 = IMPLICIT_DEF |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 349 | S_NOP 0 |
Puyan Lotfi | 43e94b1 | 2018-01-31 22:04:26 +0000 | [diff] [blame] | 350 | S_NOP 0, implicit undef $sgpr0 |
| 351 | $sgpr0 = IMPLICIT_DEF implicit $sgpr0(tied-def 0) |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 352 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 959a8c9 | 2016-06-11 00:31:28 +0000 | [diff] [blame] | 353 | testHandleMove(MF, LIS, 3, 1); |
| 354 | }); |
| 355 | } |
| 356 | |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 357 | TEST(LiveIntervalTest, SubRegMoveDown) { |
| 358 | // Subregister ranges can have holes inside a basic block. Check for a |
| 359 | // movement of the form 32->150 in a liverange [16, 32) [100,200). |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 360 | liveIntervalTest(R"MIR( |
| 361 | successors: %bb.1, %bb.2 |
| 362 | %0 = IMPLICIT_DEF |
Puyan Lotfi | 43e94b1 | 2018-01-31 22:04:26 +0000 | [diff] [blame] | 363 | S_CBRANCH_VCCNZ %bb.2, implicit undef $vcc |
Matthias Braun | 1cba140 | 2017-02-23 01:09:01 +0000 | [diff] [blame] | 364 | S_BRANCH %bb.1 |
| 365 | bb.2: |
| 366 | successors: %bb.1 |
| 367 | S_NOP 0, implicit %0.sub0 |
| 368 | S_NOP 0, implicit %0.sub1 |
| 369 | S_NOP 0 |
| 370 | undef %0.sub0 = IMPLICIT_DEF |
| 371 | %0.sub1 = IMPLICIT_DEF |
| 372 | bb.1: |
| 373 | S_NOP 0, implicit %0 |
| 374 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
Matthias Braun | 3865b1d | 2016-07-26 03:57:45 +0000 | [diff] [blame] | 375 | // Scheduler behaviour: Clear def,read-undef flag and move. |
| 376 | MachineInstr &MI = getMI(MF, 3, /*BlockNum=*/1); |
| 377 | MI.getOperand(0).setIsUndef(false); |
| 378 | testHandleMove(MF, LIS, 1, 4, /*BlockNum=*/1); |
| 379 | }); |
| 380 | } |
| 381 | |
Stanislav Mekhanoshin | b546174b | 2017-03-11 00:14:52 +0000 | [diff] [blame] | 382 | TEST(LiveIntervalTest, SubRegMoveUp) { |
| 383 | // handleMoveUp had a bug not updating valno of segment incoming to bb.2 |
| 384 | // after swapping subreg definitions. |
| 385 | liveIntervalTest(R"MIR( |
| 386 | successors: %bb.1, %bb.2 |
| 387 | undef %0.sub0 = IMPLICIT_DEF |
| 388 | %0.sub1 = IMPLICIT_DEF |
Puyan Lotfi | 43e94b1 | 2018-01-31 22:04:26 +0000 | [diff] [blame] | 389 | S_CBRANCH_VCCNZ %bb.2, implicit undef $vcc |
Stanislav Mekhanoshin | b546174b | 2017-03-11 00:14:52 +0000 | [diff] [blame] | 390 | S_BRANCH %bb.1 |
| 391 | bb.1: |
| 392 | S_NOP 0, implicit %0.sub1 |
| 393 | bb.2: |
| 394 | S_NOP 0, implicit %0.sub1 |
| 395 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
| 396 | testHandleMove(MF, LIS, 1, 0); |
| 397 | }); |
| 398 | } |
| 399 | |
Tim Renouf | f40707a | 2018-02-26 14:42:13 +0000 | [diff] [blame] | 400 | TEST(LiveIntervalTest, DeadSubRegMoveUp) { |
| 401 | // handleMoveUp had a bug where moving a dead subreg def into the middle of |
| 402 | // an earlier segment resulted in an invalid live range. |
| 403 | liveIntervalTest(R"MIR( |
| 404 | undef %125.sub0:vreg_128 = V_MOV_B32_e32 0, implicit $exec |
| 405 | %125.sub1:vreg_128 = COPY %125.sub0 |
| 406 | %125.sub2:vreg_128 = COPY %125.sub0 |
| 407 | undef %51.sub0:vreg_128 = V_MOV_B32_e32 898625526, implicit $exec |
| 408 | %51.sub1:vreg_128 = COPY %51.sub0 |
| 409 | %51.sub2:vreg_128 = COPY %51.sub0 |
| 410 | %52:vgpr_32 = V_MOV_B32_e32 986714345, implicit $exec |
| 411 | %54:vgpr_32 = V_MOV_B32_e32 1742342378, implicit $exec |
| 412 | %57:vgpr_32 = V_MOV_B32_e32 3168768712, implicit $exec |
| 413 | %59:vgpr_32 = V_MOV_B32_e32 1039972644, implicit $exec |
| 414 | %60:vgpr_32 = V_MAD_F32 0, %52, 0, undef %61:vgpr_32, 0, %59, 0, 0, implicit $exec |
| 415 | %63:vgpr_32 = V_ADD_F32_e32 %51.sub3, undef %64:vgpr_32, implicit $exec |
| 416 | dead %66:vgpr_32 = V_MAD_F32 0, %60, 0, undef %67:vgpr_32, 0, %125.sub2, 0, 0, implicit $exec |
| 417 | undef %124.sub1:vreg_128 = V_MAD_F32 0, %57, 0, undef %70:vgpr_32, 0, %125.sub1, 0, 0, implicit $exec |
| 418 | %124.sub0:vreg_128 = V_MAD_F32 0, %54, 0, undef %73:vgpr_32, 0, %125.sub0, 0, 0, implicit $exec |
| 419 | dead undef %125.sub3:vreg_128 = V_MAC_F32_e32 %63, undef %76:vgpr_32, %125.sub3, implicit $exec |
| 420 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
| 421 | testHandleMove(MF, LIS, 15, 12); |
| 422 | }); |
| 423 | } |
| 424 | |
Matt Arsenault | d4274f0 | 2019-10-18 23:24:25 +0000 | [diff] [blame] | 425 | TEST(LiveIntervalTest, TestMoveSubRegDefAcrossUseDef) { |
| 426 | liveIntervalTest(R"MIR( |
| 427 | %1:vreg_64 = IMPLICIT_DEF |
| 428 | |
| 429 | bb.1: |
| 430 | %2:vgpr_32 = V_MOV_B32_e32 2, implicit $exec |
| 431 | %3:vgpr_32 = V_ADD_U32_e32 %2, %1.sub0, implicit $exec |
| 432 | undef %1.sub0:vreg_64 = V_ADD_U32_e32 %2, %2, implicit $exec |
| 433 | %1.sub1:vreg_64 = COPY %2 |
| 434 | S_NOP 0, implicit %1.sub1 |
| 435 | S_BRANCH %bb.1 |
| 436 | |
| 437 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
| 438 | MachineInstr &UndefSubregDef = getMI(MF, 2, 1); |
| 439 | // The scheduler clears undef from subregister defs before moving |
| 440 | UndefSubregDef.getOperand(0).setIsUndef(false); |
| 441 | testHandleMove(MF, LIS, 3, 1, 1); |
| 442 | }); |
| 443 | } |
| 444 | |
| 445 | TEST(LiveIntervalTest, TestMoveSubRegDefAcrossUseDefMulti) { |
| 446 | liveIntervalTest(R"MIR( |
| 447 | %1:vreg_96 = IMPLICIT_DEF |
| 448 | |
| 449 | bb.1: |
| 450 | %2:vgpr_32 = V_MOV_B32_e32 2, implicit $exec |
| 451 | %3:vgpr_32 = V_ADD_U32_e32 %2, %1.sub0, implicit $exec |
| 452 | undef %1.sub0:vreg_96 = V_ADD_U32_e32 %2, %2, implicit $exec |
| 453 | %1.sub1:vreg_96 = COPY %2 |
| 454 | %1.sub2:vreg_96 = COPY %2 |
| 455 | S_NOP 0, implicit %1.sub1, implicit %1.sub2 |
| 456 | S_BRANCH %bb.1 |
| 457 | |
| 458 | )MIR", [](MachineFunction &MF, LiveIntervals &LIS) { |
| 459 | MachineInstr &UndefSubregDef = getMI(MF, 2, 1); |
| 460 | // The scheduler clears undef from subregister defs before moving |
| 461 | UndefSubregDef.getOperand(0).setIsUndef(false); |
| 462 | testHandleMove(MF, LIS, 4, 1, 1); |
| 463 | }); |
| 464 | } |
Matthias Braun | 3199c4e | 2016-05-02 23:05:48 +0000 | [diff] [blame] | 465 | int main(int argc, char **argv) { |
| 466 | ::testing::InitGoogleTest(&argc, argv); |
| 467 | initLLVM(); |
| 468 | return RUN_ALL_TESTS(); |
NAKAMURA Takumi | c1857d1 | 2016-02-18 07:37:17 +0000 | [diff] [blame] | 469 | } |