[PATCH] D38941: [PowerPC] Use record-form instruction for Less-or-Equal -1 and Greater-or-Equal 1

Hiroshi Inoue via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 16 00:09:24 PDT 2017


inouehrs created this revision.

Currently a record-form instruction is used for comparison of "greater than -1" and "less than 1" by modifying the predicate (e.g. LT 1 into LE 0) in addition to the naive case of comparison against 0.
This patch also enables emitting a record-form instruction for "less than or equal to -1" (i.e. "less than 0") and "greater than or equal to 1" (i.e. "greater than 0") to increase the optimization opportunities.


https://reviews.llvm.org/D38941

Files:
  lib/Target/PowerPC/PPCInstrInfo.cpp
  test/CodeGen/PowerPC/opt-cmp-inst-cr0-live.ll


Index: test/CodeGen/PowerPC/opt-cmp-inst-cr0-live.ll
===================================================================
--- test/CodeGen/PowerPC/opt-cmp-inst-cr0-live.ll
+++ test/CodeGen/PowerPC/opt-cmp-inst-cr0-live.ll
@@ -78,3 +78,36 @@
 }
 
 declare void @exit(i32 signext)
+
+
+; CHECK-LABEL: fn5
+define i8* @fn5(i8* readonly %p) {
+; CHECK: LBZU
+; CHECK: EXTSBo
+; CHECK-NOT: CMP
+; CHECK: BCC 12
+; CHECK: LBZU
+; CHECK: EXTSBo
+; CHECK-NOT: CMP
+; CHECK: BCC 4
+
+entry:
+  %incdec.ptr = getelementptr inbounds i8, i8* %p, i64 -1
+  %0 = load i8, i8* %incdec.ptr
+  %cmp = icmp sgt i8 %0, -1
+  br i1 %cmp, label %out, label %if.end
+
+if.end:
+  %incdec.ptr2 = getelementptr inbounds i8, i8* %p, i64 -2
+  %1 = load i8, i8* %incdec.ptr2
+  %cmp4 = icmp sgt i8 %1, -1
+  br i1 %cmp4, label %out, label %cleanup
+
+out:
+  %p.addr.0 = phi i8* [ %incdec.ptr, %entry ], [ %incdec.ptr2, %if.end ]
+  br label %cleanup
+
+cleanup:
+  %retval.0 = phi i8* [ %p.addr.0, %out ], [ null, %if.end ]
+  ret i8* %retval.0
+}
Index: lib/Target/PowerPC/PPCInstrInfo.cpp
===================================================================
--- lib/Target/PowerPC/PPCInstrInfo.cpp
+++ lib/Target/PowerPC/PPCInstrInfo.cpp
@@ -1738,25 +1738,32 @@
       MachineInstr *UseMI = &*MRI->use_instr_begin(CRReg);
       if (UseMI->getOpcode() == PPC::BCC) {
         PPC::Predicate Pred = (PPC::Predicate)UseMI->getOperand(0).getImm();
+        PPC::Predicate NewPred = Pred;
         unsigned PredCond = PPC::getPredicateCondition(Pred);
         unsigned PredHint = PPC::getPredicateHint(Pred);
         int16_t Immed = (int16_t)Value;
 
         // When modyfing the condition in the predicate, we propagate hint bits
         // from the original predicate to the new one.
-        if (Immed == -1 && PredCond == PPC::PRED_GT) {
+        if (Immed == -1 && PredCond == PPC::PRED_GT)
           // We convert "greater than -1" into "greater than or equal to 0",
           // since we are assuming signed comparison by !equalityOnly
-          PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
-                                  PPC::getPredicate(PPC::PRED_GE, PredHint)));
-          Success = true;
-        }
-        else if (Immed == 1 && PredCond == PPC::PRED_LT) {
+          NewPred = PPC::getPredicate(PPC::PRED_GE, PredHint);
+        else if (Immed == -1 && PredCond == PPC::PRED_LE)
+          // We convert "less than or equal to -1" into "less than 0".
+          NewPred = PPC::getPredicate(PPC::PRED_LT, PredHint);
+        else if (Immed == 1 && PredCond == PPC::PRED_LT)
           // We convert "less than 1" into "less than or equal to 0".
-          PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
-                                  PPC::getPredicate(PPC::PRED_LE, PredHint)));
-          Success = true;
-        }
+          NewPred = PPC::getPredicate(PPC::PRED_LE, PredHint);
+        else if (Immed == 1 && PredCond == PPC::PRED_GE)
+          // We convert "greater than or equal to 1" into "greater than 0".
+          NewPred = PPC::getPredicate(PPC::PRED_GT, PredHint);
+        else
+          return false;
+
+        PredsToUpdate.push_back(std::make_pair(&(UseMI->getOperand(0)),
+                                               NewPred));
+        Success = true;
       }
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38941.119108.patch
Type: text/x-patch
Size: 3333 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171016/68c28345/attachment.bin>


More information about the llvm-commits mailing list