blob: 787f6a2791873e63fd157056ce90022c6c899282 [file] [log] [blame]
Evan Cheng48575f62010-12-05 22:04:16 +00001//===-- ARMHazardRecognizer.cpp - ARM postra hazard recognizer ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARMHazardRecognizer.h"
11#include "ARMBaseInstrInfo.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000012#include "ARMBaseRegisterInfo.h"
Evan Cheng48575f62010-12-05 22:04:16 +000013#include "ARMSubtarget.h"
14#include "llvm/CodeGen/MachineInstr.h"
15#include "llvm/CodeGen/ScheduleDAG.h"
16#include "llvm/Target/TargetRegisterInfo.h"
17using namespace llvm;
18
19static bool hasRAWHazard(MachineInstr *DefMI, MachineInstr *MI,
20 const TargetRegisterInfo &TRI) {
21 // FIXME: Detect integer instructions properly.
Evan Chenge837dea2011-06-28 19:10:37 +000022 const MCInstrDesc &MCID = MI->getDesc();
23 unsigned Domain = MCID.TSFlags & ARMII::DomainMask;
24 if (MCID.mayStore())
Evan Cheng48575f62010-12-05 22:04:16 +000025 return false;
Evan Chenge837dea2011-06-28 19:10:37 +000026 unsigned Opcode = MCID.getOpcode();
Evan Cheng6557bce2011-02-22 19:53:14 +000027 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
28 return false;
29 if ((Domain & ARMII::DomainVFP) || (Domain & ARMII::DomainNEON))
30 return MI->readsRegister(DefMI->getOperand(0).getReg(), &TRI);
31 return false;
Evan Cheng48575f62010-12-05 22:04:16 +000032}
33
34ScheduleHazardRecognizer::HazardType
Andrew Trick2da8bc82010-12-24 05:03:26 +000035ARMHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
36 assert(Stalls == 0 && "ARM hazards don't support scoreboard lookahead");
37
Evan Cheng48575f62010-12-05 22:04:16 +000038 MachineInstr *MI = SU->getInstr();
39
40 if (!MI->isDebugValue()) {
41 if (ITBlockSize && MI != ITBlockMIs[ITBlockSize-1])
42 return Hazard;
43
44 // Look for special VMLA / VMLS hazards. A VMUL / VADD / VSUB following
45 // a VMLA / VMLS will cause 4 cycle stall.
Evan Chenge837dea2011-06-28 19:10:37 +000046 const MCInstrDesc &MCID = MI->getDesc();
47 if (LastMI && (MCID.TSFlags & ARMII::DomainMask) != ARMII::DomainGeneral) {
Evan Cheng48575f62010-12-05 22:04:16 +000048 MachineInstr *DefMI = LastMI;
Evan Chenge837dea2011-06-28 19:10:37 +000049 const MCInstrDesc &LastMCID = LastMI->getDesc();
Evan Cheng48575f62010-12-05 22:04:16 +000050 // Skip over one non-VFP / NEON instruction.
Evan Chenge837dea2011-06-28 19:10:37 +000051 if (!LastMCID.isBarrier() &&
Bob Wilson84c5eed2011-04-19 18:11:57 +000052 // On A9, AGU and NEON/FPU are muxed.
Evan Chenge837dea2011-06-28 19:10:37 +000053 !(STI.isCortexA9() && (LastMCID.mayLoad() || LastMCID.mayStore())) &&
54 (LastMCID.TSFlags & ARMII::DomainMask) == ARMII::DomainGeneral) {
Evan Cheng48575f62010-12-05 22:04:16 +000055 MachineBasicBlock::iterator I = LastMI;
56 if (I != LastMI->getParent()->begin()) {
57 I = llvm::prior(I);
58 DefMI = &*I;
59 }
60 }
61
62 if (TII.isFpMLxInstruction(DefMI->getOpcode()) &&
63 (TII.canCauseFpMLxStall(MI->getOpcode()) ||
64 hasRAWHazard(DefMI, MI, TRI))) {
65 // Try to schedule another instruction for the next 4 cycles.
Andrew Trick2da8bc82010-12-24 05:03:26 +000066 if (FpMLxStalls == 0)
67 FpMLxStalls = 4;
Evan Cheng48575f62010-12-05 22:04:16 +000068 return Hazard;
69 }
70 }
71 }
72
Andrew Trick2da8bc82010-12-24 05:03:26 +000073 return ScoreboardHazardRecognizer::getHazardType(SU, Stalls);
Evan Cheng48575f62010-12-05 22:04:16 +000074}
75
76void ARMHazardRecognizer::Reset() {
77 LastMI = 0;
Andrew Trick2da8bc82010-12-24 05:03:26 +000078 FpMLxStalls = 0;
Evan Cheng48575f62010-12-05 22:04:16 +000079 ITBlockSize = 0;
Andrew Trick6b120722010-12-08 20:04:29 +000080 ScoreboardHazardRecognizer::Reset();
Evan Cheng48575f62010-12-05 22:04:16 +000081}
82
83void ARMHazardRecognizer::EmitInstruction(SUnit *SU) {
84 MachineInstr *MI = SU->getInstr();
85 unsigned Opcode = MI->getOpcode();
86 if (ITBlockSize) {
87 --ITBlockSize;
88 } else if (Opcode == ARM::t2IT) {
89 unsigned Mask = MI->getOperand(1).getImm();
90 unsigned NumTZ = CountTrailingZeros_32(Mask);
91 assert(NumTZ <= 3 && "Invalid IT mask!");
92 ITBlockSize = 4 - NumTZ;
93 MachineBasicBlock::iterator I = MI;
94 for (unsigned i = 0; i < ITBlockSize; ++i) {
95 // Advance to the next instruction, skipping any dbg_value instructions.
96 do {
97 ++I;
98 } while (I->isDebugValue());
99 ITBlockMIs[ITBlockSize-1-i] = &*I;
100 }
101 }
102
103 if (!MI->isDebugValue()) {
104 LastMI = MI;
Andrew Trick2da8bc82010-12-24 05:03:26 +0000105 FpMLxStalls = 0;
Evan Cheng48575f62010-12-05 22:04:16 +0000106 }
107
Andrew Trick6b120722010-12-08 20:04:29 +0000108 ScoreboardHazardRecognizer::EmitInstruction(SU);
Evan Cheng48575f62010-12-05 22:04:16 +0000109}
110
111void ARMHazardRecognizer::AdvanceCycle() {
Andrew Trick2da8bc82010-12-24 05:03:26 +0000112 if (FpMLxStalls && --FpMLxStalls == 0)
Evan Cheng48575f62010-12-05 22:04:16 +0000113 // Stalled for 4 cycles but still can't schedule any other instructions.
114 LastMI = 0;
Andrew Trick6b120722010-12-08 20:04:29 +0000115 ScoreboardHazardRecognizer::AdvanceCycle();
116}
117
118void ARMHazardRecognizer::RecedeCycle() {
119 llvm_unreachable("reverse ARM hazard checking unsupported");
Evan Cheng48575f62010-12-05 22:04:16 +0000120}