X86: templatize GenerateTestAndBranch and friends

Allow the use of NearLabel as well as Label.  This will be used by the
HSelect patch.

Replace a couple of Label(s) with NearLabel(s) as well.

Change-Id: I8e674c89e691bcdbccf4a5cdc07ad13b29ec21dd
Signed-off-by: Mark Mendell <mark.p.mendell@intel.com>
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index fd18917..a808c27 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1335,9 +1335,10 @@
 void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
-                                                  Label* true_label,
-                                                  Label* false_label) {
+                                                  LabelType* true_label,
+                                                  LabelType* false_label) {
   if (cond->IsFPConditionTrueIfNaN()) {
     __ j(kUnordered, true_label);
   } else if (cond->IsFPConditionFalseIfNaN()) {
@@ -1346,9 +1347,10 @@
   __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
-                                                               Label* true_label,
-                                                               Label* false_label) {
+                                                               LabelType* true_label,
+                                                               LabelType* false_label) {
   LocationSummary* locations = cond->GetLocations();
   Location left = locations->InAt(0);
   Location right = locations->InAt(1);
@@ -1437,14 +1439,15 @@
   __ j(final_condition, true_label);
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
-                                                               Label* true_target_in,
-                                                               Label* false_target_in) {
+                                                               LabelType* true_target_in,
+                                                               LabelType* false_target_in) {
   // Generated branching requires both targets to be explicit. If either of the
   // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
-  Label fallthrough_target;
-  Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
-  Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
+  LabelType fallthrough_target;
+  LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
+  LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
 
   LocationSummary* locations = condition->GetLocations();
   Location left = locations->InAt(0);
@@ -1486,10 +1489,11 @@
          !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
                                                         size_t condition_input_index,
-                                                        Label* true_target,
-                                                        Label* false_target) {
+                                                        LabelType* true_target,
+                                                        LabelType* false_target) {
   HInstruction* cond = instruction->InputAt(condition_input_index);
 
   if (true_target == nullptr && false_target == nullptr) {
@@ -1613,7 +1617,7 @@
   GenerateTestAndBranch(deoptimize,
                         /* condition_input_index */ 0,
                         slow_path->GetEntryLabel(),
-                        /* false_target */ nullptr);
+                        /* false_target */ static_cast<Label*>(nullptr));
 }
 
 void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
@@ -1709,7 +1713,7 @@
   Location lhs = locations->InAt(0);
   Location rhs = locations->InAt(1);
   Register reg = locations->Out().AsRegister<Register>();
-  Label true_label, false_label;
+  NearLabel true_label, false_label;
 
   switch (cond->InputAt(0)->GetType()) {
     default: {
diff --git a/compiler/optimizing/code_generator_x86.h b/compiler/optimizing/code_generator_x86.h
index 3d34317..df73476 100644
--- a/compiler/optimizing/code_generator_x86.h
+++ b/compiler/optimizing/code_generator_x86.h
@@ -267,15 +267,22 @@
 
   void GenerateImplicitNullCheck(HNullCheck* instruction);
   void GenerateExplicitNullCheck(HNullCheck* instruction);
+  template<class LabelType>
   void GenerateTestAndBranch(HInstruction* instruction,
                              size_t condition_input_index,
-                             Label* true_target,
-                             Label* false_target);
+                             LabelType* true_target,
+                             LabelType* false_target);
+  template<class LabelType>
   void GenerateCompareTestAndBranch(HCondition* condition,
-                                    Label* true_target,
-                                    Label* false_target);
-  void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
-  void GenerateLongComparesAndJumps(HCondition* cond, Label* true_label, Label* false_label);
+                                    LabelType* true_target,
+                                    LabelType* false_target);
+  template<class LabelType>
+  void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
+  template<class LabelType>
+  void GenerateLongComparesAndJumps(HCondition* cond,
+                                    LabelType* true_label,
+                                    LabelType* false_label);
+
   void HandleGoto(HInstruction* got, HBasicBlock* successor);
   void GenPackedSwitchWithCompares(Register value_reg,
                                    int32_t lower_bound,
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 7c94a8c..76a4ce2 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1370,9 +1370,10 @@
 void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
-                                                     Label* true_label,
-                                                     Label* false_label) {
+                                                     LabelType* true_label,
+                                                     LabelType* false_label) {
   if (cond->IsFPConditionTrueIfNaN()) {
     __ j(kUnordered, true_label);
   } else if (cond->IsFPConditionFalseIfNaN()) {
@@ -1381,14 +1382,15 @@
   __ j(X86_64FPCondition(cond->GetCondition()), true_label);
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
-                                                                  Label* true_target_in,
-                                                                  Label* false_target_in) {
+                                                                  LabelType* true_target_in,
+                                                                  LabelType* false_target_in) {
   // Generated branching requires both targets to be explicit. If either of the
   // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
-  Label fallthrough_target;
-  Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
-  Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
+  LabelType fallthrough_target;
+  LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
+  LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
 
   LocationSummary* locations = condition->GetLocations();
   Location left = locations->InAt(0);
@@ -1470,10 +1472,11 @@
          !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
 }
 
+template<class LabelType>
 void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
                                                            size_t condition_input_index,
-                                                           Label* true_target,
-                                                           Label* false_target) {
+                                                           LabelType* true_target,
+                                                           LabelType* false_target) {
   HInstruction* cond = instruction->InputAt(condition_input_index);
 
   if (true_target == nullptr && false_target == nullptr) {
@@ -1597,7 +1600,7 @@
   GenerateTestAndBranch(deoptimize,
                         /* condition_input_index */ 0,
                         slow_path->GetEntryLabel(),
-                        /* false_target */ nullptr);
+                        /* false_target */ static_cast<Label*>(nullptr));
 }
 
 void LocationsBuilderX86_64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
@@ -1684,7 +1687,7 @@
   Location lhs = locations->InAt(0);
   Location rhs = locations->InAt(1);
   CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
-  Label true_label, false_label;
+  NearLabel true_label, false_label;
 
   switch (cond->InputAt(0)->GetType()) {
     default:
@@ -5747,7 +5750,7 @@
                                                            is_type_check_slow_path_fatal);
   codegen_->AddSlowPath(type_check_slow_path);
 
-  Label done;
+  NearLabel done;
   // Avoid null check if we know obj is not null.
   if (instruction->MustDoNullCheck()) {
     __ testl(obj, obj);
diff --git a/compiler/optimizing/code_generator_x86_64.h b/compiler/optimizing/code_generator_x86_64.h
index 9995416..c5e8a04 100644
--- a/compiler/optimizing/code_generator_x86_64.h
+++ b/compiler/optimizing/code_generator_x86_64.h
@@ -258,14 +258,18 @@
   void GenerateExplicitNullCheck(HNullCheck* instruction);
   void PushOntoFPStack(Location source, uint32_t temp_offset,
                        uint32_t stack_adjustment, bool is_float);
+  template<class LabelType>
   void GenerateTestAndBranch(HInstruction* instruction,
                              size_t condition_input_index,
-                             Label* true_target,
-                             Label* false_target);
+                             LabelType* true_target,
+                             LabelType* false_target);
+  template<class LabelType>
   void GenerateCompareTestAndBranch(HCondition* condition,
-                                    Label* true_target,
-                                    Label* false_target);
-  void GenerateFPJumps(HCondition* cond, Label* true_label, Label* false_label);
+                                    LabelType* true_target,
+                                    LabelType* false_target);
+  template<class LabelType>
+  void GenerateFPJumps(HCondition* cond, LabelType* true_label, LabelType* false_label);
+
   void HandleGoto(HInstruction* got, HBasicBlock* successor);
 
   X86_64Assembler* const assembler_;