Skip to content

Commit 6ed47be

Browse files
committedFeb 21, 2019
[ARM] Negative constants mishandled in ARM CGP
During type promotion, sometimes we convert negative an add with a negative constant into a sub with a positive constant. The loop that performs this transformation has two issues: - it iterates over a set, causing non-determinism. - it breaks, instead of continuing, when it finds the first non-negative operand. Differential Revision: https://reviews.llvm.org/D58452 llvm-svn: 354557
1 parent 0e98ad2 commit 6ed47be

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed
 

‎llvm/lib/Target/ARM/ARMCodeGenPrepare.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class IRPromoter {
119119
// This defines the max range of the values that we allow in the promoted
120120
// tree.
121121
IntegerType *OrigTy = nullptr;
122-
SmallPtrSetImpl<Value*> *Visited;
122+
SetVector<Value*> *Visited;
123123
SmallPtrSetImpl<Value*> *Sources;
124124
SmallPtrSetImpl<Instruction*> *Sinks;
125125
SmallPtrSetImpl<Instruction*> *SafeToPromote;
@@ -138,7 +138,7 @@ class IRPromoter {
138138

139139

140140
void Mutate(Type *OrigTy,
141-
SmallPtrSetImpl<Value*> &Visited,
141+
SetVector<Value*> &Visited,
142142
SmallPtrSetImpl<Value*> &Sources,
143143
SmallPtrSetImpl<Instruction*> &Sinks,
144144
SmallPtrSetImpl<Instruction*> &SafeToPromote);
@@ -498,7 +498,7 @@ void IRPromoter::PrepareConstants() {
498498

499499
if (auto *Const = dyn_cast<ConstantInt>(I->getOperand(1))) {
500500
if (!Const->isNegative())
501-
break;
501+
continue;
502502

503503
unsigned Opc = I->getOpcode();
504504
if (Opc != Instruction::Add && Opc != Instruction::Sub)
@@ -755,7 +755,7 @@ void IRPromoter::ConvertTruncs() {
755755
}
756756

757757
void IRPromoter::Mutate(Type *OrigTy,
758-
SmallPtrSetImpl<Value*> &Visited,
758+
SetVector<Value*> &Visited,
759759
SmallPtrSetImpl<Value*> &Sources,
760760
SmallPtrSetImpl<Instruction*> &Sinks,
761761
SmallPtrSetImpl<Instruction*> &SafeToPromote) {
@@ -935,7 +935,7 @@ bool ARMCodeGenPrepare::TryToPromote(Value *V) {
935935
SetVector<Value*> WorkList;
936936
SmallPtrSet<Value*, 8> Sources;
937937
SmallPtrSet<Instruction*, 4> Sinks;
938-
SmallPtrSet<Value*, 16> CurrentVisited;
938+
SetVector<Value*> CurrentVisited;
939939
WorkList.insert(V);
940940

941941
// Return true if V was added to the worklist as a supported instruction,

‎llvm/test/CodeGen/ARM/CGP/arm-cgp-overflow.ll

+17
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,20 @@ entry:
230230
%conv4 = zext i1 %cmp to i32
231231
ret i32 %conv4
232232
}
233+
234+
; CHECK-LABEL: convert_add_order
235+
; CHECK: orr{{.*}}, #1
236+
; CHECK: sub{{.*}}, #40
237+
; CHECK-NOT: uxt
238+
define i8 @convert_add_order(i8 zeroext %arg) {
239+
%mask.0 = and i8 %arg, 1
240+
%mask.1 = and i8 %arg, 2
241+
%shl = or i8 %arg, 1
242+
%add = add nuw i8 %shl, 10
243+
%cmp.0 = icmp ult i8 %add, 60
244+
%sub = add nsw i8 %shl, -40
245+
%cmp.1 = icmp ult i8 %sub, 20
246+
%mask.sel = select i1 %cmp.1, i8 %mask.0, i8 %mask.1
247+
%res = select i1 %cmp.0, i8 %mask.sel, i8 %arg
248+
ret i8 %res
249+
}

0 commit comments

Comments
 (0)
Please sign in to comment.