676b01e91c53a1d88bc800323f5a7bbc897e951f
[oota-llvm.git] / lib / Target / ARM / ARMHazardRecognizer.cpp
1 //===-- 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"
12 #include "ARMBaseRegisterInfo.h"
13 #include "ARMSubtarget.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/ScheduleDAG.h"
16 #include "llvm/Target/TargetRegisterInfo.h"
17 using namespace llvm;
18
19 static 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;
24   if (Domain == ARMII::DomainVFP) {
25     unsigned Opcode = MI->getOpcode();
26     if (Opcode == ARM::VSTRS || Opcode == ARM::VSTRD ||
27         Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
28       return false;
29   } else if (Domain == ARMII::DomainNEON) {
30     if (MI->getDesc().mayStore() || MI->getDesc().mayLoad())
31       return false;
32   } else
33     return false;
34   return MI->readsRegister(DefMI->getOperand(0).getReg(), &TRI);
35 }
36
37 ScheduleHazardRecognizer::HazardType
38 ARMHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
39   assert(Stalls == 0 && "ARM hazards don't support scoreboard lookahead");
40
41   MachineInstr *MI = SU->getInstr();
42
43   if (!MI->isDebugValue()) {
44     if (ITBlockSize && MI != ITBlockMIs[ITBlockSize-1])
45       return Hazard;
46
47     // Look for special VMLA / VMLS hazards. A VMUL / VADD / VSUB following
48     // a VMLA / VMLS will cause 4 cycle stall.
49     const TargetInstrDesc &TID = MI->getDesc();
50     if (LastMI && (TID.TSFlags & ARMII::DomainMask) != ARMII::DomainGeneral) {
51       MachineInstr *DefMI = LastMI;
52       const TargetInstrDesc &LastTID = LastMI->getDesc();
53       // Skip over one non-VFP / NEON instruction.
54       if (!LastTID.isBarrier() &&
55           (LastTID.TSFlags & ARMII::DomainMask) == ARMII::DomainGeneral) {
56         MachineBasicBlock::iterator I = LastMI;
57         if (I != LastMI->getParent()->begin()) {
58           I = llvm::prior(I);
59           DefMI = &*I;
60         }
61       }
62
63       if (TII.isFpMLxInstruction(DefMI->getOpcode()) &&
64           (TII.canCauseFpMLxStall(MI->getOpcode()) ||
65            hasRAWHazard(DefMI, MI, TRI))) {
66         // Try to schedule another instruction for the next 4 cycles.
67         if (FpMLxStalls == 0)
68           FpMLxStalls = 4;
69         return Hazard;
70       }
71     }
72   }
73
74   return ScoreboardHazardRecognizer::getHazardType(SU, Stalls);
75 }
76
77 void ARMHazardRecognizer::Reset() {
78   LastMI = 0;
79   FpMLxStalls = 0;
80   ITBlockSize = 0;
81   ScoreboardHazardRecognizer::Reset();
82 }
83
84 void ARMHazardRecognizer::EmitInstruction(SUnit *SU) {
85   MachineInstr *MI = SU->getInstr();
86   unsigned Opcode = MI->getOpcode();
87   if (ITBlockSize) {
88     --ITBlockSize;
89   } else if (Opcode == ARM::t2IT) {
90     unsigned Mask = MI->getOperand(1).getImm();
91     unsigned NumTZ = CountTrailingZeros_32(Mask);
92     assert(NumTZ <= 3 && "Invalid IT mask!");
93     ITBlockSize = 4 - NumTZ;
94     MachineBasicBlock::iterator I = MI;
95     for (unsigned i = 0; i < ITBlockSize; ++i) {
96       // Advance to the next instruction, skipping any dbg_value instructions.
97       do {
98         ++I;
99       } while (I->isDebugValue());
100       ITBlockMIs[ITBlockSize-1-i] = &*I;
101     }
102   }
103
104   if (!MI->isDebugValue()) {
105     LastMI = MI;
106     FpMLxStalls = 0;
107   }
108
109   ScoreboardHazardRecognizer::EmitInstruction(SU);
110 }
111
112 void ARMHazardRecognizer::AdvanceCycle() {
113   if (FpMLxStalls && --FpMLxStalls == 0)
114     // Stalled for 4 cycles but still can't schedule any other instructions.
115     LastMI = 0;
116   ScoreboardHazardRecognizer::AdvanceCycle();
117 }
118
119 void ARMHazardRecognizer::RecedeCycle() {
120   llvm_unreachable("reverse ARM hazard checking unsupported");
121 }