Skip to content

Commit

Permalink
Fix mix-type arithmetic detection in umin/max expansion (#28465)
Browse files Browse the repository at this point in the history
Pointers can have different types. For these, a simple bitcast
suffices rather than going through inttoptr.

Fixes #28464
  • Loading branch information
Keno authored Aug 6, 2018
1 parent f5a0b03 commit 717ff90
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions deps/patches/llvm-D50167-scev-umin.patch
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,46 @@ index bfff7afb5b4..750c1fdfdfb 100644
bool Proper = true;
for (const SCEV *NAryOp : NAry->operands()) {
diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp
index 53ce33bacbe..3179de31b92 100644
index 01a8732b0b8..8160a1eaa0b 100644
--- a/lib/Analysis/ScalarEvolutionExpander.cpp
+++ b/lib/Analysis/ScalarEvolutionExpander.cpp
@@ -1671,6 +1671,54 @@ Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
@@ -1634,14 +1634,15 @@ Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
for (int i = S->getNumOperands()-2; i >= 0; --i) {
// In the case of mixed integer and pointer types, do the
// rest of the comparisons as integer.
- if (S->getOperand(i)->getType() != Ty) {
+ Type *OpTy = S->getOperand(i)->getType();
+ if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
Ty = SE.getEffectiveSCEVType(Ty);
LHS = InsertNoopCastOfTo(LHS, Ty);
}
Value *RHS = expandCodeFor(S->getOperand(i), Ty);
Value *ICmp = Builder.CreateICmpSGT(LHS, RHS);
rememberInstruction(ICmp);
- Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
+ Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smin");
rememberInstruction(Sel);
LHS = Sel;
}
@@ -1658,14 +1659,15 @@ Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
for (int i = S->getNumOperands()-2; i >= 0; --i) {
// In the case of mixed integer and pointer types, do the
// rest of the comparisons as integer.
- if (S->getOperand(i)->getType() != Ty) {
+ Type *OpTy = S->getOperand(i)->getType();
+ if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
Ty = SE.getEffectiveSCEVType(Ty);
LHS = InsertNoopCastOfTo(LHS, Ty);
}
Value *RHS = expandCodeFor(S->getOperand(i), Ty);
Value *ICmp = Builder.CreateICmpUGT(LHS, RHS);
rememberInstruction(ICmp);
- Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
+ Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umin");
rememberInstruction(Sel);
LHS = Sel;
}
@@ -1671,6 +1671,56 @@ Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
return LHS;
}

Expand All @@ -634,7 +670,8 @@ index 53ce33bacbe..3179de31b92 100644
+ for (int i = S->getNumOperands()-2; i >= 0; --i) {
+ // In the case of mixed integer and pointer types, do the
+ // rest of the comparisons as integer.
+ if (S->getOperand(i)->getType() != Ty) {
+ Type *OpTy = S->getOperand(i)->getType();
+ if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
+ Ty = SE.getEffectiveSCEVType(Ty);
+ LHS = InsertNoopCastOfTo(LHS, Ty);
+ }
Expand All @@ -658,7 +695,8 @@ index 53ce33bacbe..3179de31b92 100644
+ for (int i = S->getNumOperands()-2; i >= 0; --i) {
+ // In the case of mixed integer and pointer types, do the
+ // rest of the comparisons as integer.
+ if (S->getOperand(i)->getType() != Ty) {
+ Type *OpTy = S->getOperand(i)->getType();
+ if (OpTy->isIntegerTy() != Ty->isIntegerTy()) {
+ Ty = SE.getEffectiveSCEVType(Ty);
+ LHS = InsertNoopCastOfTo(LHS, Ty);
+ }
Expand Down

0 comments on commit 717ff90

Please sign in to comment.