blob: e97ce50bc4294952e468085f36eca9a9e3bd002f [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.
22 const TargetInstrDesc &TID = MI->getDesc();
23 unsigned Domain = TID.TSFlags & ARMII::DomainMask;
Evan Cheng6557bce2011-02-22 19:53:14 +000024 if (TID.mayStore())
Evan Cheng48575f62010-12-05 22:04:16 +000025 return false;
Evan Cheng6557bce2011-02-22 19:53:14 +000026 unsigned Opcode = TID.getOpcode();
27 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.
46 const TargetInstrDesc &TID = MI->getDesc();
47 if (LastMI && (TID.TSFlags & ARMII::DomainMask) != ARMII::DomainGeneral) {
48 MachineInstr *DefMI = LastMI;
49 const TargetInstrDesc &LastTID = LastMI->getDesc();
50 // Skip over one non-VFP / NEON instruction.
51 if (!LastTID.isBarrier() &&
52 (LastTID.TSFlags & ARMII::DomainMask) == ARMII::DomainGeneral) {
53 MachineBasicBlock::iterator I = LastMI;
54 if (I != LastMI->getParent()->begin()) {
55 I = llvm::prior(I);
56 DefMI = &*I;
57 }
58 }
59
60 if (TII.isFpMLxInstruction(DefMI->getOpcode()) &&
61 (TII.canCauseFpMLxStall(MI->getOpcode()) ||
62 hasRAWHazard(DefMI, MI, TRI))) {
63 // Try to schedule another instruction for the next 4 cycles.
Andrew Trick2da8bc82010-12-24 05:03:26 +000064 if (FpMLxStalls == 0)
65 FpMLxStalls = 4;
Evan Cheng48575f62010-12-05 22:04:16 +000066 return Hazard;
67 }
68 }
69 }
70
Andrew Trick2da8bc82010-12-24 05:03:26 +000071 return ScoreboardHazardRecognizer::getHazardType(SU, Stalls);
Evan Cheng48575f62010-12-05 22:04:16 +000072}
73
74void ARMHazardRecognizer::Reset() {
75 LastMI = 0;
Andrew Trick2da8bc82010-12-24 05:03:26 +000076 FpMLxStalls = 0;
Evan Cheng48575f62010-12-05 22:04:16 +000077 ITBlockSize = 0;
Andrew Trick6b120722010-12-08 20:04:29 +000078 ScoreboardHazardRecognizer::Reset();
Evan Cheng48575f62010-12-05 22:04:16 +000079}
80
81void ARMHazardRecognizer::EmitInstruction(SUnit *SU) {
82 MachineInstr *MI = SU->getInstr();
83 unsigned Opcode = MI->getOpcode();
84 if (ITBlockSize) {
85 --ITBlockSize;
86 } else if (Opcode == ARM::t2IT) {
87 unsigned Mask = MI->getOperand(1).getImm();
88 unsigned NumTZ = CountTrailingZeros_32(Mask);
89 assert(NumTZ <= 3 && "Invalid IT mask!");
90 ITBlockSize = 4 - NumTZ;
91 MachineBasicBlock::iterator I = MI;
92 for (unsigned i = 0; i < ITBlockSize; ++i) {
93 // Advance to the next instruction, skipping any dbg_value instructions.
94 do {
95 ++I;
96 } while (I->isDebugValue());
97 ITBlockMIs[ITBlockSize-1-i] = &*I;
98 }
99 }
100
101 if (!MI->isDebugValue()) {
102 LastMI = MI;
Andrew Trick2da8bc82010-12-24 05:03:26 +0000103 FpMLxStalls = 0;
Evan Cheng48575f62010-12-05 22:04:16 +0000104 }
105
Andrew Trick6b120722010-12-08 20:04:29 +0000106 ScoreboardHazardRecognizer::EmitInstruction(SU);
Evan Cheng48575f62010-12-05 22:04:16 +0000107}
108
109void ARMHazardRecognizer::AdvanceCycle() {
Andrew Trick2da8bc82010-12-24 05:03:26 +0000110 if (FpMLxStalls && --FpMLxStalls == 0)
Evan Cheng48575f62010-12-05 22:04:16 +0000111 // Stalled for 4 cycles but still can't schedule any other instructions.
112 LastMI = 0;
Andrew Trick6b120722010-12-08 20:04:29 +0000113 ScoreboardHazardRecognizer::AdvanceCycle();
114}
115
116void ARMHazardRecognizer::RecedeCycle() {
117 llvm_unreachable("reverse ARM hazard checking unsupported");
Evan Cheng48575f62010-12-05 22:04:16 +0000118}